Repository: dotkom/onlineweb4

Issue:
Quickfix for posters ordered by committee
_Ref mail from agnete 21.10.2015._



Discussion:

Resolving patch:
diff --git a/apps/posters/dashboard/views.py b/apps/posters/dashboard/views.py
--- a/apps/posters/dashboard/views.py
+++ b/apps/posters/dashboard/views.py
@@ -7,38 +7,26 @@
 from django.contrib.auth.decorators import login_required
 from django.contrib.auth.models import Group
 from django.core.mail import EmailMessage
-from django.core.exceptions import PermissionDenied, ObjectDoesNotExist
-from django.core.urlresolvers import reverse
-from django.forms.models import model_to_dict
+from django.core.exceptions import PermissionDenied
 from django.shortcuts import render, redirect, get_object_or_404, HttpResponseRedirect, HttpResponse
-from django.utils import timezone
 from django.utils.translation import ugettext as _
 from django.views.decorators.csrf import ensure_csrf_cookie
 
 from guardian.decorators import permission_required
 from guardian.models import UserObjectPermission, GroupObjectPermission
-# from guardian.core import ObjectPermissionChecker
-import guardian
 from pytz import timezone as tz
 
-from datetime import datetime, timedelta
-
 from apps.authentication.models import OnlineUser as User
-from apps.dashboard.tools import has_access, get_base_context
-from apps.posters.models import Poster
+from apps.dashboard.tools import get_base_context
 from apps.posters.forms import AddForm, AddPosterForm, AddBongForm, AddOtherForm, EditPosterForm
-# from apps.dashboard.posters.models import PosterForm
 from apps.companyprofile.models import Company
-from apps.posters.models import Poster, OrderMixin
+from apps.posters.models import Poster
 from apps.posters.permissions import has_view_perms, has_view_all_perms, has_edit_perms
 
 
-@ensure_csrf_cookie
 @login_required
 @permission_required('posters.overview_poster_order', return_403=True)
 def index(request):
-    if request.is_ajax():
-        do_ajax_shit = True
 
     # The group with members who should populate the dropdownlist
     group = Group.objects.get(name='proKom')
@@ -48,7 +36,8 @@ def index(request):
 
     # View to show if user not in committee, but wanting to see own orders
     if not has_view_all_perms(request.user):
-        context['your_orders'] = [x for x in Poster.objects.filter(ordered_by=request.user) if request.user.has_perm('view_poster_order', x)]
+        context['your_orders'] = [x for x in Poster.objects.filter(ordered_by=request.user)
+                                  if request.user.has_perm('view_poster_order', x)]
         return render(request, 'posters/dashboard/index.html', context)
 
     orders = Poster.objects.all()
@@ -57,12 +46,11 @@ def index(request):
     context['active_orders'] = orders.filter(finished=False).exclude(assigned_to=None)
     context['old_orders'] = orders.filter(finished=True)
 
-    context['workers'] = User.objects.filter(groups=Group.objects.get(name='proKom'))
+    context['workers'] = users_to_populate
 
     return render(request, 'posters/dashboard/index.html', context)
 
 
-@ensure_csrf_cookie
 @login_required
 @permission_required('posters.add_poster_order', return_403=True)
 def add(request, order_type=0):
@@ -88,17 +76,14 @@ def add(request, order_type=0):
             if request.POST.get('company'):
                 poster.company = Company.objects.get(pk=request.POST.get('company'))
             poster.ordered_by = request.user
-            # Should look for a more kosher solution
-            poster.ordered_committee = request.user.groups.exclude(name='Komiteer').filter(name__contains="Kom")[0]
             poster.order_type = order_type
 
             poster.save()
 
-            # for b in poster.__class__.__bases__:
-            # poster_mixin = OrderMixin.objects.get(id=poster.id)
             # Let this user have permissions to show this order
             UserObjectPermission.objects.assign_perm('view_poster_order', obj=poster, user=request.user)
-            GroupObjectPermission.objects.assign_perm('view_poster_order', obj=poster, group=Group.objects.get(name='proKom'))
+            GroupObjectPermission.objects.assign_perm('view_poster_order',
+                                                      obj=poster, group=Group.objects.get(name='proKom'))
 
             title = unicode(poster)
 
@@ -148,11 +133,12 @@ def add(request, order_type=0):
             return redirect(poster.get_absolute_url())
         else:
             context['form'] = form
+            context['form'].fields['ordered_committee'].queryset = request.user.groups.all()
             return render(request, 'posters/dashboard/add.html', context)
 
     context["order_type_name"] = type_name
     context['order_type'] = order_type
-    context['can_edit'] = True  # request.user.has_perm('posters.view_poster')
+    context['can_edit'] = request.user.has_perm('posters.view_poster')
 
     if order_type == 1:
         form = AddPosterForm()
@@ -164,6 +150,7 @@ def add(request, order_type=0):
     forms = (AddPosterForm(), AddBongForm(), AddOtherForm())
 
     context['form'] = forms[order_type-1]
+    context['form'].fields['ordered_committee'].queryset = request.user.groups.all()
 
     return render(request, 'posters/dashboard/add.html', context)
 
@@ -184,7 +171,6 @@ def edit(request, order_id=None):
         form = AddForm(request.POST, instance=poster)
         if form.is_valid():
             form.save()
-            # redirect_url = redirect(poster.get_absolute_url())
             return HttpResponseRedirect("../detail/"+str(poster.id))
 
     else:
@@ -197,8 +183,6 @@ def edit(request, order_id=None):
 @login_required
 @permission_required('view_poster_order', (Poster, 'pk', 'order_id'), return_403=True)
 def detail(request, order_id=None):
-    if request.is_ajax():
-        do_ajax_shit = True
 
     if not order_id:
         return HttpResponse(status=400)
@@ -228,7 +212,6 @@ def detail(request, order_id=None):
 # Ajax
 
 
-# @ensure_csrf_cookie
 @login_required
 def assign_person(request):
     if request.is_ajax():
diff --git a/apps/posters/forms.py b/apps/posters/forms.py
--- a/apps/posters/forms.py
+++ b/apps/posters/forms.py
@@ -2,9 +2,10 @@
 from datetime import datetime
 
 from django import forms
+from django.contrib.auth.models import Group
 
-from apps.posters.models import Poster
 from apps.events.models import Event
+from apps.posters.models import Poster
 
 
 class AddForm(forms.ModelForm):
@@ -15,11 +16,11 @@ class AddForm(forms.ModelForm):
     price = forms.IntegerField(label='Pris', required=False, widget=forms.NumberInput(attrs={'placeholder': 'Pris på event'}))
     amount = forms.IntegerField(label='Antall', required=False, widget=forms.NumberInput(attrs={'placeholder': 'Hvor mange vil du ha?', "value" : "10"}))
     display_from = forms.DateField(label=u"Vis plakat fra", required=False, widget=forms.TextInput(attrs={'type': 'date', 'placeholder': 'YYYY-MM-DD'}))
+    ordered_committee = forms.ModelChoiceField(queryset=Group.objects.all(), empty_label=None)
 
     class Meta:
         model = Poster
-        fields = ['amount', 'description',
-                  'price', 'comments']
+        fields = ['ordered_committee', 'amount', 'description', 'price', 'comments']
 
 
 class AddPosterForm(AddForm):
@@ -29,7 +30,7 @@ class AddPosterForm(AddForm):
 
     class Meta:
         model = Poster
-        fields = ['event', 'amount', 'bong', 'description',
+        fields = ['ordered_committee', 'event', 'amount', 'bong', 'description',
                   'price', 'display_from', 'comments']
 
 
@@ -42,7 +43,7 @@ class AddOtherForm(AddForm):
 
     class Meta:
         model = Poster
-        fields = ['title', 'amount', 'price', 'description',
+        fields = ['ordered_committee', 'title', 'amount', 'price', 'description',
                   'display_from', 'comments']
 
 


Test patch: