2015-12-15 16:50:50 +00:00
|
|
|
from django.shortcuts import render
|
|
|
|
from django.views.generic.edit import UpdateView, CreateView
|
2016-02-05 15:59:42 +00:00
|
|
|
from django.views.generic.base import View
|
2016-07-06 22:52:30 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from django.core.exceptions import PermissionDenied, ValidationError
|
|
|
|
from django.db import IntegrityError
|
2015-12-15 16:50:50 +00:00
|
|
|
from django import forms
|
|
|
|
from django.forms import Select
|
|
|
|
from django.conf import settings
|
|
|
|
|
2016-08-19 21:24:23 +00:00
|
|
|
from ajax_select.fields import AutoCompleteSelectField
|
2016-08-31 16:40:17 +00:00
|
|
|
import random
|
2016-08-19 21:24:23 +00:00
|
|
|
|
2016-12-10 00:58:30 +00:00
|
|
|
from subscription.models import Subscription
|
2015-12-15 16:50:50 +00:00
|
|
|
from core.views import CanEditMixin, CanEditPropMixin, CanViewMixin
|
2016-02-05 15:59:42 +00:00
|
|
|
from core.models import User
|
|
|
|
|
2015-12-15 16:50:50 +00:00
|
|
|
class SubscriptionForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Subscription
|
2016-08-04 22:50:42 +00:00
|
|
|
fields = ['member', 'subscription_type', 'payment_method', 'location']
|
2016-08-19 21:24:23 +00:00
|
|
|
member = AutoCompleteSelectField('users', required=False, help_text=None)
|
2015-12-15 16:50:50 +00:00
|
|
|
|
2016-07-06 22:52:30 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(SubscriptionForm, self).__init__(*args, **kwargs)
|
|
|
|
# Add fields to allow basic user creation
|
|
|
|
self.fields['last_name'] = forms.CharField(max_length=User._meta.get_field('last_name').max_length)
|
|
|
|
self.fields['first_name'] = forms.CharField(max_length=User._meta.get_field('first_name').max_length)
|
|
|
|
self.fields['email'] = forms.EmailField()
|
|
|
|
self.fields.move_to_end('subscription_type')
|
|
|
|
self.fields.move_to_end('payment_method')
|
2016-08-04 22:50:42 +00:00
|
|
|
self.fields.move_to_end('location')
|
2016-07-06 22:52:30 +00:00
|
|
|
|
2016-08-19 21:24:23 +00:00
|
|
|
def clean_member(self):
|
|
|
|
subscriber = self.cleaned_data.get("member")
|
|
|
|
if subscriber:
|
2016-12-10 00:58:30 +00:00
|
|
|
subscriber = User.objects.filter(id=subscriber.id).first()
|
2016-08-19 21:24:23 +00:00
|
|
|
return subscriber
|
|
|
|
|
2016-07-06 22:52:30 +00:00
|
|
|
def clean(self):
|
|
|
|
cleaned_data = super(SubscriptionForm, self).clean()
|
|
|
|
if (cleaned_data.get("member") is None
|
|
|
|
and "last_name" not in self.errors.as_data()
|
|
|
|
and "first_name" not in self.errors.as_data()
|
|
|
|
and "email" not in self.errors.as_data()):
|
|
|
|
self.errors.pop("member", None)
|
2016-09-02 10:29:50 +00:00
|
|
|
if self.errors:
|
|
|
|
return cleaned_data
|
2016-12-10 00:58:30 +00:00
|
|
|
if User.objects.filter(email=cleaned_data.get("email")).first() is not None:
|
2016-07-06 22:52:30 +00:00
|
|
|
self.add_error("email", ValidationError(_("A user with that email address already exists")))
|
|
|
|
else:
|
2016-12-10 00:58:30 +00:00
|
|
|
u = User(last_name = self.cleaned_data.get("last_name"),
|
2016-07-06 22:52:30 +00:00
|
|
|
first_name = self.cleaned_data.get("first_name"),
|
|
|
|
email = self.cleaned_data.get("email"))
|
|
|
|
u.generate_username()
|
2016-08-31 16:40:17 +00:00
|
|
|
u.set_password(str(random.randrange(1000000, 10000000)))
|
2016-07-06 22:52:30 +00:00
|
|
|
u.save()
|
|
|
|
cleaned_data["member"] = u
|
|
|
|
elif cleaned_data.get("member") is not None:
|
|
|
|
self.errors.pop("last_name", None)
|
|
|
|
self.errors.pop("first_name", None)
|
|
|
|
self.errors.pop("email", None)
|
|
|
|
if cleaned_data.get("member") is None:
|
|
|
|
# This should be handled here, but it is done in the Subscription model's clean method
|
|
|
|
# TODO investigate why!
|
|
|
|
raise ValidationError(_("You must either choose an existing user or create a new one properly"))
|
|
|
|
return cleaned_data
|
|
|
|
|
2016-08-29 17:48:29 +00:00
|
|
|
class NewSubscription(CreateView):
|
2016-03-22 10:42:00 +00:00
|
|
|
template_name = 'subscription/subscription.jinja'
|
2015-12-15 16:50:50 +00:00
|
|
|
form_class = SubscriptionForm
|
2016-03-22 10:42:00 +00:00
|
|
|
|
2016-08-29 17:48:29 +00:00
|
|
|
def dispatch(self, request, *arg, **kwargs):
|
|
|
|
res = super(NewSubscription, self).dispatch(request, *arg, **kwargs)
|
|
|
|
if request.user.is_in_group(settings.SITH_MAIN_BOARD_GROUP):
|
|
|
|
return res
|
|
|
|
raise PermissionDenied
|
|
|
|
|
2016-03-22 10:42:00 +00:00
|
|
|
def get_initial(self):
|
|
|
|
if 'member' in self.request.GET.keys():
|
2016-08-14 17:28:14 +00:00
|
|
|
return {'member': self.request.GET['member'], 'subscription_type': 'deux-semestres'}
|
|
|
|
return {'subscription_type': 'deux-semestres'}
|
2016-07-05 23:32:34 +00:00
|
|
|
|
|
|
|
def form_valid(self, form):
|
2016-07-28 10:41:29 +00:00
|
|
|
form.instance.subscription_start = Subscription.compute_start(
|
|
|
|
duration=settings.SITH_SUBSCRIPTIONS[form.instance.subscription_type]['duration'])
|
2016-07-05 23:32:34 +00:00
|
|
|
form.instance.subscription_end = Subscription.compute_end(
|
|
|
|
duration=settings.SITH_SUBSCRIPTIONS[form.instance.subscription_type]['duration'],
|
|
|
|
start=form.instance.subscription_start
|
|
|
|
)
|
|
|
|
return super(NewSubscription, self).form_valid(form)
|
2016-07-06 22:52:30 +00:00
|
|
|
|