Commit 8fc360a9 authored by Skia's avatar Skia

Improve subscription and add it to admin app

parent f71ce2f7
from django.contrib import admin
# Register your models here.
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('ae', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='subscription',
name='subscription_type',
field=models.CharField(verbose_name='subscription type', max_length=255, choices=[('cursus-branche', 'Cursus Branche'), ('cursus-tronc-commun', 'Cursus Tronc Commun'), ('deux-semestres', 'Deux semestres'), ('un-semestre', 'Un semestre')]),
),
]
from django.contrib import admin from django.contrib import admin
from core.models import User, Page, Group from core.models import User, Page, Group
from django.contrib.auth.models import Group as AuthGroup
admin.site.register(User) admin.site.register(User)
admin.site.unregister(AuthGroup)
admin.site.register(Group) admin.site.register(Group)
admin.site.register(Page) admin.site.register(Page)
...@@ -38,7 +38,7 @@ INSTALLED_APPS = ( ...@@ -38,7 +38,7 @@ INSTALLED_APPS = (
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'core', 'core',
'ae', 'subscription',
'accounting', 'accounting',
) )
...@@ -151,19 +151,23 @@ AE_PAYMENT_METHOD = [('cheque', 'Chèque'), ...@@ -151,19 +151,23 @@ AE_PAYMENT_METHOD = [('cheque', 'Chèque'),
# Subscription durations are in semestres (should be settingized) # Subscription durations are in semestres (should be settingized)
AE_SUBSCRIPTIONS = { AE_SUBSCRIPTIONS = {
'Un semestre': { 'un-semestre': {
'name': 'Un semestre',
'price': 15, 'price': 15,
'duration': 1, 'duration': 1,
}, },
'Deux semestres': { 'deux-semestres': {
'name': 'Deux semestres',
'price': 28, 'price': 28,
'duration': 2, 'duration': 2,
}, },
'Cursus Tronc Commun': { 'cursus-tronc-commun': {
'name': 'Cursus Tronc Commun',
'price': 45, 'price': 45,
'duration': 4, 'duration': 4,
}, },
'Cursus Branche': { 'cursus-branche': {
'name': 'Cursus Branche',
'price': 45, 'price': 45,
'duration': 6, 'duration': 6,
}, },
......
...@@ -23,6 +23,6 @@ handler404 = "core.views.not_found" ...@@ -23,6 +23,6 @@ handler404 = "core.views.not_found"
urlpatterns = [ urlpatterns = [
url(r'^', include('core.urls', namespace="core", app_name="core")), url(r'^', include('core.urls', namespace="core", app_name="core")),
url(r'^ae/', include('ae.urls', namespace="ae", app_name="ae")), url(r'^subscription/', include('subscription.urls', namespace="asso", app_name="subscription")),
url(r'^admin/', include(admin.site.urls)), url(r'^admin/', include(admin.site.urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) # TODO: remove me for production!!! ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) # TODO: remove me for production!!!
from django.contrib import admin
from subscription.models import Member, Subscription
admin.site.register(Member)
admin.site.register(Subscription)
...@@ -8,25 +8,25 @@ from django.conf import settings ...@@ -8,25 +8,25 @@ from django.conf import settings
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
('core', '0002_auto_20151215_0827'), ('core', '0005_auto_20160128_0842'),
] ]
operations = [ operations = [
migrations.CreateModel( migrations.CreateModel(
name='Member', name='Member',
fields=[ fields=[
('user', models.OneToOneField(primary_key=True, to=settings.AUTH_USER_MODEL, serialize=False)), ('user', models.OneToOneField(to=settings.AUTH_USER_MODEL, primary_key=True, serialize=False)),
], ],
), ),
migrations.CreateModel( migrations.CreateModel(
name='Subscription', name='Subscription',
fields=[ fields=[
('id', models.AutoField(auto_created=True, serialize=False, primary_key=True, verbose_name='ID')), ('id', models.AutoField(auto_created=True, serialize=False, verbose_name='ID', primary_key=True)),
('subscription_type', models.CharField(choices=[('cursus branche', 'Cursus Branche'), ('cursus tronc commun', 'Cursus Tronc Commun'), ('deux semestres', 'Deux semestres'), ('un semestre', 'Un semestre')], verbose_name='subscription type', max_length=255)), ('subscription_type', models.CharField(choices=[('cursus-branche', 'Cursus Branche'), ('cursus-tronc-commun', 'Cursus Tronc Commun'), ('deux-semestres', 'Deux semestres'), ('un-semestre', 'Un semestre')], verbose_name='subscription type', max_length=255)),
('subscription_start', models.DateField(verbose_name='subscription start')), ('subscription_start', models.DateField(verbose_name='subscription start')),
('subscription_end', models.DateField(verbose_name='subscription end')), ('subscription_end', models.DateField(verbose_name='subscription end')),
('payment_method', models.CharField(choices=[('cheque', 'Chèque'), ('cash', 'Espèce'), ('other', 'Autre')], verbose_name='payment method', max_length=255)), ('payment_method', models.CharField(choices=[('cheque', 'Chèque'), ('cash', 'Espèce'), ('other', 'Autre')], verbose_name='payment method', max_length=255)),
('member', models.ForeignKey(related_name='subscriptions', to='ae.Member')), ('member', models.ForeignKey(to='subscription.Member', related_name='subscriptions')),
], ],
options={ options={
'ordering': ['subscription_start'], 'ordering': ['subscription_start'],
......
...@@ -20,18 +20,39 @@ class Member(models.Model): ...@@ -20,18 +20,39 @@ class Member(models.Model):
def is_subscribed(self): def is_subscribed(self):
return self.subscriptions.last().is_valid_now() return self.subscriptions.last().is_valid_now()
def __str__(self):
return self.user.username
class Subscription(models.Model): class Subscription(models.Model):
member = models.ForeignKey(Member, related_name='subscriptions') member = models.ForeignKey(Member, related_name='subscriptions')
subscription_type = models.CharField(_('subscription type'), subscription_type = models.CharField(_('subscription type'),
max_length=255, max_length=255,
choices=((k.lower().replace(' ', '-'), k) for k in sorted(settings.AE_SUBSCRIPTIONS.keys()))) choices=((k, v['name']) for k,v in sorted(settings.AE_SUBSCRIPTIONS.items())))
subscription_start = models.DateField(_('subscription start')) subscription_start = models.DateField(_('subscription start'))
subscription_end = models.DateField(_('subscription end')) subscription_end = models.DateField(_('subscription end'))
payment_method = models.CharField(_('payment method'), max_length=255, choices=settings.AE_PAYMENT_METHOD) payment_method = models.CharField(_('payment method'), max_length=255, choices=settings.AE_PAYMENT_METHOD)
def save(self, *args, **kwargs):
"""
This makes the Subscription to be updated with right dates with respect to date.today() each time you save the
Subscription object.
It means that you must be careful when modifying old Subscription, because you could make
someone that had no more valid subscription to get one again!
TODO: FIXME by putting it in the right function that would be triggered only when using the right Form!!!!
"""
self.subscription_start = self.compute_start()
self.subscription_end = self.compute_end(
duration=settings.AE_SUBSCRIPTIONS[self.subscription_type]['duration'],
start=self.subscription_start)
super(Subscription, self).save(*args, **kwargs)
class Meta: class Meta:
ordering = ['subscription_start',] ordering = ['subscription_start',]
def __str__(self):
return self.member.user.username+' - '+str(self.pk)
@staticmethod @staticmethod
def compute_start(d=date.today()): def compute_start(d=date.today()):
""" """
......
from django.conf.urls import url, include from django.conf.urls import url, include
from ae.views import * from subscription.views import *
urlpatterns = [ urlpatterns = [
# Subscription views # Subscription views
......
...@@ -4,7 +4,7 @@ from django import forms ...@@ -4,7 +4,7 @@ from django import forms
from django.forms import Select from django.forms import Select
from django.conf import settings from django.conf import settings
from ae.models import Member, Subscription from subscription.models import Member, Subscription
from core.views import CanEditMixin, CanEditPropMixin, CanViewMixin from core.views import CanEditMixin, CanEditPropMixin, CanViewMixin
class SubscriptionForm(forms.ModelForm): class SubscriptionForm(forms.ModelForm):
...@@ -17,5 +17,5 @@ class SubscriptionForm(forms.ModelForm): ...@@ -17,5 +17,5 @@ class SubscriptionForm(forms.ModelForm):
class NewSubscription(CanEditMixin, CreateView): class NewSubscription(CanEditMixin, CreateView):
template_name = 'ae/subscription.html' template_name = 'subscription/subscription.html'
form_class = SubscriptionForm form_class = SubscriptionForm
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment