def api_register(): username = request.form.get('username') password = request.form.get('password') if not isinstance(username, str) or not 2 <= len(username) <= 100: return jsonify(message="invalid username!", code=406) if not isinstance(username, str) or not 2 <= len(password) <= 100: return jsonify(message="invalid password!", code=406) dup_user = model.User.query.filter_by(username=username).first() if dup_user is not None: return jsonify(message="username already exists!", code=406) password = generate_password_hash(password + username) candidate_user = model.User(username=username, password=password) db.session.add(candidate_user) db.session.commit() s3 = boto3.client('s3') s3.put_object(Bucket='chaoshuai', Body='', Key=username + '/') s3.put_object(Bucket='chaoshuai', Body='', Key=username + '/' + 'original/') s3.put_object(Bucket='chaoshuai', Body='', Key=username + '/' + 'processed/') return jsonify(message="user created!", code=201)
def add_user(): if len(request.json['username']) > 20: abort(413) if len(request.json['password']) > 20: abort(413) model.User(username=request.json['username'], password_hash=str( md5((request.json['password']).encode()).hexdigest()), register_date=datetime.now()).save() return jsonify({ 'success': True, 'message': 'User was added.', 'data': None })
def create_test_user(): me = model.User() me.facebook_id = "2335427115026" me.first_name = "Jessica" me.last_name = "McElroy" me.email = "*****@*****.**" me.avatar = "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpa1/v/t1.0-1/c32.0.50.50/p50x50/10169369_2128045370612_4824974463606072307_n.jpg?oh=84abbe14a17716d2e6b66d07a88f11f8&oe=54D23BF6&__gda__=1423797041_941578f05e56238a7887592bb802e207" me.facebook_url = "https://www.facebook.com/app_scoped_user_id/2335427115026/" db.session.add(me) db.session.commit() user = model.User.query.get(1) name = user.first_name print name, "added to database!"
def create_user(user, email, password=None, role='user'): if not password: password = read_pwd() data = dict(user_name=user, email=email, password=hash_pwd(password), active=True) errors = schema.UserSchema(exclude=('version_id', )).validate( data, db.session) if errors: raise InvalidCommand('Validation Error: %s' % errors) role = model.Role.query.filter_by(name=role).one() u = model.User(**data) u.roles.append(role) db.session.add(u) # @UndefinedVariable db.session.commit() # @UndefinedVariable
from app import create_app, db, model # db.create_all(app = create_app()) app = create_app() with app.app_context(): u1 = model.User(username='******', password='******', email='*****@*****.**') u2 = model.User(username='******', password='******', email='*****@*****.**') u3 = model.User(username='******', password='******', email='*****@*****.**') u4 = model.User(username='******', password='******', email='*****@*****.**') db.session.add(u1) db.session.add(u2) db.session.add(u3) db.session.add(u4) db.session.commit() # 相当于SQL操作INSERT INTO User VALUES(字段1='值1',字段2='值2'),并将该SQL语句赋值到一个对象上 # u1 = db.User(username='******',password='******',email='*****@*****.**') # u2 = db.User(username='******',password='******',email='*****@*****.**') # u3 = db.User(username='******',password='******',email='*****@*****.**') # u4 = db.User(username='******',password='******',email='*****@*****.**') # #将对于数据库的操作保存在缓存中,既然是保存到缓存中,那么该数据库语句还没有提交到数据库中 # db.session.add(u1) # db.session.add(u2) # db.session.add(u3) # db.session.add(u4) #commit()和数据库中的commit指令一样,将数据库操作提交到数据库中。
def test_login(self): with self.client as c: res = c.get('/') self.assertTrue('Welcome' in str(res.data)) def test_login(user, pwd, check=[]): res = c.post('/login', data={'username': user, 'password': pwd}, follow_redirects=True) self.assert200(res) if isinstance(check, str): check = [check] for phrase in check: self.assertTrue( phrase.lower() in str(res.data).lower(), 'Result should contain %s' % phrase) res = c.get('/login') self.assertTrue( 'User Name:' in str(res.data) and 'Password:'******'admin', 'admin', 'Welcome') res = c.get('/logoff', follow_redirects=True) self.assert200(res) self.assertTrue( 'User Name:' in str(res.data) and 'Password:'******'admin', 'wrong', 'Invalid user name or password') test_login('usak', 'wrong', 'Invalid user name or password') def test_login_json(user, pwd, success=True, failure=False, email=None): if email: res = c.post('/login', data='{"email":"%s", "password":"******"}' % (email, pwd), content_type='application/json') else: res = c.post('/login', data='{"username":"******", "password":"******"}' % (user, pwd), content_type='application/json') if success: self.assert200(res) self.assertTrue( res.json.get('access_token') and len(res.json.get('access_token')) > 60) elif not failure: self.assert200(res) self.assertEqual(res.json, {"error": "Invalid Login"}) else: self.assert400(res) test_login_json('admin', 'admin', success=True) test_login_json( None, 'admin', email='*****@*****.**', success=True) test_login_json('admin', 'xxx', success=False) test_login_json('pakomako', 'admin', success=False) test_login_json('admin', '', success=False, failure=True) test_login_json('', '', success=False, failure=True) new_user = model.User( user_name='test', email='*****@*****.**', password=hash_pwd('test'), active=True) app.db.session.add(new_user) app.db.session.commit() test_login_json('test', 'test', success=True) new_user.active = False app.db.session.commit() test_login_json('test', 'test', success=False)
def export_data(args): # MySql connections conn = mysql.connect(host=args.host, port=args.port, user=args.user, password=args.pwd, database=args.db, charset='utf8') conn2 = mysql.connect(host=args.host, port=args.port, user=args.user, password=args.pwd, database=args.db, charset='utf8') c = conn.cursor() ######################################################### # Postgresql - create session and clear and initialize DB engine = create_engine(settings.SQLALCHEMY_DATABASE_URI) Session = sessionmaker(bind=engine) model.Base.metadata.drop_all(bind=engine) # @UndefinedVariable model.Base.metadata.create_all(bind=engine) # @UndefinedVariable prepare_db(engine) session = Session(autoflush=False) ######################################################### # Codelists genre, language format load_model(c, session, 'select id,name from ebook_subject', model.Genre, {'name': 'name'}) load_model(c, session, 'select id, code, name from ebook_language', model.Language, { 'code': 'code', 'name': 'name' }) load_model(c, session, ' select id, mime_type, name, extension from ebook_format', model.Format, { 'mime_type': 'mime_type', 'name': 'name', 'extension': 'extension' }) #fix fb2 mimetype f = model.Format.query.filter_by(extension='fb2').one() f.mime_type = 'application/x-fictionbook+xml' # fix pdb mimetype f = model.Format.query.filter_by(extension='pdb').one() f.mime_type = 'application/x-aportisdoc' session.commit() print('Created genres, languages and formats') ############################################################################# # New user admin and standard roles now = datetime.now() parent = None for r in ['guest', 'user', 'trusted_user', 'superuser', 'admin']: role = model.Role(name=r, parent=parent) session.add(role) session.flush() parent = role admin = model.User(user_name="admin", password=hash_pwd("admin"), email="*****@*****.**", created=now, active=True) session.add(admin) admin.roles.append(role) session.commit() print("Created user roles") ################################################################################### def auditable_attrs(data): return { 'created_by': admin, 'modified_by': admin, 'created': data['created'], 'modified': data['modified'], 'id': data['id'] } # Authors no_author_id = None q = 'select id, created, modified, name, description from ebook_author' c.execute(q) for row in c: data = dict(zip(c.column_names, row)) first, last = split_name(data['name']) if last == '__NONE__': no_author_id = data['id'] print('No author id is %d' % no_author_id) continue o = model.Author(first_name=first, last_name=last, **auditable_attrs(data)) session.add(o) session.commit() print('Loaded Authors: %d records' % session.query(model.Author).count()) ################################################################## # Series q = 'select id, created, modified, title, rating from ebook_serie' c.execute(q) for row in c: data = dict(zip(c.column_names, row)) o = model.Series(title=data['title'], rating=data['rating'], **auditable_attrs(data)) session.add(o) session.commit() print('Loaded Series: %d records' % session.query(model.Series).count()) ######################################################################## # Ebooks q = 'select id, created, modified, title, rating, author_id, description, language_id, serie_id, serie_index from ebook_ebook' c.execute(q) count = 0 for row in c: if args.sample: if random.randint(1, args.sample) > 1: continue data = dict(zip(c.column_names, row)) language = session.query(model.Language).get(data['language_id']) series = session.query(model.Series).get( data['serie_id']) if data['serie_id'] else None primary_author = session.query(model.Author).get( data['author_id'] ) if data['author_id'] and data['author_id'] != no_author_id else None o = model.Ebook(title=data['title'], rating=data['rating'], description=data['description'], language=language, series=series, series_index=data['serie_index'], **auditable_attrs(data)) if primary_author: o.authors.append(primary_author) c2 = conn2.cursor() q2 = 'select author_id from ebook_ebook_other_authors where ebook_id=%s' c2.execute(q2, (data['id'], )) authors = flat(c2.fetchall()) if authors: authors = session.query(model.Author).filter( model.Author.id.in_(authors)).all() # @UndefinedVariable o.authors.extend(authors) q2 = 'select subject_id from ebook_ebook_subjects where ebook_id=%s' c2.execute(q2, (data['id'], )) genres = flat(c2.fetchall()) if genres: genres = session.query(model.Genre).filter( model.Genre.id.in_(genres)).all() # @UndefinedVariable o.genres.extend(genres) c2.close() #Set base directory with app.app_context(): logic.update_ebook_base_dir(o) session.add(o) count += 1 if not count % 1000: print('Loaded Ebook %d records\r' % count, end='') session.commit() session.commit() print('Loaded Ebook: %d records' % session.query(model.Ebook).count()) #################################################### # Sources q = 'select id, created, modified, ebook_id, location, format_id, size, crc, quality from ebook_source order by created desc' c.execute(q) col_names = c.column_names rows = c.fetchall( ) # need to fetch all to prevent timeout of connection later count = 0 for row in rows: data = dict(zip(col_names, row)) ebook = session.query(model.Ebook).get(data['ebook_id']) if not ebook: continue format = session.query(model.Format).get(data['format_id']) o = model.Source(ebook=ebook, format=format, location=data['location'], size=data['size'], hash=data['crc'], quality=data['quality'], **auditable_attrs(data)) #Copy files if args.dir: fname = os.path.join(args.dir, data['location']) with app.app_context(): location = logic.create_new_location(o, fname) o.location = location #Extract cover if not o.ebook.cover: try: cover = extract_cover(o) ebook.cover = cover except Exception: traceback.print_exc() session.add(o) count += 1 if not count % 1000: print('Loaded Source %d records\r' % count, end='') session.commit() session.commit() print('Loaded Source: %d records' % session.query(model.Source).count()) ########################################################################################## # Bookshelves q = 'select id, created, modified, name, description, public, rating from ebook_bookshelf' c.execute(q) for row in c: data = dict(zip(c.column_names, row)) o = model.Bookshelf(name=data['name'], description=data['description'], public=bool(data['public']), rating=data['rating'], **auditable_attrs(data)) session.add(o) session.commit() print('Loaded Bookshelf: %d records' % session.query(model.Bookshelf).count()) q = 'select id, created, modified, type, bookshelf_id, ebook_id, serie_id, `order`, note from ebook_bookshelfitem' c.execute(q) for row in c: data = dict(zip(c.column_names, row)) ebook = session.query(model.Ebook).get( data['ebook_id']) if data['ebook_id'] else None series = session.query(model.Series).get( data['serie_id']) if data['serie_id'] else None bookshelf = session.query(model.Bookshelf).get(data['bookshelf_id']) type = 'EBOOK' if data[ 'type'] == 1 else 'SERIES' if data['type'] == 2 else None if not type: print('Error invalid bookshelfitem type on %s' % str(row), file=sys.stderr) continue elif type == 'EBOOK' and not ebook: print( 'Error invalid bookshelfitem, type is ebook but no book - %s' % str(row), file=sys.stderr) continue elif type == 'SERIES' and not series: print( 'Error invalid bookshelfitem, type is series but no series - %s' % str(row), file=sys.stderr) continue o = model.BookshelfItem(type=type, ebook=ebook, series=series, bookshelf=bookshelf, order=data['order'], note=data['note'], **auditable_attrs(data)) session.add(o) session.commit() print('Loaded BookshelfItem: %d records' % session.query(model.BookshelfItem).count()) ############################################################################ for t in [ 'author', 'bookshelf', 'bookshelf_item', 'ebook', 'format', 'genre', 'language', 'series', 'source' ]: update_seq(engine, t) session.close() c.close() conn.close() conn2.close() return
def register(): # registration page # On the server side, check whether the username and password are valid or not. # username and password must be strings between 2 to 100 characters if request.method == "POST": username = request.form.get('username') password = request.form.get('password') confirm_password = request.form.get('confirm_password') context = { 'username_valid': 0, 'password_valid': 0, 'pawconfirm_valid': 0, 'username': username } flag = False if not 2 <= len(username) <= 100: context['username_valid'] = 1 flag = True if password != confirm_password: context['password_valid'] = 1 flag = True if not 2 <= len(password) <= 100: context['password_valid'] = 2 flag = True # users are not allowed to have same username dup_user = model.User.query.filter_by(username=username).first() if dup_user: context['username_valid'] = 2 flag = True if flag: return render_template('signup.html', **context) # Different users are allowed to have the same password # After using salt value for storing passwords, they will look completely different on the server(database) # even though they are the same password = generate_password_hash(password + username) candidate_user = model.User(username=username, password=password) db.session.add(candidate_user) db.session.commit() s3 = boto3.client('s3') s3.put_object(Bucket='chaoshuai', Body='', Key=username + '/') s3.put_object(Bucket='chaoshuai', Body='', Key=username + '/' + 'original/') s3.put_object(Bucket='chaoshuai', Body='', Key=username + '/' + 'processed/') # log in session['user'] = username return redirect(url_for('user')) context = { 'username_valid': -1, 'password_valid': -1, 'pawconfirm_valid': -1 } return render_template('signup.html', **context)