Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Project Report Book Feature #93

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ Other fabfile commands (including rollback) here: https://github.com/TBP-IT/tbpw
## Various links to understand some of the code structure and tools

* fixtures/*.yaml -- https://docs.djangoproject.com/en/2.2/howto/initial-data/
* Project Report pdflatex -- https://www.tug.org/texlive/ (need to install if you plan to develop on or generate a Project Report Books, otherwise you don't need to install it)
1 change: 1 addition & 0 deletions config/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pillow==6.2.0
pycparser==2.19
pyjwt==1.7.1
pynacl==1.3.0
pypandoc==1.5
python3-openid==3.1.0 ; python_version >= '3.0'
pytz==2019.3
git+https://github.com/TBP-IT/recaptcha-client-1.0.6-py3
Expand Down
3 changes: 1 addition & 2 deletions config/tbpweb-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ name: tbpweb-dev
dependencies:
- python=3.7
- pip
- pypandoc=1.6.3
- pip:
- -r requirements.txt
- pre-commit==1.14.4
- pytest==4.3.0
- pytest-django==3.4.7
- tox==3.7.0
- livereload==2.6.0
# - coverage[toml]==6.3.2
# - black==22.3.0
variables:
TBPWEB_MODE: "dev"
DJANGO_SETTINGS_MODULE: "settings"
Expand Down
1 change: 1 addition & 0 deletions config/tbpweb-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: tbpweb-prod
dependencies:
- python=3.7
- pip
- pypandoc=1.6.3
- pip:
- -r requirements.txt
- mysqlclient==2.1.0
Expand Down
3 changes: 1 addition & 2 deletions project_reports/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import sys


class DelayedException(object):
# Taken from http://stackoverflow.com/questions/6126007 ...
# /python-getting-a-traceback-from-a-multiprocessing-process
Expand All @@ -9,4 +8,4 @@ def __init__(self, exc):
_, _, self.traceback = sys.exc_info()

def re_raise(self):
raise self.exc, None, self.traceback
raise self.exc.with_traceback(self.traceback)
2 changes: 1 addition & 1 deletion project_reports/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ProjectReportBookExportForm(forms.Form):

def __init__(self, *args, **kwargs):
super(ProjectReportBookExportForm, self).__init__(*args, **kwargs)
self.terms.choices = self.get_term_choices()
self.fields['terms'].choices = self.get_term_choices()


def get_term_choices(self):
Expand Down
32 changes: 20 additions & 12 deletions project_reports/management/commands/generate_pr_book.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.core.files import File
from django.conf import settings
from django.core.management import BaseCommand
from django.template.loader import render_to_string

Expand All @@ -23,23 +23,26 @@


class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('pr_book_id')

def handle(self, *args, **options):
# Save the PDF to the PR book object, or save the exception if there
# there was a failure
try:
self.unwrapped_handle(*args, **options)
except Exception as e: # pylint: disable=broad-except
pr_book = ProjectReportBook.objects.get(id=args[0])
pr_book = ProjectReportBook.objects.get(id=options['pr_book_id'])
pr_book.exception = DelayedException(e)
pr_book.save()

def unwrapped_handle(self, *args, **options):
pr_book = ProjectReportBook.objects.get(id=args[0])
pr_book = ProjectReportBook.objects.get(id=options['pr_book_id'])

terms = pr_book.terms.order_by('id')
president = Officer.objects.get(
position__long_name='President',
term=list(terms)[-1])
term=terms.last())

pandoc_header = self.get_pandoc_header(terms)
presidents_letter = self.generate_presidents_letter(
Expand Down Expand Up @@ -78,18 +81,21 @@ def unwrapped_handle(self, *args, **options):

def run(context):
# Run LaTeX, storing information in the `context` dictionary
context['proc'] = subprocess.Popen(
['pdflatex', 'book.tex'],
stdin=subprocess.PIPE,
pdflatex_command = ['pdflatex', 'book.tex']
context['proc'] = subprocess.run(
pdflatex_command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stderr=subprocess.PIPE,
input=b'R')
proc = context['proc']
proc.communicate(input='R') # ignore warnings
if not os.path.isfile('book.pdf'):
output_result="NOTE: No book.log - Generally means pdflatex failed to start"
if os.path.isfile('book.log'):
output_result = open('book.log').read()
context['exception'] = subprocess.CalledProcessError(
cmd='pdflatex book.tex',
cmd=" ".join(pdflatex_command),
returncode=proc.returncode,
output=open('book.log').read(),
output=output_result,
)

def run_thread(timeout=100):
Expand All @@ -107,7 +113,9 @@ def run_thread(timeout=100):
run_thread()
run_thread() # run LaTeX twice to create table of contents

pr_book.pdf.save('{}.pdf'.format(pr_book.pk), File(open('book.pdf')))
pr_book_name = '{}.pdf'.format(pr_book.pk)
pr_book.pdf.name = pr_book.pdf.field.upload_to + pr_book_name
shutil.copyfile(src=os.path.join(os.getcwd(), 'book.pdf'), dst=os.path.join(settings.PRIVATE_STORAGE_ROOT, pr_book.pdf.name))

pr_book.save()

Expand Down
1 change: 1 addition & 0 deletions project_reports/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ def get(self, request, *args, **kwargs):
elif not pr_book.pdf:
return render(request, 'project_reports/download_book.html', {})
else:
self.object = pr_book
response = super(PrivateStorageDetailView, self).get(request, args, kwargs)
response.content_type = 'application/pdf'
response['Content-Disposition'] = \
Expand Down