def post(self): """ Register user username min 5, max 50 password min 5, max 50 :return: """ try: data = LoginRegisterParser.parse_args() user = User.query.get(username=data['username']) if user is None: user = User(username=data['username']) user.set_password(data['password']) user.create() data = {'token': user.encode_auth_token()} return responses.SuccessResponse(data, status=201).send() else: raise CustomException(detail='A user with this data exist.') except CustomException as e: return responses.ErrorResponse(message=e.detail, status=e.status_code).send()
def sign_up(request): phone = request.POST.get("phone", "") password = request.POST.get("password", "") if not is_phone(phone): return JsonResponse(4001, "请输入正确的手机号") user = User.get_by_phone(phone) if user: return JsonResponse(4002, "手机号已被注册") try: User.create(phone, password) except: return JsonResponse(4003, "注册失败,请稍后再试") return JsonResponse(200, "注册成功")
def setUp(self): User.create(first_name='Anca', last_name='Zarku', email="*****@*****.**", date_of_birth="1944-09-28", password="******", username="******", role=User.REDACTOR) User.create(first_name='Peter', last_name='Saigu', email="*****@*****.**", date_of_birth="1944-09-28", password="******", username="******", role=User.VIEWER)
def create_user(request, id_token): access_token = get_jwt_value(request).decode("utf-8") return User.create( access_token=access_token, email=id_token.get("email").lower(), first_name=id_token.get("given_name"), last_name=id_token.get("family_name"), sub=id_token.get("sub"), )
def register_handler(request): username = request.POST.get('username') password = request.POST.get('password') copy_pwd = request.POST.get('copy_pwd') print(username) print(password) if password != copy_pwd: return redirect('/register/') else: u = User() if u.findName(username): md = md5() md.update(password.encode()) pwd = md.hexdigest() u.create(username, pwd) print("u : %s",u) else: return render(request, 'login/register.html') return redirect('/login/')
def add_user(name, surname, birthday, registration_date): """ prepare data and create user :param name: str :param surname: str :param birthday: str | date :param registration_date: str | date :return: User object or None """ birthday = User.format_date(birthday) registration_date = User.format_date(registration_date) user = User.create(name=name, surname=surname, birthday=birthday, registration_date=registration_date) return user
def save(self): print('saving ....') return User.create(self.model_values())
def dashboard(): if request.method == "POST": if session["user"]["user_type"] == "user": if request.form.get("update"): amount = request.form["amount"] description = request.form["description"] comment = request.form["comment"] ID = request.form["_id"] expense = Expenses() expense.update(ID, amount, description, comment, session["user"]["email"]) elif request.form.get("delete"): expense = Expenses() expense.delete(session["user"]["email"], request.form["_id"]) elif request.form.get("create"): amount = request.form["amount"] description = request.form["description"] comment = request.form["comment"] expense = Expenses() expense.create(amount, description, comment, session["user"]["email"]) if session["user"]["user_type"] == "manager": if request.form.get("update"): name = request.form["name"] email = request.form["email"] password = request.form["password"] ID = request.form["_id"] user = User() user.update(ID, name, email, password) elif request.form.get("delete"): user = User() user.delete(request.form["_id"]) elif request.form.get("create"): print(request.form) name = request.form["name"] email = request.form["email"] password = request.form["password"] user = User() user.create(name, email, password) if session["user"]["user_type"] == "admin": if request.form.get("update_user"): name = request.form["name"] email = request.form["email"] password = request.form["password"] ID = request.form["_id"] user = User() user.update(ID, name, email, password) elif request.form.get("delete_user"): user = User() user.delete(request.form["_id"]) elif request.form.get("create_user"): print(request.form) name = request.form["name"] email = request.form["email"] password = request.form["password"] user_type = request.form["user_type"] user = User() user.create(name, email, password, user_type) elif request.form.get("update_exp"): amount = request.form["amount"] description = request.form["description"] comment = request.form["comment"] ID = request.form["_id"] expense = Expenses() expense.update(ID, amount, description, comment, "admin") elif request.form.get("delete_exp"): expense = Expenses() expense.delete("admin", request.form["_id"]) elif request.form.get("create_exp"): amount = request.form["amount"] description = request.form["description"] comment = request.form["comment"] email = request.form["email"] expense = Expenses() expense.create(amount, description, comment, email) expenses = db.expenses.find({"email": session["user"]["email"]}) expenses_admin = db.expenses.find() users = db.users.find({"user_type": "user"}) users_admin = db.users.find() return render_template('dashboard.html', expenses=expenses, users=users, expenses_admin=expenses_admin, users_admin=users_admin)
def create_user(): user = User.create(**request.get_json()) return jsonify(user)
def provide_info(): if WHITELIST_CLOSED: raise WhitelistClosed() if WHITELIST_OPEN_DATE and WHITELIST_OPEN_DATE > datetime.datetime.utcnow( ): raise WhitelistClosed(details=dict( open_ts=int(WHITELIST_OPEN_DATE.strftime('%s')))) schema = Schema( { 'first_name': All(Length(2, 30), str), 'last_name': All(Length(2, 30), str), 'email': Email(), 'dob': Coerce(to_datetime), 'address': All(Length(1, 100), str), 'city': All(Length(2, 30), str), Optional('state_code', default=None): Any(None, All(Length(0, 30), str)), 'zip_code': All(Length(2, 20), str), 'country_code': All(Length(2, 3), str), 'phone': All(Length(8, 20), str), Optional('eth_address', default=None): Coerce(to_eth), Optional('eth_amount', default=None): All(Range(0, 100), Any(float, int)), 'telegram': Coerce(to_telegram), 'confirmed_location': bool, 'dfp': All(Length(10, 4096), str), Optional('medium', default=None): Any(None, All(Length(0, 150), str)), Optional('reddit', default=None): Any(None, All(Length(0, 150), str)), Optional('twitter', default=None): Any(None, All(Length(0, 150), str)), Optional('linkedin', default=None): Any(None, All(Length(0, 150), str)), Optional('facebook', default=None): Any(None, All(Length(0, 150), str)), }, extra=REMOVE_EXTRA, required=True) data = schema(request.json) data['email'] = data['email'].lower() if not data['confirmed_location']: raise ValidationError('Unconfirmed location') data['ip'] = request.remote_addr data['ip_country'] = request.headers.get('CF-IPCountry') # Try to find existing user user = User.objects(eth_address=data['eth_address']).first() if user: return jsonify(user.to_json()) ####################### WHITELIST IS CLOSED FOR NEW USERS #################### raise WhitelistClosed() user = User.create(**data) status = user.transition(INFO_NOT_VERIFIED) if status == INFO_NOT_VERIFIED: verify_info(user) return jsonify(user.reload().to_json()), 201
def test_user_creation_exceptions(self): # first name value error self.assertRaises(ValueError, User.create, first_name=None, last_name='Zarku', email="*****@*****.**", date_of_birth="1944-09-28", password="******", username="******", role=User.REDACTOR) # last name value error self.assertRaises(ValueError, User.create, first_name="Anca", last_name=None, email="*****@*****.**", date_of_birth="1944-09-28", password="******", username="******", role=User.REDACTOR) # date of birth value error self.assertRaises(ValueError, User.create, first_name="Anca", last_name='Zarku', email="*****@*****.**", date_of_birth=None, password="******", username="******", role=User.REDACTOR) # email value error self.assertRaises(ValueError, User.create, first_name="Anca", last_name='Zarku', email=None, date_of_birth="1944-09-28", password="******", username="******", role=User.REDACTOR) # username value error self.assertRaises(ValueError, User.create, first_name="Anca", last_name='Zarku', email="*****@*****.**", date_of_birth="1944-09-28", password="******", username=None, role=User.REDACTOR) # email unique error try: with transaction.atomic(): User.create(first_name="Anca", last_name='Zarku', email="*****@*****.**", date_of_birth="1944-09-28", password="******", username="******", role=User.REDACTOR) except Exception as e: if e.__str__().__contains__("already exists"): self.assertEqual(e.__str__().__contains__("email"), True) else: # Correct exception not caught => fail self.assertEqual(True, False) # username unique error try: with transaction.atomic(): User.create(first_name="Anca", last_name='Zarku', email="*****@*****.**", date_of_birth="1944-09-28", password="******", username="******", role=User.REDACTOR) except Exception as e: if e.__str__().__contains__("already exists"): self.assertEqual(e.__str__().__contains__("username"), True) else: # Correct exception not caught => fail self.assertEqual(True, False)