From 576eb6d872fd3cc6005be26ac930eed9505e4143 Mon Sep 17 00:00:00 2001 From: eric Date: Thu, 12 Mar 2015 11:58:49 -0400 Subject: [PATCH] add keyword mapping --- frontend/forms.py | 15 ++++- frontend/templates/map_subject.html | 86 +++++++++++++++++++++++++++++ frontend/templates/subjectbox.html | 7 +++ frontend/templates/subjects.html | 19 ++++--- frontend/urls.py | 2 + frontend/views.py | 29 +++++++++- 6 files changed, 148 insertions(+), 10 deletions(-) create mode 100644 frontend/templates/map_subject.html create mode 100644 frontend/templates/subjectbox.html diff --git a/frontend/forms.py b/frontend/forms.py index 52ebe1ca..c104cb5b 100644 --- a/frontend/forms.py +++ b/frontend/forms.py @@ -50,6 +50,7 @@ from regluit.core.models import ( Work, Press, Libpref, + Subject, TWITTER, FACEBOOK, GRAVATAR, @@ -826,4 +827,16 @@ class SubjectSelectForm(forms.Form): SubjectLookup, widget=AutoCompleteSelectWidget(SubjectLookup,allow_new=False), label='Keyword', - ) \ No newline at end of file + ) +class MapSubjectForm(forms.Form): + subject = AutoCompleteSelectField( + SubjectLookup, + widget=AutoCompleteSelectWidget(SubjectLookup,allow_new=False), + label='Source Subject', + ) + onto_subject = AutoCompleteSelectField( + SubjectLookup, + widget=AutoCompleteSelectWidget(SubjectLookup,allow_new=False), + label='Target Subject', + ) + diff --git a/frontend/templates/map_subject.html b/frontend/templates/map_subject.html new file mode 100644 index 00000000..01d815fc --- /dev/null +++ b/frontend/templates/map_subject.html @@ -0,0 +1,86 @@ +{% extends "base.html" %} +{% block extra_css %} + + {{ form.media.css }} + +{% endblock %} + +{% block extra_js %} + + {{ form.media.js }} +{% endblock %} + +{% block content %} +
+
+ {% if map_complete %} +

Mapping successful

+

{{ added }} works, {{ added_free }} of them free, added to {{ onto_subject.name }}

+
+

Added this keyword...

+ {% with onto_subject as subject%} + {% include 'subjectbox.html' %} + {% endwith %} +
+
+

... to works with this keyword.

+ {% include 'subjectbox.html' %} +
+

Map more keywords

+
+ {% csrf_token %} +
+

Add this keyword...

+ {{ form.onto_subject }} +
+
+

... to works with this keyword.

+ {{ form.subject }} +
+ +
+ {% else %} +

Keyword mapping

+
+ {% csrf_token %} +
+

Add this keyword...

+ {% if onto_subject %} +
{{ form.onto_subject }}
+ {% with onto_subject as subject%} + {% include 'subjectbox.html' %} + {% endwith %} + {% else %} + {{ form.onto_subject.errors }}{{ form.onto_subject }} + {% endif %} +
+
+

... to works with this keyword.

+ {% if subject %} +
{{ form.subject }}
+ {% include 'subjectbox.html' %} + {% else %} + {{ form.subject.errors }}{{ form.subject }} + {% endif %} +
+
+ {% if subject and onto_subject %} +

This can't be undone, so be sure you want to do this!

+ + {% else %} + + {% endif %} +
+ {% endif %} +
+
+ +{% endblock %} + diff --git a/frontend/templates/subjectbox.html b/frontend/templates/subjectbox.html new file mode 100644 index 00000000..15087e68 --- /dev/null +++ b/frontend/templates/subjectbox.html @@ -0,0 +1,7 @@ +
+ {{ subject.name }}
+ Works with this keyword: {{ subject.works.all.count }}
+ Free Works with this keyword: {{ subject.free_works.count }}
+ Set keywords.
+ Edit keyword.
+
diff --git a/frontend/templates/subjects.html b/frontend/templates/subjects.html index 047b8717..af02a335 100644 --- a/frontend/templates/subjects.html +++ b/frontend/templates/subjects.html @@ -3,19 +3,22 @@ {% block content %}
-

Subjects

+

Keywords

- Below is a list of subjects for books that users have added to - wishlists. It is here primarily to show what subjects are present + Below is a list of keywords for books that users have added to + wishlists. It is here primarily to show what keywords are present in the database to guide further development.

diff --git a/frontend/urls.py b/frontend/urls.py index 56f77a2d..51a5badb 100644 --- a/frontend/urls.py +++ b/frontend/urls.py @@ -38,6 +38,7 @@ from regluit.frontend.views import ( LibModeView, DownloadView, FacetedView, + MapSubjectView, ) urlpatterns = patterns( @@ -131,6 +132,7 @@ urlpatterns = patterns( url(r"^purchase/(?P\d+)/download/$", "download_purchased", name="download_purchased"), url(r"^donate_to_campaign/$", csrf_exempt(NonprofitCampaign.as_view()), name="nonprofit"), url(r"^subjects/$", "subjects", name="subjects"), + url(r"^subjects/map/$", login_required(MapSubjectView.as_view()), name="map_subject"), url(r"^librarything/$", LibraryThingView.as_view(), name="librarything"), url(r"^librarything/load/$","librarything_load", name="librarything_load"), url('^404testing/$', direct_to_template, {'template': '404.html'}), diff --git a/frontend/views.py b/frontend/views.py index 9a16005d..a26864c6 100755 --- a/frontend/views.py +++ b/frontend/views.py @@ -119,7 +119,8 @@ from regluit.frontend.forms import ( DateCalculatorForm, UserNamePass, RegiftForm, - SubjectSelectForm + SubjectSelectForm, + MapSubjectForm, ) from regluit.payment import baseprocessor, stripelib @@ -786,6 +787,32 @@ def subjects(request): return render(request, 'subjects.html', {'subjects': subjects}) +class MapSubjectView(FormView): + template_name="map_subject.html" + form_class = MapSubjectForm + + def dispatch(self, request, *args, **kwargs): + if not request.user.is_staff: + return render(request, "admins_only.html") + else: + return super(MapSubjectView, self).dispatch(request, *args, **kwargs) + + def form_valid(self, form): + context=self.get_context_data() + context['subject']=form.cleaned_data['subject'] + context['onto_subject']=form.cleaned_data['onto_subject'] + if self.request.POST.has_key('confirm_map_subject'): + initial_count = context['onto_subject'].works.all().count() + initial_free_count = context['onto_subject'].works.filter(is_free=True).count() + context['onto_subject'].works.add(*list(context['subject'].works.all())) + context['map_complete']=True + context['form'] = MapSubjectForm(initial=form.cleaned_data) + context['added'] = context['onto_subject'].works.all().count() - initial_count + context['added_free'] = context['onto_subject'].works.filter(is_free=True).count() - initial_free_count + else: + context['form']=MapSubjectForm(initial=form.cleaned_data) + return render(self.request, self.template_name, context) + class FilterableListView(ListView): send_marc = False def get_queryset(self):