def get_image_info(file): ''' Gets details about an uploaded image file :param file: A file object :return: An image object which describes the image file ''' image = Image() file.seek(0, 2) image.file_size = file.tell() file.seek(0, 0) image.filename = secure_filename(file.filename) image.mime_type = get_content_type(image.filename) return image
def init_database(): database.drop_all() database.create_all() for i in range(2): database.session.add(User('zyxzjs'+str(i),'lay'+str(i),'','issing'+str(i)+'@163.com')) database.session.commit() for i in range(3): url=str(random.randint(1,6)) + '.jpg' database.session.add(Image(url,1)) for j in range(3): database.session.add(Comment('Issing'+str(i)+str(j),1+i*2,2)) url =str(random.randint(1,6)) + '.jpg' database.session.add(Image(url, 2)) for j in range(3): database.session.add(Comment('xback'+str(i)+str(j),(i+1)*2,1)) database.session.commit()
def add(): if not session.get('logged_in'): return render_template('login.html') if str(request.method) == 'POST': try: vals = request.form.to_dict() files = request.files.getlist("image") for i in range(0, len(files)): file = files[i] ext = file.filename.rsplit('.', 1)[1].lower() if ext in ['png', 'jpg', 'jpeg']: filename = "/static/images/" + base64.urlsafe_b64encode(file.filename) + "." + ext file.save("." + filename) if i == 0: product = Product(vals['name'], vals['description'], filename, 1, 0) db.session.add(product) db.session.commit() db.session.close() img = Image(vals['name'], filename, i) db.session.add(img) db.session.commit() db.session.close() except Exception as err: db.session.rollback() return err.message return render_template('add_product.html')
def upload(): file = request.files['file'] file_ext = '' if file.filename.find('.') > 0: file_ext = file.filename.rsplit('.', 1)[1].strip().lower() if file_ext in app.config['ALLOWED_EXT']: file_name = str(uuid.uuid1()).replace('-', '') + '.' + file_ext url = save_to_local(file, file_name) if url != None: database.session.add(Image(url, current_user.id)) database.session.commit() return redirect('/profile/%d' % current_user.id)
def init_database(): db.drop_all() db.create_all() for i in range(0, 100): db.session.add(User('User' + str(i + 1), 'p' + str(i))) for j in range(0, 10): db.session.add(Image(get_url(), i + 1)) for k in range(0, 5): db.session.add( Comment('This is comment test' + str(k), 10 * i + j + 1, i + 1)) db.session.commit()
def init_database(): db.drop_all() db.create_all() for i in xrange(100): db.session.add(User('User'+str(i+1), 'password'+str(i))) for j in xrange(10): db.session.add(Image(get_image_url(), i+1)) for k in xrange(3): db.session.add(Comment("This is a comment"+str(k+1), 1+10*i+j, i+1)) db.session.commit() image = Image.query.get(3) print image.user
import yaml with open("db.yml") as db_file: db_entries = yaml.safe_load(db_file) db.create_all() for user in db_entries["users"]: usr = User(user["username"], user["password_hash"]) db.session.add(usr) db.session.commit() for project in db_entries["projects"]: proj = Product(project["name"], project["description"], project["default_image"], 1, 0) db.session.add(proj) db.session.commit() for i in range(0, len(project["images"])): img = Image(project['name'], project["images"][i], i) db.session.add(img) db.session.commit() db.session.close() except Exception as err: errors.append(err.message) # EB looks for an 'application' callable by default. application = Flask(__name__) # config application.config.update( DEBUG = True, SECRET_KEY = os.urandom(24) )
def init_database(): db.drop_all() db.create_all() # 添加用户 for i in range(0, 100): db.session.add( User('User' + str(i), 'a' + str(i), '.'.join(sample('0123456789asdfghjklqwertyuiopzxcvbnm', 10)), get_image_url())) for j in range(0, 5): db.session.add(Image(get_image_url(), i + 1)) for k in range(0, 3): db.session.add( Comment('This is a comment' + str(k), 1 + 1 * i + j, i + 1)) db.session.commit() # 修改两种方式 for i in range(50, 100, 2): user = User.query.get(i) user.username = '******' + user.username db.session.commit() User.query.filter_by(id=51).update({'username': '******'}) db.session.commit() # 删除 for i in range(50, 100, 2): comment = Comment.query.get(i + 1) db.session.delete(comment) db.session.commit() # 另一种直接使用delete()方法 # 查询 print(1) print(User.query.all()) print(2) print(User.query.get(3)) print(3) print(User.query.filter_by(id=5).first()) print(4) print(User.query.order_by(User.id.desc()).offset(1).limit(2).all()) print(5) print(User.query.filter(User.username.endswith('0')).limit(3).all()) print(6) print(User.query.filter(or_(User.id == 88, User.id == 99)).all()) print(7) print(User.query.filter(and_(User.id > 88, User.id < 99)).all()) print(8) print(User.query.filter(and_(User.id > 88, User.id < 99)).first_or_404()) print(9) print( User.query.order_by(User.id.desc()).paginate(page=1, per_page=10).items) user = User.query.get(1) # 从1开始,第1个用户 print(10) print(user.images.all()) image = Image.query.get(1) print(11) print(image.user)
def get(self, key_id, filename): i = Image.get_by_id(int(key_id)) if not i: self.error(404) self.response.headers['Content-Type'] = i.mimetype self.response.out.write(i.image)