Splitting serializers. M2M Changes

I took the serializers and put them in their own file.
I made the due dates be attached to the project  with a foreign key, rather than M2M, better admin inline.
This commit is contained in:
Tyrel Souza 2015-03-10 00:14:17 -04:00
parent 8bb12212e5
commit 588825332f
10 changed files with 140 additions and 6 deletions

View File

@ -1,3 +1,20 @@
from django.contrib import admin from django.contrib import admin
from orderable.admin import OrderableAdmin
from project.models import Project, DueDate, Category
# Register your models here. # Register your models here.
class DueDateInline(admin.TabularInline):
model = DueDate
class CategoryClass(OrderableAdmin):
list_display = ('__unicode__', 'sort_order_display')
class ProjectAdmin(admin.ModelAdmin):
list_display = ('title','description','status')
fields = ['title','description','status']
inlines = (DueDateInline,)
admin.site.register(Project, ProjectAdmin)
admin.site.register(DueDate)
admin.site.register(Category, CategoryClass)

1
project/api/__init__.py Normal file
View File

@ -0,0 +1 @@
__author__ = 'tyrel'

View File

@ -0,0 +1,18 @@
__author__ = 'tyrel'
from rest_framework import serializers
from project.models import Project, Category, DueDate
class ProjectSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Project
class CategorySerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Category
class DueDateSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = DueDate

19
project/api/viewsets.py Normal file
View File

@ -0,0 +1,19 @@
__author__ = 'tyrel'
from rest_framework import viewsets
from project.models import Project, Category, DueDate
from project.api.serializers import ProjectSerializer, CategorySerializer, DueDateSerializer
class ProjectViewSet(viewsets.ModelViewSet):
queryset = Project.objects.all()
serializer_class = ProjectSerializer
class CategoryViewSet(viewsets.ModelViewSet):
queryset = Category.objects.all()
serializer_class = CategorySerializer
class DueDateViewSet(viewsets.ModelViewSet):
queryset = DueDate.objects.all()
serializer_class = DueDateSerializer

13
project/helpers.py Normal file
View File

@ -0,0 +1,13 @@
__author__ = 'tyrel'
import datetime
from django.conf import settings
def get_status(title, due_date):
overdue = ""
message = title
if due_date:
if due_date < datetime.date.today():
overdue = " Overdue"
message += " ({}{})".format(due_date.strftime(settings.DATE_FMT), overdue)
return message

View File

@ -1,3 +1,52 @@
from django.db import models from django.db import models
import datetime
from django.conf import settings
from orderable.models import Orderable
# Create your models here. STATUSES = (
(0, 'Upcoming'),
(1, 'Current'),
(2, 'Backlog'),
(3, 'Completed'),
)
class Project(models.Model):
title = models.CharField(max_length=256)
description = models.TextField(blank=True, null=True)
status = models.IntegerField(choices=STATUSES, default=0)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Category(Orderable):
title = models.CharField(max_length=256)
class Meta(Orderable.Meta):
verbose_name_plural = "Categories"
def __unicode__(self):
return self.title
class DueDate(models.Model):
title = models.ForeignKey("Category")
due = models.DateField(blank=True, null=True)
completed = models.BooleanField(default=False)
project = models.ForeignKey("Project")
def completed_overdue_date(self):
if self.completed:
return "Completed"
if not self.due:
return ""
if self.due > datetime.date.today():
return "Overdue {}".format(self.due.strftime(settings.DATE_FMT))
return self.due.strftime(settings.DATE_FMT)
class Meta:
verbose_name = "Due Date"
verbose_name_plural = "Due Dates"
def __unicode__(self):
return unicode(self.title)

View File

@ -3,3 +3,4 @@ mysqlclient
djangorestframework djangorestframework
markdown markdown
django-filter django-filter
django-orderable

View File

@ -15,9 +15,19 @@ INSTALLED_APPS = (
'django.contrib.contenttypes', 'django.contrib.contenttypes',
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles')
'rest_framework'
APPS = (
'project',
) )
INSTALLED_APPS += APPS
THIRD_PARTY_APPS = (
'rest_framework',
'orderable'
)
INSTALLED_APPS += THIRD_PARTY_APPS
MIDDLEWARE_CLASSES = ( MIDDLEWARE_CLASSES = (
@ -47,7 +57,7 @@ DATABASES = {
} }
LANGUAGE_CODE = 'en-us' LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'EST' TIME_ZONE = 'America/New_York'
USE_I18N = True USE_I18N = True
USE_L10N = True USE_L10N = True
USE_TZ = True USE_TZ = True
@ -62,3 +72,5 @@ TEMPLATE_DIRS = (
) )
from drf_settings import * from drf_settings import *
DATE_FMT = "%a %m/%d"

View File

@ -2,9 +2,13 @@ from django.conf.urls import patterns, include, url
from django.contrib import admin from django.contrib import admin
from rest_framework import routers from rest_framework import routers
from user_api_views import UserViewSet from user_api_views import UserViewSet
from project.api.viewsets import ProjectViewSet, CategoryViewSet, DueDateViewSet
router = routers.DefaultRouter() router = routers.DefaultRouter()
router.register(r'users', UserViewSet) router.register(r'users', UserViewSet)
router.register(r'projects', ProjectViewSet)
router.register(r'categories', CategoryViewSet)
router.register(r'due_dates', DueDateViewSet)
urlpatterns = patterns('', urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)), url(r'^admin/', include(admin.site.urls)),