def post(self): u = User(**request.json) db.session.add(u) db.session.commit() return u.serialize, 201
class UserTests(unittest.TestCase): GOOD_USER_1 = User( username = "******", email = "*****@*****.**", password = "******", fullname = "Test User", gender = True, agreedToTerms = True ) GOOD_USER_2 = User( username = "******", email = "*****@*****.**", password = "******", fullname = "Test User 2", gender = True, agreedToTerms = True ) def setUp(self): app.config.from_object('config.TestingConfig') db.create_all() def tearDown(self): pass def test_make_user(self): db.session.add(self.GOOD_USER_1) db.session.commit() self.assertEqual(self.GOOD_USER_1.id, 1) def test_add_duplicate_user(self): BAD_USER = deepcopy(self.GOOD_USER_1) db.session.add(self.GOOD_USER_1) db.session.commit() try: db.session.add(BAD_USER) db.session.commit() except IntegrityError: pass else: raise Exception("Failed to prevent duplicate user from being made")
class TagTests(unittest.TestCase): USER = User(username="******", email="*****@*****.**", password="******", fullname="Test User", gender=True, agreedToTerms=True) TAG = Tag(name="testtag") def setUp(self): db.create_all() db.session.add(self.USER) db.session.add(self.TAG) db.session.commit() def tearDown(self): pass def test_add_tag_to_activity(self): a = ActivityPost(availability="anytime", description="desc") self.USER.activity_posts.append(a) a.tags.append(self.TAG) db.session.add(a) db.session.commit() tag = Tag.query.get(self.TAG.id) activity = ActivityPost.query.get(a.id) self.assertIn(tag, activity.tags) self.assertIn(activity, tag.activities) def test_add_tag_to_opportunity(self): o = Organization(name="O", email="[email protected]", phone="1234567890") op = OpportunityPost(location="123 Fake Street", description="A description", when="now") self.USER.organizations.append(o) o.opportunities.append(op) op.tags.append(self.TAG) db.session.add(o) db.session.add(op) db.session.commit() tag = Tag.query.get(self.TAG.id) opportunity = OpportunityPost.query.get(op.id) self.assertIn(tag, opportunity.tags) self.assertIn(opportunity, tag.opportunities)
def init_users(): """Inicializar la base de datos.""" users_ids = {} for user in users: users_list = User.objects.filter(email=user['email']) user_object = users_list.first() if not user_object: user['password'] = generate_password_hash(user['password']) element = User(**user).save() users_ids[user['name']] = str(element.id) return users_ids
def register(): content = request.json try: reg_email = re.match(EMAIL_REGEX, content['email']).group(0) u = User(content['username'], content['email'], content['password'], content['fullname'], content['gender']) db.session.add(u) db.session.commit() except IntegrityError as e: return jsonify({"errorMessage": "Account with email or username already exists."}), 409 # Conflict except AttributeError: return jsonify({"errorMessage": "Email was invalid."}), 400 # Bad Request return jsonify(u.serialize), 201 # Created
def __register_user_by_email(): form = UserEmailForm().validate_for_api() User.register_by_email(form.nickname.data, form.account.data, form.secret.data)
# -*- coding:utf-8 -*- """ created by MiracleWong on 2019/2/9 """ __author__ = 'MiracleWong' from app import create_app from app.modules.base import db from app.modules.user import User app = create_app() with app.app_context(): with db.auto_commit(): # 创建一个超级管理员 user = User() user.nickname = 'Super' user.password = '******' user.email = '*****@*****.**' user.auth = 2 db.session.add(user)