From a720089525ea2cc0d9d72af848eea7fc83cf2ccf Mon Sep 17 00:00:00 2001 From: eric Date: Thu, 17 Nov 2011 14:35:29 -0500 Subject: [PATCH] unglue it admin users can now approve rightsholder claims! --- core/models.py | 6 +++--- frontend/forms.py | 10 +++++++++- frontend/templates/rights_holders.html | 25 +++++++++++++++++++++++-- frontend/views.py | 26 ++++++++++++++++++++------ 4 files changed, 55 insertions(+), 12 deletions(-) diff --git a/core/models.py b/core/models.py index fd18a70a..fb757fc5 100755 --- a/core/models.py +++ b/core/models.py @@ -43,9 +43,9 @@ class CeleryTask(models.Model): class Claim(models.Model): STATUSES = (( - u'Active', u'Claim has been registered and approved.'), - (u'Pending', u'Claim is pending approval.'), - (u'Released', u'Claim has been released.'), + u'active', u'Claim has been registered and approved.'), + (u'pending', u'Claim is pending approval.'), + (u'release', u'Claim has been released.'), ) rights_holder = models.ForeignKey("RightsHolder", related_name="claim", null=False ) work = models.ForeignKey("Work", related_name="claim", null=False ) diff --git a/frontend/forms.py b/frontend/forms.py index c35ccda1..74b18c5f 100644 --- a/frontend/forms.py +++ b/frontend/forms.py @@ -16,6 +16,14 @@ class RightsHolderForm(forms.ModelForm): class Meta: model = RightsHolder + def clean_rights_holder_name(self): + rights_holder_name = self.data["rights_holder_name"] + try: + RightsHolder.objects.get(rights_holder_name__iexact=rights_holder_name) + except User.DoesNotExist: + return rights_holder_name + raise forms.ValidationError(_("Another rights holder with that name already exists.")) + class ProfileForm(forms.ModelForm): clear_facebook=forms.BooleanField(required=False) clear_twitter=forms.BooleanField(required=False) @@ -43,7 +51,7 @@ class UserData(forms.Form): oldusername = self.data["oldusername"] if username != oldusername: try: - User.objects.get(username=username) + User.objects.get(username__iexact=username) except User.DoesNotExist: return username raise forms.ValidationError(_("Another user with that username already exists.")) diff --git a/frontend/templates/rights_holders.html b/frontend/templates/rights_holders.html index 68cc689d..b8d19385 100644 --- a/frontend/templates/rights_holders.html +++ b/frontend/templates/rights_holders.html @@ -8,7 +8,7 @@
{% csrf_token %} {{ form.as_p }} - +

Accepted Rights Holders

@@ -19,8 +19,28 @@ owner: {{ rights_holder.owner }}
{% endfor %} +{% if pending %}

Pending Claims

-{% for claim in pending %} +
+ {{ pending_formset.management_form }} + {% csrf_token %} +{% for claim, claim_form in pending %} +

Title: {{claim.work.title }}

+

Author: {{claim.work.author }}

+

By: {{ claim.user.username }} +

On Behalf of: {{ claim.rights_holder.rights_holder_name }}

+

PSA #: {{ claim.rights_holder.id }}

+

Date of Claim : {{ claim.created }}

+

Status: {{ claim.status }}

+

Change to:

{{ claim_form.as_p }} + +{% endfor %} +
+{% endif %} +active:{{ active_data.count }} +{% if active_data.count %} +

Active Claims

+{% for claim in active_data %}

Title: {{claim.work.title }}

Author: {{claim.work.author }}

By: {{ claim.user.username }} @@ -29,5 +49,6 @@

Date of Claim : {{ claim.created }}

Status: {{ claim.status }}

{% endfor %} +{% endif %} {% endblock %} \ No newline at end of file diff --git a/frontend/views.py b/frontend/views.py index c828ec81..e78bcb73 100755 --- a/frontend/views.py +++ b/frontend/views.py @@ -7,6 +7,7 @@ from django.contrib.auth.models import User from django.core.urlresolvers import reverse from django.core.exceptions import ObjectDoesNotExist from django.forms import Select +from django.forms.models import modelformset_factory from django.http import HttpResponseRedirect from django.http import HttpResponse from django.views.decorators.csrf import csrf_exempt @@ -114,7 +115,7 @@ def claim(request): data = request.POST form = ClaimForm(data=data) if form.is_valid(): - if not models.Claim.objects.filter(work=data['work'], rights_holder=data['rights_holder']).count(): + if not models.Claim.objects.filter(work=data['work'], rights_holder=data['rights_holder'], status='pending').count(): form.save() return HttpResponseRedirect(reverse('work', kwargs={'work_id': data['work']})) else: @@ -126,19 +127,32 @@ def claim(request): def rh_admin(request): if not request.user.profile.is_admin: return render(request, "admins_only.html") + PendingFormSet = modelformset_factory(models.Claim, fields=['status'], extra=0) + pending_data = models.Claim.objects.filter(status = 'pending') + active_data = models.Claim.objects.filter(status = 'active') if request.method == 'POST': - form = RightsHolderForm(data=request.POST) - if form.is_valid(): - form.save() + if 'create_rights_holder' in request.POST.keys(): + form = RightsHolderForm(data=request.POST) + pending_formset = PendingFormSet (queryset=pending_data) + if form.is_valid(): + form.save() + if 'set_claim_status' in request.POST.keys(): + pending_formset = PendingFormSet (request.POST, request.FILES, queryset=pending_data) + form = RightsHolderForm() + if pending_formset.is_valid(): + pending_formset.save() else: form = RightsHolderForm() + pending_formset = PendingFormSet(queryset=pending_data) rights_holders = models.RightsHolder.objects.all() - pending = models.Claim.objects.filter(status = 'pending') + context = { 'request': request, 'rights_holders': rights_holders, 'form': form, - 'pending': pending, + 'pending': zip(pending_data,pending_formset), + 'pending_formset': pending_formset, + 'active_data': active_data, } return render(request, "rights_holders.html", context)