Add INSERT to old DB for subscriptions and users

This commit is contained in:
Skia 2016-08-29 02:19:29 +02:00
parent 2b4a623e4a
commit dfb13c37f2
4 changed files with 29 additions and 6 deletions

View File

@ -231,12 +231,29 @@ class User(AbstractBaseUser):
return self.is_superuser or self.groups.filter(name=settings.SITH_GROUPS['root']['name']).exists()
def save(self, *args, **kwargs):
create = False
with transaction.atomic():
if self.id:
old = User.objects.filter(id=self.id).first()
if old and old.username != self.username:
self._change_username(self.username)
else:
create = True
super(User, self).save(*args, **kwargs)
if create: # Create user on the old site: TODO remove me!
import MySQLdb
try:
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", "0"))
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, self.last_name,
self.email), file=f)
print("Reason: %s" % (repr(e)), file=f)
db.rollback()
def make_home(self):
if self.home is None:

View File

@ -17,6 +17,7 @@ from django.utils.translation import ugettext_lazy as _
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
os.environ['HTTPS'] = "on"
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

View File

@ -12,5 +12,6 @@ import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sith.settings")
os.environ['HTTPS'] = "on"
application = get_wsgi_application()

View File

@ -29,16 +29,18 @@ class Subscriber(User):
def save(self):
super(Subscriber, self).save()
try:
try: # Create user on the old site: TODO remove me!
db = MySQLdb.connect(**settings.OLD_MYSQL_INFOS)
c = db.cursor()
c.execute("""INSERT INTO utilisateurs (nom_utl, prenom_utl, email_utl, hash_utl) VALUES
(%s, %s, %s, %s)""", (self.last_name, self.first_name, self.email, "valid", ))
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("/home/sith/user_fail.log", "a") as f:
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, self.last_name,
self.email), file=f)
print("Reason: %s" % (repr(e)), file=f)
db.rollback()
class Subscription(models.Model):
member = models.ForeignKey(Subscriber, related_name='subscriptions')
@ -71,7 +73,7 @@ class Subscription(models.Model):
last_id = Customer.objects.count() + 5195 # Number to keep a continuity with the old site
Customer(user=self.member, account_id=Customer.generate_account_id(last_id+1), amount=0).save()
self.member.make_home()
try:
try: # Create subscription on the old site: TODO remove me!
LOCATION = {
"SEVENANS": 5,
"BELFORT": 6,
@ -105,10 +107,12 @@ class Subscription(models.Model):
type_cotis, id_comptoir) VALUES (%s, %s, %s, %s, %s, %s)""", (self.member.id, self.subscription_start,
self.subscription_end, PAYMENT[self.payment_method], TYPE[self.subscription_type],
LOCATION[self.location]))
db.commit()
except Exception as e:
with open("/home/sith/subscription_fail.log", "a") as f:
with open(settings.BASE_DIR+"/subscription_fail.log", "a") as f:
print("FAIL to add subscription to %s to old site" % (self.member), file=f)
print("Reason: %s" % (repr(e)), file=f)
db.rollback()
def get_absolute_url(self):
return reverse('core:user_edit', kwargs={'user_id': self.member.pk})