Skip to content

Commit

Permalink
Merge pull request #232 from FJNR-inc/develop
Browse files Browse the repository at this point in the history
New release
  • Loading branch information
RignonNoel authored Oct 15, 2019
2 parents 7567023 + afe87a8 commit c62e957
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 67 deletions.
114 changes: 114 additions & 0 deletions blitz_api/management/commands/import_members.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import csv
import datetime
import random
import string

from io import StringIO
from colorama import Fore
from django.core.management import call_command
from tqdm import tqdm

from blitz_api.models import User
from django.core.management.base import BaseCommand, CommandError


class Command(BaseCommand):
help = 'Import members "from new_members.csv" file'

def add_arguments(self, parser):

# Optional arguments
parser.add_argument(
'--notify',
action='store_true',
dest='notify',
help='Notify new user with his credentials',
)

def handle(self, *args, **options):

notify = options['notify']

with open('new_members.csv') as csv_file:
csv_reader = csv.DictReader(csv_file)

with tqdm(list(csv_reader), unit=' users', desc='Import users ',
bar_format="{l_bar}%s{bar}%s{r_bar}" %
(Fore.GREEN, Fore.RESET)
) as pbar:

nb_user_created = 0
nb_user_updated = 0

for user_data in pbar:

try:

out = StringIO()

nb_users = User.objects.all().count()

letters_and_digits = \
string.ascii_letters + string.digits
password = ''.join(
random.choice(letters_and_digits)
for i in range(10))

birthdate = datetime.datetime.strptime(
user_data["birthdate"], '%d/%m/%Y')
birthdate = birthdate.strftime('%Y-%m-%d')

if notify:

call_command(
'create_member',
f'--first_name={user_data["first_name"]}',
f'--last_name={user_data["last_name"]}',
f'--birthdate={birthdate}',
f'--gender={user_data["first_name"]}',
f'--university={user_data["university"]}',
f'--academic_level='
f'{user_data["academic_level"]}',
f'--academic_field='
f'{user_data["academic_field"]}',
f'--email={user_data["email"]}',
f'--password={password}',
f'--membership={user_data["membership"]}',
'--notify',
stdout=out
)
else:

call_command(
'create_member',
f'--first_name={user_data["first_name"]}',
f'--last_name={user_data["last_name"]}',
f'--birthdate={birthdate}',
f'--gender={user_data["first_name"]}',
f'--university={user_data["university"]}',
f'--academic_level='
f'{user_data["academic_level"]}',
f'--academic_field='
f'{user_data["academic_field"]}',
f'--email={user_data["email"]}',
f'--password={password}',
# No security on password
f'--membership={user_data["membership"]}',
stdout=out)

if User.objects.all().count() - nb_users == 0:
nb_user_updated += 1
else:
nb_user_created += 1

except CommandError as e:
self.stdout.write(
self.style.ERROR(f'{e}'))

self.stdout.write(
self.style.SUCCESS(
f'Successfully created {nb_user_created} users'))

self.stdout.write(
self.style.SUCCESS(
f'Successfully updated {nb_user_updated} users'))
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ def add_arguments(self, parser):
help='Notify new user with his credentials',
)

# Optional arguments
parser.add_argument(
'--check_email',
action='store_true',
dest='check_email',
help='Check email new user',
)

def handle(self, *args, **options):
with transaction.atomic():
try:
Expand All @@ -44,60 +52,37 @@ def handle(self, *args, **options):
raise CommandError(
'Email "%s" is not a valid email' % options['email'])

if User.objects.filter(email__iexact=options['email']):
raise CommandError(
'A user already exists with the email {0}'.format(
options['email'],
)
)
user = User.objects.filter(email__iexact=options['email'])

user = User.objects.create(
first_name=' '.join(options['first_name']),
last_name=' '.join(options['last_name']),
birthdate=options['birthdate'],
gender=options['gender'],
username=options['email'],
email=options['email'],
is_active=True,
tickets=1,
)
user.set_password(options['password'])

try:
university = Organization.objects.get(pk=options['university'])
user.university = university
user.save()
except Organization.DoesNotExist:
raise CommandError(
'Organization "%s" does not exist' % options['university'])

try:
academic_field = AcademicField.objects.get(
pk=options['academic_field'])
user.academic_field = academic_field
user.save()
except AcademicField.DoesNotExist:
raise CommandError('AcademicField "%s" does not exist' %
options['academic_field'])
if len(user) == 0:
if options['check_email']:
raise CommandError(
'A user already exists with the email {0}'.format(
options['email'],
)
)
try:
user = User.create_user(
first_name=' '.join(options['first_name']),
last_name=' '.join(options['last_name']),
birthdate=options['birthdate'],
gender=options['gender'],
email=options['email'],
password=options['password'],
university=options['university'],
academic_level=options['academic_level'],
academic_field=options['academic_field']
)

except Exception as err:
raise CommandError(f'{err}')
else:
user = user[0]
try:
academic_level = AcademicLevel.objects.get(
pk=options['academic_level'])
user.academic_level = academic_level
user.save()
except AcademicLevel.DoesNotExist:
raise CommandError('AcademicLevel "%s" does not exist' %
options['academic_level'])
user.offer_free_membership(options['membership'])

try:
membership = Membership.objects.get(pk=options['membership'])
user.membership = membership
end_date = datetime.datetime.now() + membership.duration
user.membership_end = end_date
user.save()
except Membership.DoesNotExist:
raise CommandError(
'Membership "%s" does not exist' % options['membership'])
except Exception as err:
raise CommandError(f'{err}')

if options['notify'] is True:
try:
Expand Down
77 changes: 77 additions & 0 deletions blitz_api/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import binascii
import datetime
import os
from django.conf import settings
from django.db import models
Expand Down Expand Up @@ -134,6 +135,82 @@ class User(AbstractUser):
)
history = HistoricalRecords()

@classmethod
def create_user(cls,
first_name,
last_name,
birthdate,
gender,
email,
password,
academic_level=None,
university=None,
academic_field=None):

user = User.objects.create(
first_name=first_name[:30],
last_name=last_name[:150],
birthdate=birthdate,
gender=gender,
username=email,
email=email,
is_active=True,
tickets=1,
)
user.set_password(password)

if university:
try:
university = Organization.objects.get(pk=university)
user.university = university
user.save()
except Organization.DoesNotExist:
raise ValueError(
'Organization "%s" does not exist' % university)

if academic_field:
try:
academic_field = AcademicField.objects.get(
pk=academic_field)
user.academic_field = academic_field
user.save()
except AcademicField.DoesNotExist:
raise ValueError('AcademicField "%s" does not exist' %
academic_field)

if academic_level:
try:
academic_level = AcademicLevel.objects.get(
pk=academic_level)
user.academic_level = academic_level
user.save()
except AcademicLevel.DoesNotExist:
raise ValueError('AcademicLevel "%s" does not exist' %
academic_level)

return user

def offer_free_membership(self, membership):
from store.models import Membership

if not self.membership:
try:
membership = Membership.objects.get(pk=membership)
self.membership = membership
except Membership.DoesNotExist:
raise ValueError(
'Membership "%s" does not exist' % membership)

today = timezone.now().date()
if self.membership_end and self.membership_end > today:
self.membership_end = \
self.membership_end + self.membership.duration
else:
self.membership_end = (
today + self.membership.duration
)
self.save()


class TemporaryToken(Token):
"""Subclass of Token to add an expiration time."""
Expand Down
Loading

0 comments on commit c62e957

Please sign in to comment.