Skip to content

Commit

Permalink
Merge pull request #857 from dotKom/new-member-lists-sync
Browse files Browse the repository at this point in the history
New member lists sync
  • Loading branch information
hernil committed Aug 14, 2014
2 parents 2a1c94e + baa685a commit f36dcd9
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions onlineweb4/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
'apps.mommy',
'apps.profiles',
'apps.genfors',
'scripts',

# Wiki
'wiki',
Expand Down
Empty file added scripts/__init__.py
Empty file.
Empty file added scripts/management/__init__.py
Empty file.
Empty file.
74 changes: 74 additions & 0 deletions scripts/management/commands/new_membership_lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# -*- coding: utf-8 -*-

import pytz
import datetime

from django.db.utils import IntegrityError
from django.core.management.base import BaseCommand, CommandError
from django.utils import timezone

from apps.authentication.models import AllowedUsername


class Command(BaseCommand):

def handle(self, *args, **kwargs):
now = timezone.now()
now = datetime.datetime(now.year, 9, 15, 0, 0)
# subtract a year if we're adding people with this script in the spring
if now.month < 6:
now = now - datetime.timedelta(days=365)
pytz.timezone(timezone.get_default_timezone_name()).localize(now)

expiration_date = now
note = ''

if len(args) != 1:
self.stdout.write("Error: You need to specify a filename as the first argument.")
return
filename = args[0]
f = open(filename, 'r')

new_count = 0
update_count = 0

for line in f:
line = line.strip()
# skip empty lines
if not line:
continue
# set the correct expiry date according to FOS.
if line == 'MIT' or line == 'mit':
expiration_date = now + datetime.timedelta(days=365*2)
note = 'Master %d' % now.year
continue
if line == 'BIT' or line == 'bit':
expiration_date = now + datetime.timedelta(days=365*3)
note = 'Bachelor %d' % now.year
continue


try:
entry = AllowedUsername(
username = line,
registered = now,
note = note,
description = "Added by script.",
expiration_date = expiration_date
)
entry.save()

new_count = new_count + 1
except IntegrityError:
au = AllowedUsername.objects.get(username = line)
au.expiration_date = expiration_date
au.save()

update_count = update_count + 1


if new_count > 0:
self.stdout.write("%d new memberships added" % new_count)
if update_count > 0:
self.stdout.write("%d memberships updated" % update_count)

File renamed without changes.

0 comments on commit f36dcd9

Please sign in to comment.