Skip to content

Commit

Permalink
autopopulate fields / #214
Browse files Browse the repository at this point in the history
  • Loading branch information
eliselavy committed Jan 16, 2024
1 parent d0584fb commit db0e908
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 27 deletions.
9 changes: 8 additions & 1 deletion jdhapi/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ class ArticleAdmin(admin.ModelAdmin):
list_display = ['abstract_pid', 'issue_name', 'issue', 'abstract_title', 'status', 'clickable_dataverse_url']
list_filter = ('issue__name', 'status', 'copyright_type', EmptyDataverseURLFilter)
actions = [save_notebook_fingerprint, save_notebook_specific_cell, save_article_citation, save_article_package, save_article_references]
#readonly_fields = ("notebook_url", "notebook_path", "binder_url", "notebook_ipython_url")

fieldsets = (
(
"Information related to the article", {
Expand All @@ -126,7 +128,12 @@ class ArticleAdmin(admin.ModelAdmin):
# Section Description
# "description" : "Enter the vehicle information",
# Group Make and Model
"fields": (("repository_type", "repository_url"), "notebook_url", "notebook_path", "binder_url", "notebook_ipython_url", "dataverse_url")
"fields": (("repository_type", "full_url_article_path"), ("repository_url", "notebook_url", "notebook_path", "binder_url", "notebook_ipython_url"))
}
),
(
"Information related to the dataverse repository", {
"fields": ("dataverse_url",)
}
),
(
Expand Down
5 changes: 4 additions & 1 deletion jdhapi/forms/articleForm.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ class Meta:
model = Article
fields = '__all__'

def clean(self):
""" def clean(self):
# Get the article data
article = self.instance
full_url_article_path = self.cleaned_data['full_url_article_path']
doi = self.cleaned_data['doi']
status = self.cleaned_data['status']
repository_url = self.cleaned_data['repository_url']
Expand All @@ -29,6 +30,7 @@ def clean(self):
notebook_ipython_url = self.cleaned_data['notebook_ipython_url']
if self.has_changed():
logger.info("The following fields changed: %s" % ", ".join(self.changed_data))
if 'status' in self.changed_data:
# IF PUBLISHED
if status == Article.Status.PUBLISHED:
Expand Down Expand Up @@ -60,3 +62,4 @@ def clean(self):
logger.info("no changed fields")
"""
18 changes: 18 additions & 0 deletions jdhapi/migrations/0039_article_full_url_article_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.14 on 2024-01-11 14:51

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('jdhapi', '0038_article_dataverse_url'),
]

operations = [
migrations.AddField(
model_name='article',
name='full_url_article_path',
field=models.CharField(blank=True, help_text='Full URL of the article', max_length=254, null=True),
),
]
10 changes: 8 additions & 2 deletions jdhapi/models/article.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.db.models.signals import post_save
from django.dispatch import receiver
from lxml import html
from django.dispatch import receiver
from model_utils import FieldTracker

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -43,8 +44,9 @@ class RepositoryType(models.TextChoices):
choices=Status.choices,
default=Status.DRAFT,
)
tracker = FieldTracker(fields=['status'])

issue = models.ForeignKey('jdhapi.Issue', on_delete=models.CASCADE)
full_url_article_path = models.CharField(max_length=254, null=True, blank=True, help_text="Full URL of the article")
repository_url = models.URLField(max_length=254, null=True, blank=True, help_text="GitHub's repository URL ")
repository_type = models.CharField(
max_length=15,
Expand Down Expand Up @@ -79,6 +81,8 @@ class RepositoryType(models.TextChoices):
tags = models.ManyToManyField('jdhapi.Tag', blank=True)
authors = models.ManyToManyField('jdhapi.Author', through='Role')

tracker = FieldTracker(fields=['status','full_url_article_path'])

def get_kernel_language(self):
tool_tags = self.tags.filter(category='tool')
if tool_tags.exists():
Expand Down Expand Up @@ -119,4 +123,6 @@ def send_email_if_peer_review(self):
email.send()
logger.info("Email sent")
except Exception as e:
print(f'Error sending email: {str(e)}')
print(f'Error sending email: {str(e)}')


2 changes: 1 addition & 1 deletion jdhapi/serializers/article.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Meta:
model = Article
fields = [
"abstract", "repository_url", "status", "publication_date", "repository_type",
"copyright_type", "notebook_url", "notebook_commit_hash", "notebook_path",
"copyright_type", "notebook_url", "full_url_article_path" , "notebook_commit_hash", "notebook_path",
"binder_url", "doi", "dataverse_url", "data", "citation", "kernel_language",
"tags", "issue", "authors", "fingerprint"
]
25 changes: 23 additions & 2 deletions jdhapi/signals.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.db.models.signals import pre_save, post_save
from django.core.mail import send_mail
from jdhapi.models import Article
from django.dispatch import receiver
from jdhapi.utils.articleUtils import get_notebook_from_github
import logging

logger = logging.getLogger(__name__)


@receiver(post_save, sender=Article)
def send_email_for_peer_review_article(sender, instance, created, **kwargs):
if not created and instance.tracker.has_changed('status') and instance.status == Article.Status.PEER_REVIEW:
instance.send_email_if_peer_review()

@receiver(pre_save, sender=Article)
def pre_save_article(sender, instance, **kwargs):
logger.debug("Inside pre_save_article")

# Check if full_url_article_path has changed
if instance.tracker.has_changed('full_url_article_path'):
# Update other fields if necessary
repository_url, notebook_path, notebook_ipython_url = get_notebook_from_github(instance.full_url_article_path)

# Set the values directly without calling save
instance.notebook_ipython_url = notebook_ipython_url
instance.repository_url = repository_url
instance.notebook_path = notebook_path

logger.debug("Fields populated: notebook_ipython_url=%s, repository_url=%s, notebook_path=%s" % (
instance.notebook_ipython_url, instance.repository_url, instance.notebook_path))
44 changes: 25 additions & 19 deletions jdhapi/utils/articleUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,34 @@ def get_notebook_from_raw_github(raw_url):
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")


def get_notebook_from_github(
repository_url, host='https://raw.githubusercontent.com'
):
logger.info(
f'get_notebook_from_github - parsing repository_url: {repository_url}')
result = re.search(
r'github\.com\/([^\/]+)\/([^\/]+)\/(blob\/)?(.*\.ipynb$)',
repository_url
)
github_user = result.group(1)
github_repo = result.group(2)
github_filepath = result.group(4)
raw_url = f'{host}/{github_user}/{github_repo}/{github_filepath}'
# https://raw.githubusercontent.com/jdh-observer/jdh001-WBqfZzfi7nHK/blob/8315a108416f4a5e9e6da0c5e9f18b5e583ed825/scripts/Digital_epigraphy_cite2c_biblio.ipynb
# Match github.com/<github_user abc>/<github_filepath XXX/yyy/zzz.ipynb>
# and exclude the `/blob/` part of the url if any.
# then extract the gighub username nd the filepath to download
# conveniently from githubusercontent server.
logger.info(f'get_notebook_from_github - requesting raw_url: {raw_url}...')
return get_notebook_from_raw_github(raw_url)
try:
logger.info(
f'get_notebook_from_github - parsing repository_url: {repository_url}')
result = re.search(
r'github\.com\/([^\/]+)\/([^\/]+)\/(blob\/)?(.*\.ipynb$)',
repository_url
)
github_user = result.group(1)
github_repo = result.group(2)
github_filepath = result.group(4)
github_full_url = f'https://github.com/{github_user}/{github_repo}'
raw_url = f'{host}/{github_user}/{github_repo}/{github_filepath}'

logger.info(f'get_notebook_from_github - requesting github_filepath: {github_filepath}...')
logger.info(f'get_notebook_from_github - requesting github_repo: {github_full_url}...')
logger.info(f'get_notebook_from_github - requesting raw_url: {raw_url}...')

return github_full_url, github_filepath, raw_url

except requests.exceptions.RequestException as e:
logger.error(f'Error accessing GitHub URL: {e}')
return None
except Exception as e:
logger.error(f'An unexpected error occurred: {e}')
return None


# Method to use to generate the fingerprint datas
Expand Down Expand Up @@ -417,7 +424,6 @@ def convert_notebook(notebook_file, output_format='pdf'):
try:
# Run the nbconvert command to generate the output file
command = f'jupyter nbconvert --to {output_format} {notebook_file}'
#subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, text=True)

logger.info("Conversion successful!")
Expand Down
1 change: 0 additions & 1 deletion notebook_with_ref.ipynb

This file was deleted.

Binary file removed notebook_with_ref.pdf
Binary file not shown.
Binary file removed peer_review_L2gBr3BzwH8Z.pdf
Binary file not shown.
13 changes: 13 additions & 0 deletions subprocess_article.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import subprocess

notebook_file = 'notebook_with_ref.ipynb'
output_format = 'pdf'
command = f'jupyter nbconvert --to {output_format} {notebook_file}'

try:
result = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, text=True)
print("Conversion successful!")
print(result) # Print the entire output
except subprocess.CalledProcessError as e:
print(f"Error during conversion: {e}")
print("Command output:\n", e.output)

0 comments on commit db0e908

Please sign in to comment.