def signup(): form = RegisterForm.get_form() if request.method == 'POST': success = form.validate(request.form) data = form.data_by_attr() if success: try: user = User(email=data['email'], name=data['full_name'], _type=data['type'], username=data['username']) # In case there are no options, only set these if possible if data['team']: user.team = Team(id=data['team']) if data['major']: user.major = Major(key=data['major']) user.password = data['password'] user.save() except (mongoengine.errors.OperationError, mongoengine.errors.ValidationError): raise form.start.add_error( {'message': 'Unknown database error, please retry.'}) else: form.start.add_error( {'message': 'You\'ve been successfully registered, your account is pending approval', 'type': 'success'}) return render_template('signup.html', form=form.render())
def validator(self): majlst = [] for maj in Major.objects.all(): majlst.append(maj.key) users = [] for ln in str(self.body.data).splitlines(True): user = User() # clean up and strip the list pts = [x.strip() for x in ln.split(',')] # lowercase specific parts pts[2] = pts[2].lower() pts[4] = pts[4].lower() pts[5] = pts[5].lower().capitalize() if pts[2] not in ["alum", "senior", "vol", "other"]: self.body.add_error({'message': pts[1] + ' type is invalid.'}) else: if pts[2] == "alum": user._type = 2 elif pts[2] == "senior": user._type = 1 elif pts[2] == "vol": user._type = 0 else: user._type = 3 if pts[4].lower() not in majlst: self.body.add_error({'message': pts[1] + ' major is invalid.'}) else: user.major = Major.objects.get(key=pts[4]) if not EmailValidator().valid(pts[3]): self.body.add_error({'message': pts[1] + ' email is invalid.'}) else: user.email = pts[3] try: Team.objects.get(text=pts[5]) except Team.DoesNotExist: self.start.add_error( {'message': pts[1] + '\'s team ' + pts[5] + ' does not exist!', 'type': 'warn', 'block': False}) user.team_txt = pts[5] user.name = pts[1] user.username = pts[0] users.append(user) setattr(self.body, 'valid_data', users)