Implemented selection of mandants when posting blog items

This commit is contained in:
2016-06-23 15:03:12 +02:00
parent 6042838f28
commit 5c3c56ecc3
3 changed files with 48 additions and 45 deletions

View File

@@ -22,26 +22,26 @@
{% endif %}
<div class="row">
<div class="col-md-8">
<form class="form-horizontal" action="" method="post">
{% csrf_token %}
<form class="form-horizontal" action="" method="post">{% csrf_token %}
<div class="form-group">
{{form_bp.content.errors}}
<label for="id_content" class="col-sm-4 control-label required">
{% trans 'Content' %}
</label>
<label for="id_content" class="col-sm-4 control-label required">{% trans 'Content' %}</label>
<div class="col-sm-5">
<textarea class="form-control" maxlength="256" rows="2"
id="id_content" name="content"></textarea>
{% if has_choices %}
{{ form_mandants }}
{% if cnt_mandants > 1 %}
{{ form_mandants.mandants.label_tag }}
{% for choice in form_mandants.mandants %}
<label for="{{choice.id_for_label}}">
{{ choice.tag }} {{choice.choice_label}}
</label>
{% endfor %}
{% endif %}
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">
{% trans 'Sent' %}
</button>
<button type="submit" class="btn btn-default">{% trans 'Sent' %}</button>
</div>
</div>
</form>
@@ -49,9 +49,8 @@
</div>
{% if debug %}
<div class="row">
<div class="col-md-5">
<h2 class="form-signin-heading">{% trans 'Debug' %}</h2>
{{ debug }}
<div class="col-md-8">
<p>{{ debug }}</p>
</div>
</div>
{% endif %}

View File

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

View File

@@ -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)
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()
#for m in data['mandants']:
# pass
#debug += str(m) + " "
#mandants = RelUserMandant.objects.filter(user=request.user).values_list('mandant', flat=True)
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 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()
})