def setUp(self): self.app = 'api' super().setUp() self.dummy = User(username='******') self.dummy.save() m = UserMeta(User=self.dummy) m.save()
def lti(request): if not get_timeslot(): return HttpResponse( 'Login is not available. The system is currently closed.', status=403) config = getattr(settings, 'PYLTI_CONFIG', dict()) consumers = config.get('consumers', dict()) params = dict(request.POST.items()) headers = request.META headers['X-Forwarded-Proto'] = headers['HTTP_X_FORWARDED_PROTO'] try: verify_request_common(consumers, request.build_absolute_uri(), request.method, headers, params) except LTIException as e: logger.error('LTI exception from canvas; {}'.format(e)) return HttpResponse("Signature Validation failed!", status=403) data = request.POST try: username = data['lis_person_sourcedid'] email = data['lis_person_contact_email_primary'] studentnumber = data['custom_canvas_user_login_id'] coursecode = data['context_label'] except KeyError as e: logger.error('Invalid post data from canvas; {}; {}'.format(data, e)) return HttpResponse("Missing data in POST", status=400) user = get_user(email, username) if user is None: user = User(email=email, username=username) user.save() try: meta = user.usermeta except UserMeta.DoesNotExist: meta = UserMeta(User=user) meta.Studentnumber = studentnumber if not meta.Overruled: if settings.COURSE_CODE_BEP in coursecode: meta.EnrolledBEP = True elif settings.COURSE_CODE_EXT in coursecode: meta.EnrolledBEP = True meta.EnrolledExt = True else: logger.warning( 'Course code not matched on BEP or EXT for user {}. Code was: {}' .format(user, coursecode)) meta.save() meta.TimeSlot.add(get_timeslot()) meta.save() user.save() log = CanvasLogin() log.Subject = user log.save() return redirect("{}/login/".format(settings.DOMAIN))
def is_mail_suppressed(user): """ Check if a user has the 'SuppressStatusMails' setting set to True. This is if a user doesn't want to receive mails. :param user: :return: """ try: meta = user.usermeta except UserMeta.DoesNotExist: meta = UserMeta() user.usermeta = meta meta.save() user.save() return meta.SuppressStatusMails
def lti(request): config = getattr(settings, 'PYLTI_CONFIG', dict()) consumers = config.get('consumers', dict()) params = dict(request.POST.items()) headers = request.META headers['X-Forwarded-Proto'] = headers['HTTP_X_FORWARDED_PROTO'] try: verify_request_common(consumers, request.build_absolute_uri(), request.method, headers, params) except LTIException: return HttpResponse("Signature Validation failed!", status=403) data = request.POST try: username = data['lis_person_sourcedid'] email = data['lis_person_contact_email_primary'] studentnumber = data['custom_canvas_user_login_id'] coursecode = data['context_label'] except KeyError: return HttpResponse("Missing data in POST", status=400) user = get_user(email, username) if user is None: user = User(email=email, username=username) user.save() try: meta = user.usermeta except UserMeta.DoesNotExist: meta = UserMeta(User=user) meta.Studentnumber = studentnumber if not meta.Overruled: if coursecode == '5XEC0': meta.EnrolledBEP = True elif coursecode == '5XED0': meta.EnrolledBEP = True meta.EnrolledExt = True meta.save() meta.TimeSlot.add(get_timeslot()) meta.save() user.save() log = CanvasLogin() log.Subject = user log.save() return redirect("https://bep.ele.tue.nl/login")
def setUp(self): self.app = 'support' super().setUp() # create dummy user for upgrade/downgrade self.dummy = User(username='******') self.dummy.save() m = UserMeta(User=self.dummy) m.save() # create dummy public file for edit and delete self.publicfile = PublicFile(File='/home/django/dummy.txt') self.publicfile.save() # self.debug =True t = MailTemplate( Message='test', Subject='test', RecipientsStaff='[]', RecipientsStudents='[]', ) t.pk = 1 t.save()
def create_user_from_email(email, username, student=False): """ Create a new user based on its email address. This user is updated with a real username as soon as the person logs in for the first time. :param email: emailaddres :param username: username to create, usually a part of the email address :param student: whether the users is a student. If false, user is added to the assistants group :return: THe created user account """ parts = email.split('@')[0].split('.') # strip possible index number at the end. if parts[-1].isdigit(): parts.pop() # get all single letters (initials etc) initials = '' while len(parts[0]) == 1: initials += parts.pop(0) + '.' # what remains is lastname. Join possible multiple last names. last_name = (' '.join(parts)).title() initials = initials.title() new_account = User.objects.create_user(username, email) new_account.first_name = initials new_account.last_name = last_name if not student: new_account.groups.add(get_grouptype('2u')) new_account.full_clean() new_account.save() m = UserMeta( User=new_account, Initials=initials, Fullname="{}, {}".format(last_name, initials), ) m.full_clean() m.save() return new_account
def create_users(self): """ Takes self.usernames and creates users based on this. The last character of the username determnines the group WARNING: This only generates users and assign groups. It does not assign roles (like groupadministration and trackhead models). """ # create testusers using the naming-patern: [r t]-[s p 1 2 u 3 t ] # r=random (any person of the given type), t=this(for this project) # 1=type1, 2=type2, 3=type3, s=Student, u=Unverified-type2, h=track-Head, p=Private-student self.usernames = [ 'r-1', # responsible staff 't-1', # responsible staff 'r-h', # responsible staff, trackhead 't-h', # responsible staff, trackhead 'r-2', # assistant 't-2', # assistant 't-u', # unverified assistant 'r-3', # support staff 'r-s', # student without private proposal 't-p', # student with private proposal 'r-4', # Capacity group administration of other group than tested proposal 't-4', # Capacity group administration of group of tested proposal 'r-5', # Study advisor 'r-6', # Prof skill administration 'ra-1', # type1 and assessor of other project 'ta-1', # type1 and assessor of project 'sup', # god user (superuser) 'ano', # anonymous user ] # Create the users and assign groups/roles. self.users = {} for n in self.usernames: if n != 'ano': u = User(username=n) u.email = n + '@' + settings.STAFF_EMAIL_DOMAINS[0] u.save() x = n.split('-')[-1] if x == '1': u.groups.add(self.type1staff) elif x == 'h': # trackhead u.groups.add(self.type1staff) elif x == '2': u.groups.add(self.type2staff) elif x == 'u': u.groups.add(self.type2staffunverified) elif x == '3': u.groups.add(self.type3staff) elif x == '4': u.groups.add(self.type4staff) elif x == '5': u.groups.add(self.type5staff) elif x == '6': u.groups.add(self.type6staff) elif n == 'sup': u.groups.add(self.type3staff) u.is_superuser = True u.is_staff = True else: # student u.email = n + '@' + settings.STUDENT_EMAIL_DOMAINS[0] u.save() self.users[n] = u m = UserMeta( User=u, EnrolledBEP=True, ) m.save() m.TimeSlot.add(self.ts) m.save() ua = UserAcceptedTerms(User=u) ua.save()
print("creating {} professors".format(NUMPROFS)) for i in range(0, NUMPROFS): try: prof = User.objects.create_user('professor{}'.format(i), 'professor{}@tue.nl'.format(i), 'marketplace') prof.first_name = "professor" prof.last_name = str(i) prof.groups.add(type1staff) prof.save() profs.append(prof) except: print(str(i) + " not created") prof = User.objects.get(username='******'.format(i)) try: mta = UserMeta() mta.Fullname = "Professor-" + str(i) mta.Studentnumber = 0 mta.User = prof mta.save() print("usermeta prof" + str(i)) except: print(str(i) + " prof usermeta not created") print("creating {} phders".format(NUMPHDS)) for i in range(0, NUMPHDS): try: phd = User.objects.create_user('phd{}'.format(i), 'phd{}@tue.nl'.format(i), 'marketplace') phd.first_name = "phd" phd.last_name = str(i)
def osirisToMeta(request): if request.method == 'POST': form = ConfirmForm(request.POST) if form.is_valid(): data = osirisData() for p in data.getalldata(): try: user = User.objects.get(email=p.email) except User.DoesNotExist: continue try: meta = user.usermeta except UserMeta.DoesNotExist: meta = UserMeta() user.usermeta = meta meta.save() user.save() if p.automotive: meta.Study = 'Automotive' else: meta.Study = 'Eletrical Engineering' meta.Cohort = p.cohort meta.ECTS = p.ects meta.Studentnumber = p.idnumber meta.save() return render(request, 'base.html', { 'Message': 'usermeta updated!', 'return': 'osirisdata:list' }) else: form = ConfirmForm() return render( request, 'GenericForm.html', { 'form': form, 'formtitle': 'Confirm rewrite usermeta', 'buttontext': 'Confirm' })
def osirisToMeta(request): write_errors = [] try: data, log = read_osiris_xlsx() except: return render( request, 'base.html', { 'Message': 'Retrieving Osirisdata failed. Please upload a valid file.', 'return': 'index:index', }) if request.method == 'POST': count = 0 form = ConfirmForm(request.POST) if form.is_valid(): for p in data: try: user = User.objects.get(email=p.email) except User.DoesNotExist: write_errors.append('User {} skipped'.format(p.email)) continue try: meta = user.usermeta except UserMeta.DoesNotExist: meta = UserMeta() user.usermeta = meta meta.save() user.save() if p.automotive: meta.Study = 'Automotive' else: meta.Study = 'Electrical Engineering' meta.Cohort = p.cohort meta.ECTS = p.ects meta.Studentnumber = p.idnumber meta.save() count += 1 return render( request, 'base.html', { 'Message': mark_safe('User meta updated for {} users. <br />'.format( count) + print_list(write_errors)), 'return': 'osirisdata:list' }) else: form = ConfirmForm() return render( request, 'osirisdata/osiris_to_meta_form.html', { 'form': form, 'formtitle': 'Confirm write to usermeta', 'buttontext': 'Confirm' })
import os import django os.environ['DJANGO_SETTINGS_MODULE'] = 'BepMarketplace.settings_development' django.setup() from django.contrib.auth.models import User from index.models import UserMeta for user in User.objects.all(): try: meta = user.usermeta except UserMeta.DoesNotExist: meta = UserMeta(User=user) meta.save()