2016-12-23 17:40:12 +00:00
|
|
|
from django.shortcuts import render, redirect
|
2016-12-23 02:02:46 +00:00
|
|
|
from django.views.generic import ListView, DetailView, RedirectView
|
|
|
|
from django.views.generic.edit import UpdateView, CreateView
|
2016-12-23 17:40:12 +00:00
|
|
|
from django.views.generic.detail import SingleObjectMixin
|
2016-12-23 02:17:38 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2016-12-21 01:38:21 +00:00
|
|
|
from django.core.urlresolvers import reverse, reverse_lazy
|
2016-12-23 02:02:46 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.utils import timezone
|
2016-12-23 19:11:27 +00:00
|
|
|
from django.conf import settings
|
2016-12-23 02:02:46 +00:00
|
|
|
from django import forms
|
2016-12-21 01:38:21 +00:00
|
|
|
|
2016-12-23 02:02:46 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
|
|
|
|
from com.models import Sith, News, NewsDate
|
2016-12-23 17:40:12 +00:00
|
|
|
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, TabedViewMixin, CanCreateMixin
|
2016-12-23 02:02:46 +00:00
|
|
|
from core.views.forms import SelectDateTime
|
2016-12-23 19:11:27 +00:00
|
|
|
from core.models import Notification, RealGroup
|
2016-12-23 02:02:46 +00:00
|
|
|
from club.models import Club
|
|
|
|
|
2016-12-21 01:38:21 +00:00
|
|
|
|
2016-12-23 02:02:46 +00:00
|
|
|
# Sith object
|
2016-12-21 01:38:21 +00:00
|
|
|
|
|
|
|
sith = Sith.objects.first
|
|
|
|
|
|
|
|
class ComTabsMixin(TabedViewMixin):
|
|
|
|
def get_tabs_title(self):
|
|
|
|
return _("Communication administration")
|
|
|
|
|
|
|
|
def get_list_of_tabs(self):
|
|
|
|
tab_list = []
|
|
|
|
tab_list.append({
|
|
|
|
'url': reverse('com:index_edit'),
|
|
|
|
'slug': 'index',
|
|
|
|
'name': _("Index page"),
|
|
|
|
})
|
|
|
|
tab_list.append({
|
|
|
|
'url': reverse('com:info_edit'),
|
|
|
|
'slug': 'info',
|
|
|
|
'name': _("Info message"),
|
|
|
|
})
|
|
|
|
tab_list.append({
|
|
|
|
'url': reverse('com:alert_edit'),
|
|
|
|
'slug': 'alert',
|
|
|
|
'name': _("Alert message"),
|
|
|
|
})
|
|
|
|
return tab_list
|
|
|
|
|
|
|
|
class ComEditView(ComTabsMixin, CanEditPropMixin, UpdateView):
|
|
|
|
model = Sith
|
|
|
|
template_name = 'core/edit.jinja'
|
|
|
|
|
|
|
|
def get_object(self, queryset=None):
|
|
|
|
return Sith.objects.first()
|
|
|
|
|
|
|
|
class AlertMsgEditView(ComEditView):
|
|
|
|
fields = ['alert_msg']
|
|
|
|
current_tab = "alert"
|
|
|
|
success_url = reverse_lazy('com:alert_edit')
|
|
|
|
|
|
|
|
class InfoMsgEditView(ComEditView):
|
|
|
|
fields = ['info_msg']
|
|
|
|
current_tab = "info"
|
|
|
|
success_url = reverse_lazy('com:info_edit')
|
|
|
|
|
|
|
|
class IndexEditView(ComEditView):
|
|
|
|
fields = ['index_page']
|
|
|
|
current_tab = "index"
|
|
|
|
success_url = reverse_lazy('com:index_edit')
|
2016-12-23 02:02:46 +00:00
|
|
|
|
|
|
|
# News
|
|
|
|
|
|
|
|
class NewsForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = News
|
2016-12-23 17:40:12 +00:00
|
|
|
fields = ['type', 'title', 'club', 'summary', 'content', 'author']
|
2016-12-23 02:02:46 +00:00
|
|
|
widgets = {
|
2016-12-23 17:40:12 +00:00
|
|
|
'author': forms.HiddenInput,
|
2016-12-23 02:02:46 +00:00
|
|
|
'type': forms.RadioSelect,
|
|
|
|
}
|
|
|
|
start_date = forms.DateTimeField(['%Y-%m-%d %H:%M:%S'], label=_("Start date"), widget=SelectDateTime, required=False)
|
|
|
|
end_date = forms.DateTimeField(['%Y-%m-%d %H:%M:%S'], label=_("End date"), widget=SelectDateTime, required=False)
|
|
|
|
until = forms.DateTimeField(['%Y-%m-%d %H:%M:%S'], label=_("Until"), widget=SelectDateTime, required=False)
|
2016-12-23 17:40:12 +00:00
|
|
|
automoderation = forms.BooleanField(label=_("Automoderation"), required=False)
|
2016-12-23 02:02:46 +00:00
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
self.cleaned_data = super(NewsForm, self).clean()
|
|
|
|
if self.cleaned_data['type'] != "NOTICE":
|
|
|
|
if not self.cleaned_data['start_date']:
|
|
|
|
self.add_error('start_date', ValidationError(_("This field is required.")))
|
|
|
|
if not self.cleaned_data['end_date']:
|
|
|
|
self.add_error('end_date', ValidationError(_("This field is required.")))
|
|
|
|
if self.cleaned_data['type'] == "WEEKLY" and not self.cleaned_data['until']:
|
|
|
|
self.add_error('until', ValidationError(_("This field is required.")))
|
|
|
|
return self.cleaned_data
|
|
|
|
|
|
|
|
def save(self):
|
|
|
|
ret = super(NewsForm, self).save()
|
|
|
|
self.instance.dates.all().delete()
|
|
|
|
if self.instance.type == "EVENT" or self.instance.type == "CALL":
|
|
|
|
NewsDate(start_date=self.cleaned_data['start_date'],
|
|
|
|
end_date=self.cleaned_data['end_date'],
|
|
|
|
news=self.instance).save()
|
|
|
|
elif self.instance.type == "WEEKLY":
|
|
|
|
start_date = self.cleaned_data['start_date']
|
|
|
|
end_date = self.cleaned_data['end_date']
|
|
|
|
while start_date <= self.cleaned_data['until']:
|
|
|
|
NewsDate(start_date=start_date,
|
|
|
|
end_date=end_date,
|
|
|
|
news=self.instance).save()
|
|
|
|
start_date += timedelta(days=7)
|
|
|
|
end_date += timedelta(days=7)
|
|
|
|
return ret
|
|
|
|
|
2016-12-23 17:40:12 +00:00
|
|
|
class NewsEditView(CanEditMixin, UpdateView):
|
2016-12-23 02:02:46 +00:00
|
|
|
model = News
|
|
|
|
form_class = NewsForm
|
|
|
|
template_name = 'com/news_edit.jinja'
|
|
|
|
pk_url_kwarg = 'news_id'
|
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
init = {}
|
|
|
|
try:
|
|
|
|
init['start_date'] = self.object.dates.order_by('id').first().start_date.strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
except: pass
|
|
|
|
try:
|
|
|
|
init['end_date'] = self.object.dates.order_by('id').first().end_date.strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
except: pass
|
|
|
|
return init
|
|
|
|
|
2016-12-23 18:43:59 +00:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
form = self.get_form()
|
|
|
|
if form.is_valid() and not 'preview' in request.POST.keys():
|
|
|
|
return self.form_valid(form)
|
|
|
|
else:
|
|
|
|
return self.form_invalid(form)
|
|
|
|
|
2016-12-23 17:40:12 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
self.object = form.save()
|
|
|
|
if form.cleaned_data['automoderation'] and self.request.user.is_in_group(settings.SITH_GROUP_COM_ADMIN_ID):
|
|
|
|
self.object.moderator = self.request.user
|
|
|
|
self.object.is_moderated = True
|
|
|
|
self.object.save()
|
|
|
|
else:
|
|
|
|
self.object.is_moderated = False
|
|
|
|
self.object.save()
|
2016-12-23 19:11:27 +00:00
|
|
|
for u in RealGroup.objects.filter(id=settings.SITH_GROUP_COM_ADMIN_ID).first().users.all():
|
|
|
|
if not u.notifications.filter(type="NEWS_MODERATION", viewed=False).exists():
|
|
|
|
Notification(user=u, url=reverse("com:news_detail", kwargs={'news_id': self.object.id}), type="NEWS_MODERATION").save()
|
2016-12-23 17:40:12 +00:00
|
|
|
return super(NewsEditView, self).form_valid(form)
|
|
|
|
|
|
|
|
class NewsCreateView(CanCreateMixin, CreateView):
|
2016-12-23 02:02:46 +00:00
|
|
|
model = News
|
|
|
|
form_class = NewsForm
|
|
|
|
template_name = 'com/news_edit.jinja'
|
|
|
|
|
|
|
|
def get_initial(self):
|
2016-12-23 17:40:12 +00:00
|
|
|
init = {'author': self.request.user}
|
2016-12-23 02:02:46 +00:00
|
|
|
try:
|
|
|
|
init['club'] = Club.objects.filter(id=self.request.GET['club']).first()
|
|
|
|
except: pass
|
|
|
|
return init
|
|
|
|
|
2016-12-23 18:43:59 +00:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
form = self.get_form()
|
|
|
|
if form.is_valid() and not 'preview' in request.POST.keys():
|
|
|
|
return self.form_valid(form)
|
|
|
|
else:
|
|
|
|
self.object = form.instance
|
|
|
|
return self.form_invalid(form)
|
|
|
|
|
2016-12-23 17:40:12 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
self.object = form.save()
|
|
|
|
if form.cleaned_data['automoderation'] and self.request.user.is_in_group(settings.SITH_GROUP_COM_ADMIN_ID):
|
|
|
|
self.object.moderator = self.request.user
|
|
|
|
self.object.is_moderated = True
|
|
|
|
self.object.save()
|
2016-12-23 19:11:27 +00:00
|
|
|
else:
|
|
|
|
for u in RealGroup.objects.filter(id=settings.SITH_GROUP_COM_ADMIN_ID).first().users.all():
|
|
|
|
if not u.notifications.filter(type="NEWS_MODERATION", viewed=False).exists():
|
|
|
|
Notification(user=u, url=reverse("com:news_detail", kwargs={'news_id': self.object.id}), type="NEWS_MODERATION").save()
|
2016-12-23 17:40:12 +00:00
|
|
|
return super(NewsCreateView, self).form_valid(form)
|
|
|
|
|
|
|
|
class NewsModerateView(CanEditMixin, SingleObjectMixin):
|
|
|
|
model = News
|
|
|
|
pk_url_kwarg = 'news_id'
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.object = self.get_object()
|
|
|
|
self.object.is_moderated = True
|
|
|
|
self.object.moderator = request.user
|
|
|
|
self.object.save()
|
|
|
|
if 'next' in self.request.GET.keys():
|
|
|
|
return redirect(self.request.GET['next'])
|
|
|
|
return redirect('com:news_admin_list')
|
|
|
|
|
|
|
|
class NewsAdminListView(CanEditMixin, ListView):
|
2016-12-23 02:02:46 +00:00
|
|
|
model = News
|
|
|
|
template_name = 'com/news_admin_list.jinja'
|
2016-12-23 17:40:12 +00:00
|
|
|
queryset = News.objects.filter(dates__end_date__gte=timezone.now()).distinct().order_by('id')
|
2016-12-23 02:02:46 +00:00
|
|
|
|
2016-12-23 17:40:12 +00:00
|
|
|
class NewsListView(CanViewMixin, ListView):
|
2016-12-23 02:02:46 +00:00
|
|
|
model = News
|
|
|
|
template_name = 'com/news_list.jinja'
|
|
|
|
|
2016-12-23 17:40:12 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
kwargs = super(NewsListView, self).get_context_data(**kwargs)
|
|
|
|
kwargs['NewsDate'] = NewsDate
|
|
|
|
kwargs['timedelta'] = timedelta
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
class NewsDetailView(CanViewMixin, DetailView):
|
2016-12-23 02:02:46 +00:00
|
|
|
model = News
|
|
|
|
template_name = 'com/news_detail.jinja'
|
|
|
|
pk_url_kwarg = 'news_id'
|
2016-12-23 17:40:12 +00:00
|
|
|
|
|
|
|
|