Implemented selection of mandants when posting blog items
This commit is contained in:
@@ -22,26 +22,26 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-md-8">
|
<div class="col-md-8">
|
||||||
<form class="form-horizontal" action="" method="post">
|
<form class="form-horizontal" action="" method="post">{% csrf_token %}
|
||||||
{% csrf_token %}
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
{{form_bp.content.errors}}
|
{{form_bp.content.errors}}
|
||||||
<label for="id_content" class="col-sm-4 control-label required">
|
<label for="id_content" class="col-sm-4 control-label required">{% trans 'Content' %}</label>
|
||||||
{% trans 'Content' %}
|
|
||||||
</label>
|
|
||||||
<div class="col-sm-5">
|
<div class="col-sm-5">
|
||||||
<textarea class="form-control" maxlength="256" rows="2"
|
<textarea class="form-control" maxlength="256" rows="2"
|
||||||
id="id_content" name="content"></textarea>
|
id="id_content" name="content"></textarea>
|
||||||
{% if has_choices %}
|
{% if cnt_mandants > 1 %}
|
||||||
{{ form_mandants }}
|
{{ 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 %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<div class="col-sm-offset-2 col-sm-10">
|
<div class="col-sm-offset-2 col-sm-10">
|
||||||
<button type="submit" class="btn btn-default">
|
<button type="submit" class="btn btn-default">{% trans 'Sent' %}</button>
|
||||||
{% trans 'Sent' %}
|
|
||||||
</button>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
@@ -49,9 +49,8 @@
|
|||||||
</div>
|
</div>
|
||||||
{% if debug %}
|
{% if debug %}
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-md-5">
|
<div class="col-md-8">
|
||||||
<h2 class="form-signin-heading">{% trans 'Debug' %}</h2>
|
<p>{{ debug }}</p>
|
||||||
{{ debug }}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|||||||
@@ -50,8 +50,8 @@ class BlogpostForm(ModelForm):
|
|||||||
|
|
||||||
class MandantsForm(forms.Form):
|
class MandantsForm(forms.Form):
|
||||||
mandants = forms.MultipleChoiceField(
|
mandants = forms.MultipleChoiceField(
|
||||||
label= "Mandanten",
|
label= "Posten in",
|
||||||
widget=forms.CheckboxSelectMultiple()
|
widget=forms.CheckboxSelectMultiple({'checked':'checked'})
|
||||||
)
|
)
|
||||||
|
|
||||||
class UserForm(forms.ModelForm):
|
class UserForm(forms.ModelForm):
|
||||||
|
|||||||
@@ -405,8 +405,9 @@ def matchday(request, ls, season, matchday, template_name='md.html'):
|
|||||||
matches.append(item)
|
matches.append(item)
|
||||||
|
|
||||||
# get the newest blogposts
|
# get the newest blogposts
|
||||||
|
pm = RelPostMandant.objects.filter(mandant__in=mandants).values_list('post', flat=True)
|
||||||
posts = []
|
posts = []
|
||||||
for post in Post.objects.filter(published=True)[:1]:
|
for post in Post.objects.filter(id__in=pm, published=True)[:1]:
|
||||||
try:
|
try:
|
||||||
avatar = UserProfile.objects.get(user_id=post.author_id).avatar.name
|
avatar = UserProfile.objects.get(user_id=post.author_id).avatar.name
|
||||||
except:
|
except:
|
||||||
@@ -486,7 +487,6 @@ def charts(request, ls, season, pos='default', template_name='charts.html'):
|
|||||||
def blogindex(request, page):
|
def blogindex(request, page):
|
||||||
# get the blog posts that are published
|
# get the blog posts that are published
|
||||||
# and related to mandant
|
# and related to mandant
|
||||||
|
|
||||||
mandants = RelUserMandant.objects.filter(user=request.user).values_list('mandant', flat=True)
|
mandants = RelUserMandant.objects.filter(user=request.user).values_list('mandant', flat=True)
|
||||||
posts = RelPostMandant.objects.filter(mandant__in=mandants).values_list('post', flat=True)
|
posts = RelPostMandant.objects.filter(mandant__in=mandants).values_list('post', flat=True)
|
||||||
|
|
||||||
@@ -503,13 +503,10 @@ def blogindex(request, page):
|
|||||||
try:
|
try:
|
||||||
posts = p.page(page)
|
posts = p.page(page)
|
||||||
except PageNotAnInteger:
|
except PageNotAnInteger:
|
||||||
# If page is not an integer, deliver first page.
|
|
||||||
posts = p.page(1)
|
posts = p.page(1)
|
||||||
except EmptyPage:
|
except EmptyPage:
|
||||||
# If page is out of range (e.g. 9999), deliver last page of results.
|
|
||||||
posts = p.page(p.num_pages)
|
posts = p.page(p.num_pages)
|
||||||
|
|
||||||
# now return the rendered template
|
|
||||||
return render(request, 'blogindex.html', {
|
return render(request, 'blogindex.html', {
|
||||||
'posts': posts,
|
'posts': posts,
|
||||||
'ls': get_current_ls(),
|
'ls': get_current_ls(),
|
||||||
@@ -520,41 +517,48 @@ def blogindex(request, page):
|
|||||||
@login_required
|
@login_required
|
||||||
def newBlogpost(request):
|
def newBlogpost(request):
|
||||||
debug = ''
|
debug = ''
|
||||||
"""
|
|
||||||
if this is a POST request we need to process the form data
|
has_mandant = False
|
||||||
"""
|
mandants = RelUserMandant.objects.filter(user=request.user).values_list('mandant', flat=True)
|
||||||
# create form instance and populate it with data from the request
|
choices = []
|
||||||
if request == 'POST':
|
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_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():
|
||||||
if form_bp.is_valid() and form_mandant.is_valid():
|
data = form_bp.cleaned_data
|
||||||
data = form.cleaned_data
|
p = Post(author_id=request.user.id, content=data['content'])
|
||||||
#p = Post(author_id=request.user.id, content=data['content'])
|
p.save()
|
||||||
#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')
|
return redirect( '/blog/1')
|
||||||
|
|
||||||
blogpostForm = BlogpostForm()
|
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', {
|
return render(request, 'newblogpost.html', {
|
||||||
'debug': debug,
|
'debug': debug,
|
||||||
'form_bp': blogpostForm,
|
'form_bp': blogpostForm,
|
||||||
'form_mandants': form_mandant.as_table(),
|
'form_mandants': form_mandant,
|
||||||
|
'cnt_mandants' : cnt_mandants,
|
||||||
'ls': get_current_ls(),
|
'ls': get_current_ls(),
|
||||||
'season': get_current_season()
|
'season': get_current_season()
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user