Implemented mandant based blog posts
This commit is contained in:
@@ -18,7 +18,7 @@ You should have received a copy of the GNU General Public License
|
||||
along with TipPy. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
"""
|
||||
from tipp.models import Team, Match, Tipp, Score, Mandant, RelUserMandant, Competition, Post, UserProfile
|
||||
from tipp.models import *
|
||||
from django.contrib import admin
|
||||
|
||||
class PostAdmin(admin.ModelAdmin):
|
||||
@@ -41,4 +41,5 @@ admin.site.register(Score)
|
||||
admin.site.register(RelUserMandant)
|
||||
admin.site.register(Competition)
|
||||
admin.site.register(Post, PostAdmin)
|
||||
admin.site.register(RelPostMandant)
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ from django import forms
|
||||
from django.forms import ModelForm
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.files.images import get_image_dimensions
|
||||
from tipp.models import Match, Competition, Post, UserProfile
|
||||
from tipp.models import Match, Competition, Post, UserProfile, Mandant, RelUserMandant
|
||||
|
||||
class NumberInput(forms.TextInput):
|
||||
input_type = 'number'
|
||||
@@ -48,6 +48,13 @@ class BlogpostForm(ModelForm):
|
||||
model = Post
|
||||
fields = ['content']
|
||||
|
||||
class MandantsForm(forms.Form):
|
||||
mandants = forms.MultipleChoiceField(
|
||||
label= "Mandanten",
|
||||
widget=forms.CheckboxSelectMultiple()
|
||||
)
|
||||
|
||||
|
||||
class UserForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = User
|
||||
|
||||
@@ -123,7 +123,6 @@ class Post(models.Model):
|
||||
published = models.BooleanField(default=True)
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
author = models.ForeignKey(User)
|
||||
mandant = models.ForeignKey(Mandant)
|
||||
|
||||
class Meta:
|
||||
ordering = ['-created']
|
||||
@@ -131,4 +130,12 @@ class Post(models.Model):
|
||||
def __unicode__(self):
|
||||
return u'%s' % self.created
|
||||
|
||||
class RelPostMandant(models.Model):
|
||||
post = models.ForeignKey(Post)
|
||||
mandant = models.ForeignKey(Mandant)
|
||||
|
||||
class Meta:
|
||||
unique_together = ("post", "mandant")
|
||||
def __unicode__(self):
|
||||
return (str(self.post) + " -> " + str(self.mandant))
|
||||
|
||||
|
||||
@@ -72,7 +72,6 @@ def profile(request, pk):
|
||||
debug = []
|
||||
debug.append('avatar: ' + str(img))
|
||||
|
||||
# If it's a HTTP POST, we're interested in processing form data.
|
||||
if request.method == 'POST':
|
||||
user_form = UserForm(data=request.POST, instance=user)
|
||||
profile_form = UserProfileForm(data=request.POST, instance=profile)
|
||||
@@ -84,10 +83,8 @@ def profile(request, pk):
|
||||
profile = profile_form.save(commit=False)
|
||||
profile.user = user
|
||||
|
||||
# Did the user provide a profile picture?
|
||||
if 'avatar' in request.FILES:
|
||||
profile.avatar = request.FILES['avatar']
|
||||
# Now we save the UserProfile model instance.
|
||||
profile.save()
|
||||
|
||||
# create thumbnail
|
||||
@@ -106,16 +103,11 @@ def profile(request, pk):
|
||||
for item in request.POST:
|
||||
debug.append(str(item) + " -> " + str(request.POST[item]))
|
||||
|
||||
# Invalid form or forms - mistakes or something else?
|
||||
# Print problems to the terminal.
|
||||
# They'll also be shown to the user.
|
||||
else:
|
||||
print user_form.errors, profile_form.errors
|
||||
|
||||
return redirect( '/accounts/profile/' + str(user.id) )
|
||||
|
||||
# Not a HTTP POST, so we render our form using two ModelForm instances.
|
||||
# These forms will be blank, ready for user input.
|
||||
else:
|
||||
user_form = UserForm(
|
||||
initial={ 'last_name': user.last_name,
|
||||
@@ -398,8 +390,9 @@ def matchday(request, ls, season, matchday, template_name='md.html'):
|
||||
matches.append(item)
|
||||
|
||||
# get the newest blogposts
|
||||
pIDs = 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(published=True, id__in=pIDs)[:1]:
|
||||
try:
|
||||
avatar = UserProfile.objects.get(user_id=post.author_id).avatar.name
|
||||
except:
|
||||
@@ -478,8 +471,13 @@ def charts(request, ls, season, pos='default', template_name='charts.html'):
|
||||
@login_required
|
||||
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)
|
||||
|
||||
post_list = []
|
||||
for post in Post.objects.filter(published=True):
|
||||
for post in Post.objects.filter(published=True, id__in=posts):
|
||||
try:
|
||||
avatar = UserProfile.objects.get(user_id=post.author_id).avatar.name
|
||||
except:
|
||||
@@ -507,52 +505,62 @@ def blogindex(request, page):
|
||||
|
||||
@login_required
|
||||
def newBlogpost(request):
|
||||
"""
|
||||
if this is a POST request we need to process the form data
|
||||
"""
|
||||
if request.method == 'POST':
|
||||
# create form instance and populate it with data from the request
|
||||
form = BlogpostForm(request.POST)
|
||||
# check whether it's valid:
|
||||
if form.is_valid():
|
||||
data = form.cleaned_data
|
||||
p = Post(author_id=request.user.id, content=data['content'])
|
||||
debug = ''
|
||||
ms = Mandant.objects.filter(
|
||||
id__in=RelUserMandant.objects.filter(user=request.user).values_list('mandant', flat=True)
|
||||
)
|
||||
choices = []
|
||||
for m in ms:
|
||||
choices.append((m.name, m.description))
|
||||
|
||||
has_choices = False
|
||||
if ms.count() > 1:
|
||||
has_choices = True
|
||||
|
||||
if request.method == "POST":
|
||||
form_bp = BlogpostForm(request.POST)
|
||||
|
||||
if form_bp.is_valid():
|
||||
p = Post(author_id=request.user.id, content=form_bp.cleaned_data['content'])
|
||||
p.save()
|
||||
|
||||
# ...
|
||||
# redirect to a new URL:
|
||||
return redirect( '/blog/1')
|
||||
if has_choices is True:
|
||||
form_mandant = MandantsForm(request.POST)
|
||||
form_mandant.fields["mandants"].choices = choices
|
||||
|
||||
# if a GET (or any other method) we'll create a blank form
|
||||
else:
|
||||
blogpostForm = BlogpostForm()
|
||||
return render(request, 'newblogpost.html', {
|
||||
'form': blogpostForm,
|
||||
'ls': get_current_ls(),
|
||||
'season': get_current_season()
|
||||
})
|
||||
if form_mandant.is_valid():
|
||||
for m in form_mandant.cleaned_data['mandants']:
|
||||
r = RelPostMandant(mandant=Mandant.objects.get(name=m), post=p)
|
||||
r.save()
|
||||
else:
|
||||
r = RelPostMandant(mandant=Mandant.objects.get(name=ms[0]), post=p)
|
||||
r.save()
|
||||
|
||||
return redirect( '/blog/1')
|
||||
|
||||
blogpostForm = BlogpostForm()
|
||||
form_mandant = MandantsForm()
|
||||
form_mandant.fields["mandants"].choices = choices
|
||||
|
||||
return render(request, 'newblogpost.html', {
|
||||
#'debug': debug,
|
||||
'form_bp': blogpostForm,
|
||||
'form_mandants': form_mandant.as_table(),
|
||||
'has_choices': has_choices,
|
||||
'ls': get_current_ls(),
|
||||
'season': get_current_season()
|
||||
})
|
||||
|
||||
def logout(request):
|
||||
"""
|
||||
Log users out and re-direct them to the main page.
|
||||
"""
|
||||
logout(request)
|
||||
return HttpResponseRedirect('/')
|
||||
|
||||
def get_current_md(ls):
|
||||
"""
|
||||
get current matchday()
|
||||
returns dict with keys
|
||||
['groupName']
|
||||
['groupOrderID']
|
||||
['groupID']
|
||||
"""
|
||||
|
||||
try:
|
||||
ol = OpenLiga()
|
||||
cur_md = ol.getCurrentGroup(ls)
|
||||
except:
|
||||
cur_md = {'groupName': u"1. Spieltag", 'groupOrderID': 1}
|
||||
cur_md = 1
|
||||
|
||||
return(cur_md['groupOrderID'])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user