mark books final by form
authorJan Szejko <janek37@gmail.com>
Wed, 29 Nov 2017 11:19:41 +0000 (12:19 +0100)
committerJan Szejko <janek37@gmail.com>
Wed, 29 Nov 2017 11:19:41 +0000 (12:19 +0100)
apps/catalogue/forms.py
apps/catalogue/templates/catalogue/mark_final.html [new file with mode: 0644]
apps/catalogue/templates/catalogue/mark_final_completed.html [new file with mode: 0644]
apps/catalogue/urls.py
apps/catalogue/views.py

index 40c9608..bf51f40 100644 (file)
@@ -190,7 +190,39 @@ class ReadonlyImageForm(ImageForm):
     """Form used for not editing an Image."""
 
     def __init__(self, *args, **kwargs):
-        ret = super(ReadonlyImageForm, self).__init__(*args, **kwargs)
+        super(ReadonlyImageForm, self).__init__(*args, **kwargs)
         for field in self.fields.values():
             field.widget.attrs.update({"disabled": "disabled"})
-        return ret
+
+
+class MarkFinalForm(forms.Form):
+    username = forms.CharField(initial='lipszyc')
+    comment = forms.CharField(initial=u'Ostateczna akceptacja merytoryczna przez kierownika literackiego.')
+    books = forms.CharField(widget=forms.Textarea, help_text=u'linki do książek w redakcji, po jednym na wiersz')
+
+    def clean_books(self):
+        books_value = self.cleaned_data['books']
+        slugs = [line.split('/')[-2] for line in books_value.split('\n') if line.strip()]
+        books = Book.objects.filter(slug__in=slugs)
+        if len(books) != len(slugs):
+            raise forms.ValidationError(
+                'Incorrect slug(s): %s' % ' '.join(slug for slug in slugs if not Book.objects.filter(slug=slug)))
+        return books
+
+    def clean_username(self):
+        username = self.cleaned_data['username']
+        if not User.objects.filter(username=username):
+            raise forms.ValidationError('Invalid username')
+        return username
+
+    def save(self):
+        for book in self.cleaned_data['books']:
+            for chunk in book.chunk_set.all():
+                src = chunk.head.materialize()
+                chunk.commit(
+                    text=src,
+                    author=User.objects.get(username=self.cleaned_data['username']),
+                    description=self.cleaned_data['comment'],
+                    tags=[Chunk.tag_model.objects.get(slug='editor-proofreading')],
+                    publishable=True
+                )
diff --git a/apps/catalogue/templates/catalogue/mark_final.html b/apps/catalogue/templates/catalogue/mark_final.html
new file mode 100644 (file)
index 0000000..9ed740c
--- /dev/null
@@ -0,0 +1,16 @@
+{% extends "catalogue/base.html" %}
+
+{% block titleextra %}Oznacz książki{% endblock %}
+
+
+{% block leftcolumn %}
+
+<h1>Oznacz książki</h1>
+
+<form method="post" action="">
+    {% csrf_token %}
+    {{ form.as_p }}
+    <input type="submit" value="Oznacz">
+</form>
+
+{% endblock leftcolumn %}
\ No newline at end of file
diff --git a/apps/catalogue/templates/catalogue/mark_final_completed.html b/apps/catalogue/templates/catalogue/mark_final_completed.html
new file mode 100644 (file)
index 0000000..1b37c83
--- /dev/null
@@ -0,0 +1,12 @@
+{% extends "catalogue/base.html" %}
+
+{% block titleextra %}Oznaczono książki{% endblock %}
+
+
+{% block leftcolumn %}
+
+<h1>Oznaczono książki</h1>
+
+<p>Książki zostały oznaczone.</p>
+
+{% endblock leftcolumn %}
\ No newline at end of file
index 164ec1b..022839e 100644 (file)
@@ -57,4 +57,7 @@ urlpatterns = patterns('catalogue.views',
 
     url(r'^track/(?P<slug>[^/]*)/$', PublishTrackFeed()),
     url(r'^active/$', 'active_users_list', name='active_users_list'),
+
+    url(r'^mark-final/$', 'mark_final', name='mark_final'),
+    url(r'^mark-final-completed/$', 'mark_final_completed', name='mark_final_completed'),
 )
index 3d1a47f..fc572c2 100644 (file)
@@ -15,15 +15,18 @@ from django.db.models import Count, Q
 from django.db import transaction
 from django import http
 from django.http import Http404, HttpResponse, HttpResponseForbidden
+from django.http.response import HttpResponseRedirect
 from django.shortcuts import get_object_or_404, render
 from django.utils.encoding import iri_to_uri
 from django.utils.http import urlquote_plus
 from django.utils.translation import ugettext_lazy as _
 from django.views.decorators.http import require_POST
+from django_cas.decorators import user_passes_test
 
 from apiclient import NotAuthorizedError
 from catalogue import forms
 from catalogue import helpers
+from catalogue.forms import MarkFinalForm
 from catalogue.helpers import active_tab
 from catalogue.models import (Book, Chunk, Image, BookPublishRecord, 
         ChunkPublishRecord, ImagePublishRecord, Project)
@@ -642,3 +645,17 @@ def active_users_list(request):
     })
 
 
+@user_passes_test(lambda u: u.is_superuser)
+def mark_final(request):
+    if request.method == 'POST':
+        form = MarkFinalForm(data=request.POST)
+        if form.is_valid():
+            form.save()
+            return HttpResponseRedirect(reverse('mark_final_completed'))
+    else:
+        form = MarkFinalForm()
+    return render(request, 'catalogue/mark_final.html', {'form': form})
+
+
+def mark_final_completed(request):
+    return render(request, 'catalogue/mark_final_completed.html')