Format subscription

This commit is contained in:
Pierre Brunet 2017-06-12 10:10:42 +02:00
parent cd46e099b6
commit 113c9e696b
4 changed files with 65 additions and 67 deletions

View File

@ -26,6 +26,4 @@ from django.contrib import admin
from subscription.models import Subscription
admin.site.register(Subscription)

View File

@ -35,15 +35,16 @@ from core.models import User
from core.utils import get_start_of_semester
def validate_type(value):
if value not in settings.SITH_SUBSCRIPTIONS.keys():
raise ValidationError(_('Bad subscription type'))
def validate_payment(value):
if value not in settings.SITH_SUBSCRIPTION_PAYMENT_METHOD:
raise ValidationError(_('Bad payment method'))
class Subscription(models.Model):
member = models.ForeignKey(User, related_name='subscriptions')
subscription_type = models.CharField(_('subscription type'),
@ -134,7 +135,6 @@ class Subscription(models.Model):
else:
return 'No user - ' + str(self.pk)
@staticmethod
def compute_start(d=date.today(), duration=1):
"""
@ -171,17 +171,21 @@ class Subscription(models.Model):
return start.replace(day=1, month=(start.month + 6 * duration) % 12 + 1,
year=start.year + int(duration / 2) + (1 if start.month > 6 and duration % 2 == 1 else 0))
def can_be_edited_by(self, user):
return user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) or user.is_root
def is_valid_now(self):
return self.subscription_start <= date.today() and date.today() <= self.subscription_end
def guy_test(date, duration=4):
print(str(date) + " - " + str(duration) + " -> " + str(Subscription.compute_start(date, duration)))
def bibou_test(duration, date=date.today()):
print(str(date) + " - " + str(duration) + " -> " + str(Subscription.compute_end(duration, Subscription.compute_start(date, duration))))
def guy():
guy_test(date(2015, 7, 11))
guy_test(date(2015, 8, 11))
@ -219,5 +223,6 @@ def guy():
bibou_test(3)
bibou_test(4)
if __name__ == "__main__":
guy()

View File

@ -31,6 +31,3 @@ urlpatterns = [
url(r'^$', NewSubscription.as_view(), name='subscription'),
url(r'stats', SubscriptionsStatsView.as_view(), name='stats'),
]

View File

@ -26,16 +26,13 @@ from django.views.generic.edit import CreateView, FormView
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import PermissionDenied, ValidationError
from django.core.urlresolvers import reverse_lazy
from django.db import IntegrityError
from django import forms
from django.forms import Select
from django.conf import settings
from ajax_select.fields import AutoCompleteSelectField
import random
from subscription.models import Subscription
from core.views import CanEditMixin, CanEditPropMixin, CanViewMixin
from core.views.forms import SelectDateTime
from core.models import User
@ -102,6 +99,7 @@ class SubscriptionForm(forms.ModelForm):
raise ValidationError(_("You must either choose an existing user or create a new one properly"))
return cleaned_data
class NewSubscription(CreateView):
template_name = 'subscription/subscription.jinja'
form_class = SubscriptionForm