From 85c4c3601cd99385380bc7a7939d94d7189b63f8 Mon Sep 17 00:00:00 2001 From: eric Date: Tue, 25 Jul 2017 08:21:26 -0400 Subject: [PATCH] small refactor --- frontend/views/__init__.py | 7 ++----- frontend/views/bibedit.py | 14 ++++++++++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/frontend/views/__init__.py b/frontend/views/__init__.py index 74bd1dbf..4ac32308 100755 --- a/frontend/views/__init__.py +++ b/frontend/views/__init__.py @@ -136,7 +136,7 @@ from regluit.marc.views import qs_marc_records from questionnaire.models import Landing, Questionnaire from questionnaire.views import export_summary as answer_summary, export_csv as export_answers -from .bibedit import new_edition, user_can_edit_work, safe_get_work +from .bibedit import new_edition, user_can_edit_work, safe_get_work, get_edition logger = logging.getLogger(__name__) @@ -418,10 +418,7 @@ def edition_uploads(request, edition_id): context = {} if not request.user.is_authenticated() : return render(request, "admins_only.html") - try: - edition = models.Edition.objects.get(id = edition_id) - except models.Edition.DoesNotExist: - raise Http404 + edition = get_edition(edition_id) campaign_type = edition.work.last_campaign().type if not user_can_edit_work(request.user, edition.work): return render(request, "admins_only.html") diff --git a/frontend/views/bibedit.py b/frontend/views/bibedit.py index 979c759e..ce894803 100644 --- a/frontend/views/bibedit.py +++ b/frontend/views/bibedit.py @@ -49,6 +49,15 @@ def add_subject(subject_name, work, authority=''): subject = models.Subject.objects.create(name=subject_name, authority=authority) subject.works.add(work) +def get_edition(edition_id): + ''' + get edition and 404 if not found + ''' + try: + return models.Edition.objects.get(id = edition_id) + except models.Edition.DoesNotExist: + raise Http404 (duplicate-code) + @login_required def new_edition(request, work_id, edition_id, by=None): ''' @@ -73,10 +82,7 @@ def new_edition(request, work_id, edition_id, by=None): alert = '' admin = user_can_edit_work(request.user, work) if edition_id: - try: - edition = models.Edition.objects.get(id = edition_id) - except models.Edition.DoesNotExist: - raise Http404 + edition = get_edition(edition_id) if work: edition.work = work language = edition.work.language