add keyword mapping

pull/1/head
eric 2015-03-12 11:58:49 -04:00
parent 99350d6cc4
commit 576eb6d872
6 changed files with 148 additions and 10 deletions

View File

@ -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',
)
)
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',
)

View File

@ -0,0 +1,86 @@
{% extends "base.html" %}
{% block extra_css %}
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/themes/ui-lightness/jquery-ui.css" type="text/css" media="screen">
{{ form.media.css }}
<style type="text/css">
div.half {
width: 45%;
margin-left: 5%;
margin-bottom: 1em;
float: left;
}
</style>
{% endblock %}
{% block extra_js %}
<script type="text/javascript" src="{{ jquery_ui_home }}"></script>
{{ form.media.js }}
{% endblock %}
{% block content %}
<div id="js-topsection">
<div class="js-main">
{% if map_complete %}
<h1 class="yikes">Mapping successful</h1>
<p> {{ added }} works, {{ added_free }} of them free, added to <b>{{ onto_subject.name }}</b></p>
<div class="half">
<h3>Added this keyword...</h3>
{% with onto_subject as subject%}
{% include 'subjectbox.html' %}
{% endwith %}
</div>
<div class="half">
<h3>... to works with this keyword.</h3>
{% include 'subjectbox.html' %}
</div>
<h1>Map more keywords</h1>
<form action="#" method="POST">
{% csrf_token %}
<div class="half">
<h3>Add this keyword...</h3>
{{ form.onto_subject }}
</div>
<div class="half">
<h3>... to works with this keyword.</h3>
{{ form.subject }}
</div>
<input type="submit" name="merge_works" value="Preview Keyword Mapping" id="submit" />
</form>
{% else %}
<h1>Keyword mapping</h1>
<form action="#" method="POST">
{% csrf_token %}
<div class="half">
<h3>Add this keyword...</h3>
{% if onto_subject %}
<div style="display:none">{{ form.onto_subject }}</div>
{% with onto_subject as subject%}
{% include 'subjectbox.html' %}
{% endwith %}
{% else %}
{{ form.onto_subject.errors }}{{ form.onto_subject }}
{% endif %}
</div>
<div class="half">
<h3>... to works with this keyword.</h3>
{% if subject %}
<div style="display:none">{{ form.subject }}</div>
{% include 'subjectbox.html' %}
{% else %}
{{ form.subject.errors }}{{ form.subject }}
{% endif %}
</div>
<br clear="left" />
{% if subject and onto_subject %}
<p>This can't be undone, so be sure you want to do this!</p>
<input type="submit" name="confirm_map_subject" value="Confirm Keyword Mapping" id="submit" />
{% else %}
<input type="submit" name="merge_works" value="Preview Keyword Mapping" id="submit" />
{% endif %}
</form>
{% endif %}
</div>
</div>
{% endblock %}

View File

@ -0,0 +1,7 @@
<div class="workbox">
<a href="{% url free %}kw.{{ subject.name }}/">{{ subject.name }}</a> <br />
Works with this keyword: {{ subject.works.all.count }}<br />
Free Works with this keyword: {{ subject.free_works.count }} <br />
<a href="{% url free %}?setkw={{ subject.name }}">Set</a> keywords. <br />
<a href="{% url admin:core_subject_change subject.id %}">Edit</a> keyword. <br />
</div>

View File

@ -3,19 +3,22 @@
{% block content %}
<div id="js-topsection">
<div class="js-main">
<h1>Subjects</h1>
<h1>Keywords</h1>
<p>
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.
</p>
<ul>
<li> ordered by <a href="{% url subjects %}">subject name</a> </li>
<li> ordered by <a href="{% url subjects %}?order=count">number</a> of works with the subject.</li>
<li> ordered by <a href="{% url subjects %}?subset=free">subject name</a> (free works only)</li>
<li> ordered by <a href="{% url subjects %}?order=count&amp;subset=free">number</a> of free works with the subject.</li>
<li> <a href="{% url free %}?order_by=subjects">free works needing subjects</a>
<li> ordered by <a href="{% url subjects %}">keyword name</a> </li>
<li> ordered by <a href="{% url subjects %}?order=count">number</a> of works with the keyword.</li>
<li> ordered by <a href="{% url subjects %}?subset=free">keyword name</a> (free works only)</li>
<li> ordered by <a href="{% url subjects %}?order=count&amp;subset=free">number</a> of free works with the keyword.</li>
<li> <a href="{% url free %}?order_by=subjects">free works needing keywords</a></li>
{% if request.user.is_staff %}
<li> <a href="{% url map_subject %}">keyword mapping form</a></li>
{% endif %}
</ul>
<p>

View File

@ -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<work_id>\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'}),

View File

@ -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):