def login(): password = request.data.get('password', '') userName = request.data.get('userName', '') user = User.get_all().filter(User.userName == userName).first() print(user) if user is not None: response = jsonify({ 'status': 'ok', 'type': 'account', 'userId': user.id, 'currentAuthority': 'admin' }) loggedinuser = user.id session = Session(user.id) session.save() print('saved') return make_response(response), 200 return make_response( jsonify({ 'status': 'error', 'type': 'account', 'currentAuthority': 'guest' })), 200
def register(): form = RegisterForm() if form.validate_on_submit(): #todo: try email_address = UserEmailAddress.query.filter_by(email_address=form.email.data).first() username = User.query.filter_by(username=form.username.data).first() if username is not None: flash("The username ({0}) is already taken".format(form.username.data), category="danger") elif email_address is not None: flash("That e-mail address has already been registered on this site", category="danger") else: new_user = User( username=form.username.data, first_name=form.first_name.data, last_name=form.last_name.data, password_digest=crypto.get_digest(form.password.data)) db.session.add(new_user) db.session.commit() new_user.add_to_users_group() new_email_address = UserEmailAddress( user_id=new_user.id, email_address=form.email.data) db.session.add(new_email_address) db.session.commit() activation = EmailActivation( user_id=new_user.id, email_address_id=new_email_address.id) db.session.add(activation) db.session.commit() new_user.primary_email_id = new_email_address.id db.session.add(new_user) db.session.commit() emails.send_user_email_activation(activation) flash("Please check your e-mail for activation instructions", category="info") return redirect('login') return render_template( 'auth/page_register.html', title='Register', form=form)
def find_or_create_user(username, email, password, role=None): """ Find existing user or create new user """ user = User.query.filter(User.email == email).first() if not user: user = User(username=username, email=email, password=current_app.user_manager.hash_password(password), is_enabled=True, confirmed_at=datetime.datetime.utcnow()) if role: user.roles.append(role) db.session.add(user) return user
def list_targetgroups(): userId = request.data.get('userId', 0) user = User.get_all().filter(User.id == userId).first() targetgroups = TargetGroup.get_all().filter( TargetGroup.country.has( Country.id.in_(x.id for x in user.countries))) results = [] for tg in targetgroups: results.append(tg.serialise()) return make_response(jsonify({'list': results})), 200
def post(self): data = request.json # profile = Profile(id=uuid.uuid4().hex, email=data.get('email')) if data.get('password') != data.get('password_confirm'): abort(400, 'Passsword not match') nickname = data.get('nickname') if nickname is None: nickname = data.get('username') userCreated = User(id=uuid.uuid4().hex, username=data.get('username'), email=data.get('email'), nickname=nickname, password=data.get('password')) create_user(userCreated) return userCreated, 201
def currentUser(): session = Session.get_all().first() if session == None: return make_response( jsonify({ 'status': 'ok', 'type': 'account', 'currentAuthority': 'guest' })), 200 print(session.userId) user = User.get_all().filter(User.id == session.userId).first() res = user.serialise() response = jsonify(res) return make_response(response), 200
def currentUser(): session = Session.get_all().first() if session == None: return make_response(jsonify({'status': 'ok', 'type' : 'account', 'currentAuthority': 'guest'})), 200 print (session.userId) user = User.get_all().filter(User.id == session.userId).first() print (user.favourites) res = user.serialise() res['favourites'] = [] for fav in user.favourites: res['favourites'].append(Card.get_all().filter(Card.id == fav.cardId).first().serialise()) response = jsonify(res) return make_response(response), 200
def upload_users(filename): columns_keys = ['employee_number', 'last_name', 'first_name', 'suffix', 'middle_name', 'email'] wb = oxl.load_workbook(filename) ws = wb.active headers = [c.value for c in ws[1]] some_columns_missing = False for i in columns_keys: if i not in headers: print('\n') if not some_columns_missing else False print("Column '{}' not found in upload.".format(i)) some_columns_missing = True if some_columns_missing: return columns_values = [headers.index(i) + 1 for i in columns_keys] columns_dict = dict(zip(columns_keys, columns_values)) rec_count = 0 duplicate_rows = [] employee_numbers = db.session.query(User.employee_number).all() employee_number_list = [v[0] for v in employee_numbers] rows_to_process = ws.max_row for i in range(2, rows_to_process + 1): if str(ws.cell(i, columns_dict['employee_number']) .value) in employee_number_list: duplicate_rows.append(i) else: user = User( email=ws.cell(i, columns_dict['email']).value, employee_number=ws.cell(i, columns_dict['employee_number']).value, password=current_app.user_manager.hash_password( current_app.config['DEFAULT_USR_PWD']), active=True, email_confirmed_at=datetime.datetime.utcnow()) db.session.add(user) userdetail = UserDetail( employee_number=ws.cell(i, columns_dict['employee_number']).value, last_name=ws.cell(i, columns_dict['last_name']).value, first_name=ws.cell(i, columns_dict['first_name']).value, middle_name=ws.cell(i, columns_dict['middle_name']).value, suffix=ws.cell(i, columns_dict['suffix']).value) db.session.add(userdetail) rec_count += 1 if rec_count > 999: db.session.commit() rec_count = 0 db.session.commit() if duplicate_rows: wb2 = oxl.Workbook() ws2 = wb2.active ws2.append(headers) for i in duplicate_rows: values_to_copy = [c.value for c in ws[i]] ws2.append(values_to_copy) wb2.save('duplicates.xlsx') print('\nDuplicate rows saved in \'duplicates.xlsx\'') else: print('\nDone.')
def on_model_change(self, form, UserDetail, is_created): try: if not form.user_id.data: # create a User u = User(email=request.form['email'], password=current_app.user_manager. hash_password(current_app.config['DEFAULT_USR_PWD']), email_confirmed_at=datetime.utcnow(), active=True) u.detail = UserDetail db.session.add(u) # if no salary set if not form.salary_data.data: if request.form['SGSI'] == '-1': # no change in salary return s = dict(form.SGSI.choices).get(int(request.form['SGSI'])) update_salary = (form.salary_data.data != s) if update_salary: s = re.search(r'(.+)-(.+) \[(.+)\]', s.replace(',', '')) member_salary = MemberSalary.query.\ filter_by(user_detail_id=UserDetail.id).first() if not member_salary: member_salary = MemberSalary( user_detail_id=UserDetail.id, sg=s.group(1), step=s.group(2), salary=s.group(3), effective_date=datetime.utcnow()) db.session.add(member_salary) else: s2 = f"{member_salary.sg}-"\ f"{member_salary.step} "\ f"[{member_salary.salary:,.0f}]" print(member_salary.user_detail_id, '\n') print(s2) if s2 != s: sal_hist = MemberSalaryHistory( user_detail_id=member_salary.user_detail_id, sg=member_salary.sg, step=member_salary.step, salary=member_salary.salary, effective_date=member_salary.effective_date) db.session.add(sal_hist) member_salary.sg = s.group(1) member_salary.step = s.group(2) member_salary.salary = s.group(3) member_salary.effective_date = datetime.utcnow() db.session.commit() except SQLAlchemyError as e: db.session.rollback() if not self.handle_view_exception(e): raise return