def handle(self, *args, **kwargs): now = self.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)
def approve_application(request): if request.is_ajax(): if request.method == "POST": application_id = request.POST.get("application_id") apps = MembershipApproval.objects.filter(pk=application_id) if apps.count() == 0: response_text = json.dumps({ "message": _("""Kan ikke finne en søknad med denne IDen (%s). Om feilen vedvarer etter en refresh, kontakt [email protected].""") % application_id }) return HttpResponse(status=412, content=response_text) app = apps[0] if app.processed: response_text = json.dumps( {"message": _("Denne søknaden er allerede behandlet.")}) return HttpResponse(status=412, content=response_text) user = app.applicant if not user.ntnu_username: response_text = json.dumps({ "message": _("""Brukeren (%s) har ikke noe lagret ntnu brukernavn.""") % user.get_full_name() }) return HttpResponse(status=412, content=response_text) if app.is_fos_application(): user.field_of_study = app.field_of_study user.started_date = app.started_date user.save() if app.is_membership_application(): membership = AllowedUsername.objects.filter( username=user.ntnu_username) if membership.count() == 1: membership = membership[0] membership.expiration_date = app.new_expiry_date if not membership.description: membership.description = "" membership.description += """ ------------------- Updated by approvals app. Approved by %s on %s. Old notes: %s """ % ( request.user.get_full_name(), str(timezone.now().date()), membership.note, ) membership.note = (user.get_field_of_study_display() + " " + str(user.started_date)) membership.save() else: membership = AllowedUsername() membership.username = user.ntnu_username membership.expiration_date = app.new_expiry_date membership.registered = timezone.now().date() membership.note = (user.get_field_of_study_display() + " " + str(user.started_date)) membership.description = """Added by approvals app. Approved by %s on %s.""" % ( request.user.get_full_name(), str(timezone.now().date()), ) membership.save() app.processed = True app.processed_date = timezone.now() app.approved = True app.approver = request.user app.save() return HttpResponse(status=200) raise Http404
def find_user_study_and_update(user, groups): study_group = get_study(groups) study_id = get_group_id(study_group) study_year = get_year(study_id, groups) study_name = study_group.get('displayName') field_of_study = get_field_of_study(groups) # Remove the years from bachelor if the user is a master student. if study_year >= 4: start_date_for_study = study_year - 3 else: start_date_for_study = study_year # Approvals are usually set from July 1st, so we do that here too. started_date = datetime(timezone.now().year - start_date_for_study + 1, 7, 1) logger.debug('Found {} to be studying {} on year {}'.format( user, study_id, study_year)) if study_name: application = MembershipApproval.objects.create( applicant=user, approver=user, processed=True, processed_date=timezone.now(), approved=True, message='Automatisk godkjent gjennom integrasjon mot Dataporten.', field_of_study=field_of_study, new_expiry_date=get_expiry_date( started_date.year, get_length_of_field_of_study(field_of_study)), started_date=started_date, ) if application.is_fos_application(): user.field_of_study = application.field_of_study user.started_date = application.started_date user.save() if application.is_membership_application(): membership = AllowedUsername.objects.filter( username=user.ntnu_username) if membership.count() == 1: membership = membership[0] if not membership.description: membership.description = '' membership.description += """ ------------------- Updated by dataporten app. Automatically approved on %s. Old notes: %s """ % (str(timezone.now().date()), membership.note) membership.note = user.get_field_of_study_display( ) + " " + str(user.started_date) else: membership = AllowedUsername() membership.username = user.ntnu_username membership.registered = timezone.now().date() membership.description = """Added by dataporten app. Automatically approved on %s.""" % (str(timezone.now().date())) membership.note = user.get_field_of_study_display( ) + " " + str(user.started_date) membership.expiration_date = application.new_expiry_date logger.debug("ntnu username: ") logger.debug(user.ntnu_username) membership.save() return True, study_name, study_year