def upload(request): ''' 图片上传 ''' s = Sign(app_id, bucket, secert_id, secert_key).__repr__() if request.method == 'GET': return HttpResponse('ERROR') elif request.method == 'POST': url_signal = int(request.POST['type']) obj = request.FILES.get('image') url = urls[url_signal % 8] headers = { 'Host': 'recognition.image.myqcloud.com', "Authorization": s } files = { 'appid': (None, app_id), 'bucket': (None, bucket), 'image': (obj.name, obj.read(), obj.content_type) } # 附加参数 if url_signal % 8 == 0: files['card_type'] = (url_signal // 8 - 1) elif url_signal % 8 == 2: files['type'] = (url_signal // 8 - 1) # 备用参数 payload = None # 发送post请求 post_image = Post(url=url, files=files, payload=payload, headers=headers) r = post_image.send_image() responseinfo = r.content.decode('utf8') s = [] d = {} res = json2text(url_signal % 8, json.loads(responseinfo)) if type(res[0]) != type(d): s = res else: for k, v in res[0].items(): s.append('<b>' + k + '</b>' + ' : ' + v + '<br>') return HttpResponse(s) # 返回识别信息
def add_posts_to_db(): db.drop_all() db.create_all() for post in posts: db.session.add( Post(post["name"], post["max_votes"], post["applied_hostel"], post["help_text"])) db.session.commit()
def callback(ch, method, properties, body): print('Received in main application') data = json.loads(body) print(data) if properties.content_type == 'post_created': post = Post(id=data['id'], title=data['title'], image=data['image']) db.session.add(post) db.session.commit() print('Post created in main app') elif properties.content_type == 'post_updated': post = Post.query.get(data['id']) post.title = data['title'] post.image = data['image'] db.session.commit() print('Post updated in main app') elif properties.content_type == 'post_deleted': post = Post.query.get(data) db.session.delete(post) db.session.commit() print('Post deleted in main app')
def pabl(g_id,test): if test == '&test': print('test_content') test_content(g_id) else: get_all_content(g_id) ids = [] count = 0 for post in Post.select().where(Post.group_id == g_id): if post.id in ids: continue else: count += 1 ids.append(post.id) print(count) return template('templates/pabl.html', ids=ids, Comments=Comments, Pic=Pic, Post=Post, Doc=Doc)
def generate_posts(n, users, tags): for i in range(n): post = Post() post.title = faker.sentence() post.text = faker.text(max_nb_chars=1000) post.publish_date = faker.date_this_century(before_today=True, after_today=False) post.user_id = users[random.randrange(0, len(users))].id post.tags = [tags[random.randrange(0, len(tags))] for i in range(0, 2)] try: db.session.add(post) db.session.commit() except Exception as e: log.error("Fail to add post %s: %s" % (str(post), e)) db.session.rollback()
def generate_posts(n, users, tags): print(tags) for i in range(n): post = Post() post.title = fake.sentence() post.text = fake.text(max_nb_chars=1000) post.publish_date = fake.date_this_century(before_today=True, after_today=False) post.emp_id = random.randrange(1, 100) post.tags = [tags[random.randrange(0, len(tags))] for i in range(0, 2)] try: db.session.add(post) db.session.commit() except Exception as e: print(f" The exception is {e}")
def pabl(g_id, test): if test == '&test': print('test_content') test_content(g_id) else: get_all_content(g_id) ids = [] count = 0 for post in Post.select().where(Post.group_id == g_id): if post.id in ids: continue else: count += 1 ids.append(post.id) print(count) return template('templates/pabl.html', ids=ids, Comments=Comments, Pic=Pic, Post=Post, Doc=Doc)
import random import datetime from main import Tag, User, Post, db user = User.query.get(1) taglist = [Tag("Python"), Tag('Flask'), Tag("SQLAlchemy"), Tag('jinja')] s = "Example text" for i in range(100): new_post = Post("Post " + str(i)) new_post.user = user new_post.publish_date = datetime.datetime.now() new_post.text = s new_post.tags = random.sample(taglist, random.randint(1, 3)) db.session.add(new_post) db.session.commit()
from flask_script import Manager, Server from main import app, db, User, Post, Comment, Tag from flask_migrate import Migrate, MigrateCommand migrate = Migrate(app, db) manager = Manager(app) manager.add_command('server', Server()) manager.add_command('db', MigrateCommand) # db.drop_all() db.create_all() user = User(username='******', password='******') user.posts.append(Post('Post Title')) user.posts.append(Post('Second Title')) user.posts.append(Post('Third Title')) db.session.add(user) db.session.commit() @manager.shell def make_shell_context(): return dict(app=app, db=db, User=User, Post=Post, Comment=Comment, Tag=Tag) if __name__ == "__main__": manager.run()
# enter some content to the database # add first User user1 = User(first_name="Yannick", last_name="Haenggi", user_handle="yannickhaenggi", email="*****@*****.**", password="******", description="""I'm the first user of this Application""") db.session.add(user1) # commit session db.session.commit() # add post post1 = Post(post_content="I am the first user of this awesome Twitter duplicate", post_length=53, post_time=datetime.datetime.now(), user_id=1) db.session.add_all([user1, post1]) # commit session db.session.commit() # check whether both sessions were entered correctly print(user1) print(post1) # different commands for querying the sql database # all contents all_posts = Post.query.all() all_posts
#!/usr/bin/env python #-*- coding:utf-8 -*- import random import datetime from main import app, db, User, Post, Comment, Tag tag_one = Tag('Python') tag_two = Tag('Flask') tag_three = Tag('SQLAlechemy') tag_four = Tag('Jinja') tag_list = [tag_one, tag_two, tag_three, tag_four] s = "Example text Example text Example text Example text Example text Example text Example text Example text Example text " for i in xrange(100): new_post = Post("port " + str(i)) new_post.user_id = random.randint(1, 4) new_post.publish_date = datetime.datetime.now() new_post.text = s new_post.tags = random.sample(tag_list, random.randint(1, 3)) db.session.add(new_post) db.session.commit()