Skip to content

Commit

Permalink
Refactor news and make admin more pretty
Browse files Browse the repository at this point in the history
  • Loading branch information
christophmeissner committed Oct 23, 2015
1 parent e3acfd5 commit d944295
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 35 deletions.
25 changes: 20 additions & 5 deletions news/admin.py
Original file line number Diff line number Diff line change
@@ -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',)
42 changes: 42 additions & 0 deletions news/migrations/0002_rename_news_model.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
37 changes: 27 additions & 10 deletions news/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
24 changes: 24 additions & 0 deletions organizations/migrations/0007_auto_20151023_2129.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
36 changes: 16 additions & 20 deletions scheduler/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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,
Expand All @@ -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

Expand Down

0 comments on commit d944295

Please sign in to comment.