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)
def admin(username=None, action=None): if not g.user.admin: return redirect(url_for('index')) msg = "" # Make a little list of major keys for reference majors = [] for major in Major.objects.all(): majors.append(major.key) majors = ', '.join(majors) # Handle the logic of doing mass imports iform = ImportForm() test_table = None if request.method == "POST": tp = request.form['_arg_form'] if tp == "import": success = iform.validate(request.form) data = iform.data_by_attr() if success: if not data['go']: test_table = iform.body.valid_data else: for user in iform.body.valid_data: try: team = Team.objects.get_or_create(text=user.team_txt) delattr(user, 'team_txt') user.team = team[0] user.password = user.username user.active = True user.save() iform.start.add_error( {'message': 'Inserted user ' + user.username, 'type': 'success'}) except mongoengine.errors.NotUniqueError: iform.start.add_error( {'message': 'User ' + user.username + ' already exists'}) # Handle logic of approving or denying user accounts if username is not None: if action == "approve": user = User.objects.get(username=username) user.active = True user.save() msg = user.username + " has been activated" users = User.objects(active=False).all() return render_template('admin.html', iform=iform.render(), test_table=test_table, majors=majors, users=users, msg=msg, msg_type="success")
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())