diff --git a/news/admin.py b/news/admin.py index a645e37c..6b6adc73 100755 --- a/news/admin.py +++ b/news/admin.py @@ -1,19 +1,34 @@ +# coding: utf-8 + from django.contrib import admin from django import forms -# Register your models here. from ckeditor.widgets import CKEditorWidget -from .models import News + +from . import models class NewsAdminForm(forms.ModelForm): class Meta: - model = News + model = models.NewsEntry fields = '__all__' + text = forms.CharField(widget=CKEditorWidget()) +@admin.register(models.NewsEntry) class NewsAdmin(admin.ModelAdmin): form = NewsAdminForm - readonly_fields = ('slug',) -admin.site.register(News, NewsAdmin) + list_display = ( + 'title', + 'subtitle', + 'slug', + 'creation_date', + 'facility', + 'organization' + ) + list_filter = ( + 'facility', + 'organization' + ) + readonly_fields = ('slug',) diff --git a/news/migrations/0002_rename_news_model.py b/news/migrations/0002_rename_news_model.py new file mode 100644 index 00000000..74928b46 --- /dev/null +++ b/news/migrations/0002_rename_news_model.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('organizations', '0007_auto_20151023_2129'), + ('news', '0001_initial'), + ] + + operations = [ + migrations.RenameModel( + old_name='News', + new_name='NewsEntry' + ), + migrations.AlterModelOptions( + name='newsentry', + options={'ordering': ('facility', 'organization', 'creation_date'), + 'verbose_name': 'news entry', + 'verbose_name_plural': 'news entries'}, + ), + migrations.AlterField( + model_name='newsentry', + name='facility', + field=models.ForeignKey(related_name='news_entries', blank=True, + to='organizations.Facility', null=True), + ), + migrations.AlterField( + model_name='newsentry', + name='organization', + field=models.ForeignKey(related_name='news_entries', blank=True, + to='organizations.Organization', null=True), + ), + migrations.AlterField( + model_name='newsentry', + name='text', + field=models.TextField(verbose_name='articletext'), + ), + ] diff --git a/news/models.py b/news/models.py index 35143600..2df66be9 100755 --- a/news/models.py +++ b/news/models.py @@ -5,29 +5,46 @@ from django.template.defaultfilters import slugify -class News(models.Model): +class NewsEntry(models.Model): """ facilities and organizations can publish news. TODO: News are shown in appropriate organization templates """ + title = models.CharField(max_length=255, + verbose_name=_("title")) + + subtitle = models.CharField(max_length=255, + verbose_name=_("subtitle"), + null=True, + blank=True) + + text = models.TextField(verbose_name=_("articletext")) + + slug = models.SlugField(auto_created=True, max_length=255) + creation_date = models.DateField(auto_now=True, verbose_name=_("creation date")) - title = models.CharField(max_length=255, verbose_name=_("title")) - subtitle = models.CharField(max_length=255, verbose_name=_("subtitle"), - null=True, blank=True) - text = models.TextField(max_length=20055, verbose_name=_("articletext")) - slug = models.SlugField(auto_created=True, max_length=255) - facility = models.ForeignKey('organizations.Facility', null=True, + + facility = models.ForeignKey('organizations.Facility', + related_name='news_entries', + null=True, blank=True) - organization = models.ForeignKey('organizations.Organization', null=True, + + organization = models.ForeignKey('organizations.Organization', + related_name='news_entries', + null=True, blank=True) + class Meta: + verbose_name = _('news entry') + verbose_name_plural = _('news entries') + ordering = ('facility', 'organization', 'creation_date') + def save(self, *args, **kwargs): if not self.id: # Newly created object, so set slug self.slug = slugify(self.title) - - super(News, self).save(*args, **kwargs) + super(NewsEntry, self).save(*args, **kwargs) def __unicode__(self): return u'{}'.format(self.title) diff --git a/organizations/migrations/0007_auto_20151023_2129.py b/organizations/migrations/0007_auto_20151023_2129.py new file mode 100644 index 00000000..832db1a8 --- /dev/null +++ b/organizations/migrations/0007_auto_20151023_2129.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('organizations', '0006_auto_20151022_1445'), + ] + + operations = [ + migrations.AlterField( + model_name='facility', + name='contact_info', + field=models.TextField(verbose_name='contact info'), + ), + migrations.AlterField( + model_name='organization', + name='contact_info', + field=models.TextField(verbose_name='contact info'), + ), + ] diff --git a/scheduler/views.py b/scheduler/views.py index fa5af9f5..fc2de0d5 100644 --- a/scheduler/views.py +++ b/scheduler/views.py @@ -1,25 +1,28 @@ # coding: utf-8 -from datetime import date, datetime +from datetime import date import logging import json import itertools -from time import mktime -from django.core.serializers.json import DjangoJSONEncoder +from django.core.serializers.json import DjangoJSONEncoder from django.core.urlresolvers import reverse from django.contrib import messages from django.db.models import Count + from django.templatetags.l10n import localize + from django.utils.safestring import mark_safe + from django.views.generic import TemplateView, FormView, DetailView + from django.shortcuts import get_object_or_404 from django.utils.translation import ugettext_lazy as _ from accounts.models import UserAccount +from news.models import NewsEntry from organizations.models import Facility -from news.models import News from scheduler.models import Shift from google_tools.templatetags.google_links import google_maps_directions from scheduler.models import ShiftHelper @@ -48,26 +51,18 @@ def get_open_shifts(): return shifts -def getNewsFacility(facility): - news_query = News.objects.filter(facility=facility) - news = [] - if news_query: - - for item in news_query: - news.append({ - 'title':item.title, - 'date':item.creation_date, - 'text':item.text - }) - return news - - class HelpDesk(LoginRequiredMixin, TemplateView): """ Facility overview. First view that a volunteer gets redirected to when they log in. """ template_name = "helpdesk.html" + @staticmethod + def serialize_news(news_entries): + return [dict(title=news_entry.title, + date=news_entry.creation_date, + text=news_entry.text) for news_entry in news_entries] + def get_context_data(self, **kwargs): context = super(HelpDesk, self).get_context_data(**kwargs) open_shifts = get_open_shifts() @@ -85,7 +80,7 @@ def get_context_data(self, **kwargs): used_places.add(facility.place.area) facility_list.append({ 'name': facility.name, - 'news': getNewsFacility(facility), + 'news': self.serialize_news(NewsEntry.objects.filter(facility=facility)), 'address_line': address_line, 'google_maps_link': google_maps_directions( address_line) if address_line else None, @@ -105,7 +100,8 @@ def get_context_data(self, **kwargs): context['areas_json'] = json.dumps( [{'slug': area.slug, 'name': area.name} for area in sorted(used_places, key=lambda p: p.name)]) - context['facility_json'] = json.dumps(facility_list, cls=DjangoJSONEncoder) + context['facility_json'] = json.dumps(facility_list, + cls=DjangoJSONEncoder) context['shifts'] = open_shifts return context