/cms/management/commands/checkmemberstatus.py

https://github.com/ncsu-stars/Stars-CMS · Python · 134 lines · 90 code · 26 blank · 18 comment · 26 complexity · e11181dc4a81f7408d9d3a584853cbda MD5 · raw file

  1. from django.core.management.base import BaseCommand
  2. from django.core.management import CommandError
  3. from django.conf import settings
  4. from django.db.models import Q
  5. from django.contrib.auth.models import User
  6. from cms.models import Member
  7. from sys import stdin
  8. import re
  9. ncsu_email_regex = re.compile(r'(?P<unity_id>[a-zA-Z0-9._%+-]+)@ncsu\.edu')
  10. name_regex = re.compile(r'^[-a-zA-z]+$')
  11. class Command(BaseCommand):
  12. args = '<file>'
  13. help = 'Checks that all members listed in <file> are active/empty and that all others are inactive'
  14. def handle(self, *args, **kwargs):
  15. if len(args) == 0:
  16. raise CommandError('Missing <file> argument')
  17. if args[0] == '-':
  18. f = stdin
  19. else:
  20. try:
  21. f = open(args[0], 'r')
  22. except IOError as e:
  23. raise CommandError('Unable to open file "%s": %s' % (args[0], e.message))
  24. listed_members = []
  25. for l in f.readlines():
  26. l = l.strip()
  27. # try to extract a Unity id
  28. match = re.search(ncsu_email_regex, l)
  29. if match:
  30. user_id = match.groupdict()['unity_id']
  31. # retrieve User using Unity id
  32. try:
  33. user = User.objects.get(username=user_id)
  34. # found
  35. listed_members += [ user.get_profile() ]
  36. print 'found %s by user id %s' % (user.get_full_name(), user_id)
  37. # and done
  38. continue
  39. except User.DoesNotExist:
  40. # not found, try another method
  41. pass
  42. # try to extract a name
  43. names = filter(lambda x: re.match(name_regex, x), l.split(' '))
  44. # require at least first and last name
  45. if len(names) >= 2:
  46. # search by last name
  47. try:
  48. user = User.objects.get(last_name=names[-1])
  49. # found with unique last name
  50. listed_members += [ user.get_profile() ]
  51. print 'found %s by unique last name %s' % (user.get_full_name(), names[-1])
  52. # and done
  53. continue
  54. except User.MultipleObjectsReturned:
  55. # check first name
  56. candidates = filter(lambda x: names[0] in x.first_name, User.objects.filter(last_name=names[-1]))
  57. if len(candidates) == 1:
  58. # found with unique first and last name pair
  59. user = candidates[0]
  60. listed_members += [ user.get_profile() ]
  61. print 'found %s by unique first and last name pair %s %s' % (user.get_full_name(), names[0], names[-1])
  62. # and done
  63. continue
  64. else:
  65. # not found, try another method
  66. pass
  67. except User.DoesNotExist:
  68. # not found, try another method
  69. pass
  70. # give up and ask operator
  71. user = None
  72. while user is None:
  73. user_id = raw_input('Which username belongs with %s?: ' % (l, ))
  74. try:
  75. user = User.objects.get(username=user_id)
  76. # found with operator assistance
  77. listed_members += [ user.get_profile() ]
  78. print 'found %s with operator assistance' % (user.get_full_name(),)
  79. # and done
  80. break
  81. except User.DoesNotExist:
  82. print 'username %s not found' % (user_id,)
  83. listed_pks = map(lambda x: x.pk, listed_members)
  84. members_to_activate = Member.objects.filter(Q(pk__in=listed_pks) & (Q(status=Member.STATUS_ARCHIVED) | Q(user__is_active=False)))
  85. members_to_archive = Member.objects.filter(~Q(pk__in=listed_pks) & (~Q(status=Member.STATUS_ARCHIVED) | Q(user__is_active=True)))
  86. print '====== ACTIVATE ======'
  87. for m in members_to_activate:
  88. print m.user.get_full_name()
  89. print '====== ARCHIVE ======='
  90. for m in members_to_archive:
  91. print m.user.get_full_name()
  92. if len(members_to_activate) == 0 and len(members_to_archive) == 0:
  93. print 'Nothing to do'
  94. raise SystemExit
  95. if raw_input('Does this look right? [y/N] ').lower() != 'y':
  96. print 'Aborted'
  97. raise SystemExit
  98. for m in members_to_activate:
  99. m.status = Member.STATUS_ACTIVE
  100. m.save()
  101. m.user.is_active = True
  102. m.user.save()
  103. for m in members_to_archive:
  104. m.status = Member.STATUS_ARCHIVED
  105. m.save()
  106. m.user.is_active = False
  107. m.user.save()
  108. print 'Done'