kpsul/gestion/shared.py
2017-06-23 00:10:38 +01:00

43 lines
1 KiB
Python

"""
Locking/unlocking tools to prevent tables to be corrupted
"""
from django.db import connection
from django.contrib.auth.models import User
from django.conf import settings
def get_or_create_clipper(clipper, fullname=None):
user, created = User.objects.get_or_create(username=clipper)
if created:
user.email = settings.CAS_EMAIL_FORMAT % (clipper, )
if fullname:
bits = fullname.split(" ")
user.first_name = bits[0]
if len(bits) > 1:
user.last_name = " ".join(bits[1:])
user.save()
return (user, created)
def lock_table(*models):
query = "LOCK TABLES "
for i, model in enumerate(models):
table = model._meta.db_table
if i > 0:
query += ", "
query += "%s WRITE" % table
cursor = connection.cursor()
cursor.execute(query)
row = cursor.fetchone()
return row
def unlock_tables(*models):
cursor = connection.cursor()
cursor.execute("UNLOCK TABLES")
row = cursor.fetchone()
return row
unlock_table = unlock_tables