mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-09 19:40:19 +00:00
Big refactor: remove Subscriber class
This commit is contained in:
@ -1,8 +1,7 @@
|
||||
from django.contrib import admin
|
||||
|
||||
from subscription.models import Subscriber, Subscription
|
||||
from subscription.models import Subscription
|
||||
|
||||
|
||||
|
||||
admin.site.register(Subscriber)
|
||||
admin.site.register(Subscription)
|
||||
|
@ -26,21 +26,9 @@ class Migration(migrations.Migration):
|
||||
'ordering': ['subscription_start'],
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Subscriber',
|
||||
fields=[
|
||||
],
|
||||
options={
|
||||
'proxy': True,
|
||||
},
|
||||
bases=('core.user',),
|
||||
managers=[
|
||||
('objects', django.contrib.auth.models.UserManager()),
|
||||
],
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='subscription',
|
||||
name='member',
|
||||
field=models.ForeignKey(to='subscription.Subscriber', related_name='subscriptions'),
|
||||
field=models.ForeignKey(to='core.User', related_name='subscriptions'),
|
||||
),
|
||||
]
|
||||
|
@ -19,36 +19,8 @@ def validate_payment(value):
|
||||
if value not in settings.SITH_SUBSCRIPTION_PAYMENT_METHOD:
|
||||
raise ValidationError(_('Bad payment method'))
|
||||
|
||||
class Subscriber(User):
|
||||
class Meta:
|
||||
proxy = True
|
||||
|
||||
def is_subscribed(self):
|
||||
s = self.subscriptions.last()
|
||||
return s.is_valid_now() if s is not None else False
|
||||
|
||||
def save(self):
|
||||
create = False
|
||||
if not self.id:
|
||||
create = True
|
||||
super(Subscriber, self).save()
|
||||
if create and settings.IS_OLD_MYSQL_PRESENT:
|
||||
try: # Create user on the old site: TODO remove me!
|
||||
import MySQLdb
|
||||
db = MySQLdb.connect(**settings.OLD_MYSQL_INFOS)
|
||||
c = db.cursor()
|
||||
c.execute("""INSERT INTO utilisateurs (id_utilisateur, nom_utl, prenom_utl, email_utl, hash_utl, ae_utl) VALUES
|
||||
(%s, %s, %s, %s, %s, %s)""", (self.id, self.last_name, self.first_name, self.email, "valid", "1"))
|
||||
db.commit()
|
||||
except Exception as e:
|
||||
with open(settings.BASE_DIR+"/user_fail.log", "a") as f:
|
||||
print("FAIL to add user %s (%s %s - %s) to old site" % (self.id, self.first_name.encode('utf-8'),
|
||||
self.last_name.encode('utf-8'), self.email), file=f)
|
||||
print("Reason: %s" % (repr(e)), file=f)
|
||||
db.rollback()
|
||||
|
||||
class Subscription(models.Model):
|
||||
member = models.ForeignKey(Subscriber, related_name='subscriptions')
|
||||
member = models.ForeignKey(User, related_name='subscriptions')
|
||||
subscription_type = models.CharField(_('subscription type'),
|
||||
max_length=255,
|
||||
choices=((k, v['name']) for k,v in sorted(settings.SITH_SUBSCRIPTIONS.items())))
|
||||
|
@ -11,14 +11,10 @@ from django.conf import settings
|
||||
from ajax_select.fields import AutoCompleteSelectField
|
||||
import random
|
||||
|
||||
from subscription.models import Subscriber, Subscription
|
||||
from subscription.models import Subscription
|
||||
from core.views import CanEditMixin, CanEditPropMixin, CanViewMixin
|
||||
from core.models import User
|
||||
|
||||
def get_subscriber(user):
|
||||
s = Subscriber.objects.filter(pk=user.pk).first()
|
||||
return s
|
||||
|
||||
class SubscriptionForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Subscription
|
||||
@ -38,7 +34,7 @@ class SubscriptionForm(forms.ModelForm):
|
||||
def clean_member(self):
|
||||
subscriber = self.cleaned_data.get("member")
|
||||
if subscriber:
|
||||
subscriber = Subscriber.objects.filter(id=subscriber.id).first()
|
||||
subscriber = User.objects.filter(id=subscriber.id).first()
|
||||
return subscriber
|
||||
|
||||
def clean(self):
|
||||
@ -50,10 +46,10 @@ class SubscriptionForm(forms.ModelForm):
|
||||
self.errors.pop("member", None)
|
||||
if self.errors:
|
||||
return cleaned_data
|
||||
if Subscriber.objects.filter(email=cleaned_data.get("email")).first() is not None:
|
||||
if User.objects.filter(email=cleaned_data.get("email")).first() is not None:
|
||||
self.add_error("email", ValidationError(_("A user with that email address already exists")))
|
||||
else:
|
||||
u = Subscriber(last_name = self.cleaned_data.get("last_name"),
|
||||
u = User(last_name = self.cleaned_data.get("last_name"),
|
||||
first_name = self.cleaned_data.get("first_name"),
|
||||
email = self.cleaned_data.get("email"))
|
||||
u.generate_username()
|
||||
|
Reference in New Issue
Block a user