From 5c3c56ecc3c38800878b48171095ad9f0dec4b2f Mon Sep 17 00:00:00 2001 From: Martin Bley Date: Thu, 23 Jun 2016 15:03:12 +0200 Subject: [PATCH] Implemented selection of mandants when posting blog items --- templates/newblogpost.html | 25 +++++++-------- tipp/forms.py | 4 +-- tipp/views.py | 64 ++++++++++++++++++++------------------ 3 files changed, 48 insertions(+), 45 deletions(-) diff --git a/templates/newblogpost.html b/templates/newblogpost.html index 3e66c21..4b45e87 100644 --- a/templates/newblogpost.html +++ b/templates/newblogpost.html @@ -22,26 +22,26 @@ {% endif %}
-
- {% csrf_token %} + {% csrf_token %}
{{form_bp.content.errors}} - +
- {% if has_choices %} - {{ form_mandants }} + {% if cnt_mandants > 1 %} + {{ form_mandants.mandants.label_tag }} + {% for choice in form_mandants.mandants %} + + {% endfor %} {% endif %}
- +
@@ -49,9 +49,8 @@
{% if debug %}
-
- - {{ debug }} +
+

{{ debug }}

{% endif %} diff --git a/tipp/forms.py b/tipp/forms.py index d3e4bb2..53f3368 100644 --- a/tipp/forms.py +++ b/tipp/forms.py @@ -50,8 +50,8 @@ class BlogpostForm(ModelForm): class MandantsForm(forms.Form): mandants = forms.MultipleChoiceField( - label= "Mandanten", - widget=forms.CheckboxSelectMultiple() + label= "Posten in", + widget=forms.CheckboxSelectMultiple({'checked':'checked'}) ) class UserForm(forms.ModelForm): diff --git a/tipp/views.py b/tipp/views.py index 590f39c..61e7b81 100644 --- a/tipp/views.py +++ b/tipp/views.py @@ -405,8 +405,9 @@ def matchday(request, ls, season, matchday, template_name='md.html'): matches.append(item) # get the newest blogposts + pm = RelPostMandant.objects.filter(mandant__in=mandants).values_list('post', flat=True) posts = [] - for post in Post.objects.filter(published=True)[:1]: + for post in Post.objects.filter(id__in=pm, published=True)[:1]: try: avatar = UserProfile.objects.get(user_id=post.author_id).avatar.name except: @@ -486,7 +487,6 @@ def charts(request, ls, season, pos='default', template_name='charts.html'): def blogindex(request, page): # get the blog posts that are published # and related to mandant - mandants = RelUserMandant.objects.filter(user=request.user).values_list('mandant', flat=True) posts = RelPostMandant.objects.filter(mandant__in=mandants).values_list('post', flat=True) @@ -503,13 +503,10 @@ def blogindex(request, page): try: posts = p.page(page) except PageNotAnInteger: - # If page is not an integer, deliver first page. posts = p.page(1) except EmptyPage: - # If page is out of range (e.g. 9999), deliver last page of results. posts = p.page(p.num_pages) - # now return the rendered template return render(request, 'blogindex.html', { 'posts': posts, 'ls': get_current_ls(), @@ -520,41 +517,48 @@ def blogindex(request, page): @login_required def newBlogpost(request): debug = '' - """ - if this is a POST request we need to process the form data - """ - # create form instance and populate it with data from the request - if request == 'POST': + + has_mandant = False + mandants = RelUserMandant.objects.filter(user=request.user).values_list('mandant', flat=True) + choices = [] + if mandants.count > 1: + has_mandant = True + for m in Mandant.objects.filter(id__in=mandants): + choices.append( (m.name, m.description) ) + + if request.method == 'POST': form_bp = BlogpostForm(request.POST) - form_mandant = MandantsForm(request.POST) + if has_mandant is True: + form_mandant = MandantsForm(request.POST) + form_mandant.fields["mandants"].choices = choices - # check whether it's valid: - if form_bp.is_valid() and form_mandant.is_valid(): - data = form.cleaned_data - #p = Post(author_id=request.user.id, content=data['content']) - #p.save() + if form_bp.is_valid(): + data = form_bp.cleaned_data + p = Post(author_id=request.user.id, content=data['content']) + p.save() + + if has_mandant is True and form_mandant.is_valid(): + data = form_mandant.cleaned_data + for m in data['mandants']: + debug += str(m) + " " + m = RelPostMandant(post=p, mandant=Mandant.objects.get(name=m)) + m.save() + else: + m = RelPostMandant(post=p, mandant=Mandant.objects.get(id=mandants[0])) + m.save() - #for m in data['mandants']: - # pass - #debug += str(m) + " " - #mandants = RelUserMandant.objects.filter(user=request.user).values_list('mandant', flat=True) - - """ - for m in mandants: - r = RelPostMandant(mandant=Mandant.objects.get(id=m), post=p) - r.save() - """ - # ... - # redirect to a new URL: return redirect( '/blog/1') blogpostForm = BlogpostForm() - form_mandant = MandantsForm(user=request.user) + form_mandant = MandantsForm() + form_mandant.fields["mandants"].choices = choices + cnt_mandants = len(choices) return render(request, 'newblogpost.html', { 'debug': debug, 'form_bp': blogpostForm, - 'form_mandants': form_mandant.as_table(), + 'form_mandants': form_mandant, + 'cnt_mandants' : cnt_mandants, 'ls': get_current_ls(), 'season': get_current_season() })