def create_post(user_id, lat, lon, body, artist, img_url="null"): p = Post(user_id, lat, lon, body, img_url) db_session.add(p) # make sure we update our database is updated with the id db_session.flush() db_session.commit() return {'success': 1, 'id': p.id, 'msg': 'success'}
def publish() : if not session.get('is_admin'): return redirect(url_for('admin.login')) article_id = int(request.args.get('articleId', 0)) if not article_id : return abort(404) draft = db_session.query(Draft).filter(Draft.article_id == article_id).first() article = db_session.query(Article).filter(Article.id == article_id).first() if draft : if not article : article = Article(draft.user_id, draft.user_name, draft.title, draft.content) article.id = draft.article_id db_session.add(article) else : article.title = draft.title article.content = draft.content db_session.delete(draft) db_session.flush() return redirect('/detail?articleId=' + str(article_id))
def save() : userid = session.get('userid') username = session.get('username') if not userid or not username or not session.get('is_admin'): return redirect(url_for('admin.login')) title = request.form.get('title') content = request.form.get('content') if not title or not content : tag_chk_list = get_article_taglist(0) return render_template('admin/publish.html', active = 'publish', article = None, tag_list = tag_chk_list) ## 去掉HTML的背景颜色,防止和现有CSS的背景颜色冲突 content = htmlHelper.purge_background(content) ## 向数据库添加一篇文章 article = Article(userid, username, '', ''); db_session.add(article) db_session.flush(); draft = Draft(article.id, userid, username, title, content) db_session.add(draft) db_session.flush(); ## 向数据库添加文章标签 tag_list = request.form.getlist('tags') dao.save_tags(article.id, tag_list) return redirect('/admin/draft?draftId=%d' % draft.id)
def create_product_from_data(self): """ Creates product and required objects it depends on from data. """ # Normalize field names to system names. self._cleanse_data() # create independent objects first: tags, group (family, range), customer as they do not need # anything to exist. self._create_objects(self.product_config[INDEPENDENT]) # create product dependencies in database (ids will be needed to save the product object in next step) db_session.flush() base_product = self._create_base_product() # now create product dependent objects (those need product_id. self._create_objects(self.product_config[PRODUCT_DEPENDENT]) # create industry specific dependent objects. multi_relations = self.product_config[MULTI_RELATIONS] if self.product_config.get(INDUSTRY_DEPENDENT): industry_dependent_relations = self.product_config[INDUSTRY_DEPENDENT] self._create_objects(industry_dependent_relations) multi_relations.extend(industry_dependent_relations) # append dependent objects to product for relation in multi_relations: # add objects to appropriate fields getattr(base_product, relation).extend(self.objects[relation]) db_session.commit()
def callback() : code = request.args.get('code', None) if code : token = social.get_access_token(code) access_token = token.get('access_token') if access_token : baidu_user = social.get_user_info(access_token) name = baidu_user.get('username') media_type = baidu_user.get('media_type') head_url = baidu_user.get('tinyurl') user = User.find_user(name, media_type) if not user and name : user = User(name, head_url = head_url, media_type = media_type) db_session.add(user) db_session.flush(); if user : session['userid'] = user.id session['username'] = user.name session['is_admin'] = user.admin return redirect('/')
def comment(): article_id = request.form.get('articleId') or 0 if not article_id: return jsonify(state=False, message='文章不存在') user_id = session.get('userid') user_name = session.get('username') to_name = request.form.get('toName') or None content = request.form.get('content') or None if not content: return jsonify(state=False, message='评论内容不能为空') if not user_name or not user_id: return jsonify(state=False, message='用户未登录') user = db_session.query(User).filter(User.id == user_id).first() ## 向数据库添加一条评论 c = Comment(article_id, user.id, user.name, user.head_url, to_name, content) db_session.add(c) db_session.flush() article_url = 'http://hamilton.duapp.com/detail?articleId=' + str( article_id) bmq.send_mail(['*****@*****.**'], 'hamilton上的新回复', '<!--HTML-->\n您有一条新回复需要处理<br/>点击查看:<a href="' + article_url + '">' + article_url + '</a>') return jsonify(state=True, message='success')
def save(): userid = session.get('userid') username = session.get('username') if not userid or not username or not session.get('is_admin'): return redirect(url_for('admin.login')) title = request.form.get('title') content = request.form.get('content') if not title or not content: tag_chk_list = get_article_taglist(0) return render_template('admin/publish.html', active='publish', article=None, tag_list=tag_chk_list) ## 去掉HTML的背景颜色,防止和现有CSS的背景颜色冲突 content = htmlHelper.purge_background(content) ## 向数据库添加一篇文章 article = Article(userid, username, '', '') db_session.add(article) db_session.flush() draft = Draft(article.id, userid, username, title, content) db_session.add(draft) db_session.flush() ## 向数据库添加文章标签 tag_list = request.form.getlist('tags') dao.save_tags(article.id, tag_list) return redirect('/admin/draft?draftId=%d' % draft.id)
def publish(): if not session.get('is_admin'): return redirect(url_for('admin.login')) article_id = int(request.args.get('articleId', 0)) if not article_id: return abort(404) draft = db_session.query(Draft).filter( Draft.article_id == article_id).first() article = db_session.query(Article).filter( Article.id == article_id).first() if draft: if not article: article = Article(draft.user_id, draft.user_name, draft.title, draft.content) article.id = draft.article_id db_session.add(article) else: article.title = draft.title article.content = draft.content db_session.delete(draft) db_session.flush() return redirect('/detail?articleId=' + str(article_id))
def comment() : article_id = request.form.get('articleId') or 0 if not article_id : return jsonify(state=False, message='文章不存在') user_id = session.get('userid') user_name = session.get('username') to_name = request.form.get('toName') or None content = request.form.get('content') or None if not content : return jsonify(state=False, message='评论内容不能为空') if not user_name or not user_id : return jsonify(state=False, message='用户未登录') user = db_session.query(User).filter(User.id == user_id).first() ## 向数据库添加一条评论 c = Comment(article_id, user.id, user.name, user.head_url, to_name, content) db_session.add(c) db_session.flush() article_url = 'http://hamilton.duapp.com/detail?articleId=' + str(article_id) bmq.send_mail(['*****@*****.**'], 'hamilton上的新回复', '<!--HTML-->\n您有一条新回复需要处理<br/>点击查看:<a href="' + article_url + '">' + article_url + '</a>') return jsonify(state=True, message='success')
def save_tags(article_id, tag_list) : if not tag_list : return None for tag_id in tag_list : tag_name = db_session.query(Tags.name).filter(Tags.id == tag_id).first()[0] db_session.add(TagedArticle(tag_id, article_id, tag_name)) db_session.flush()
def create_follow(from_id, to_id): # assume my_id and friend_id are valid u = Follow(from_id, to_id) db_session.add(u) db_session.flush() db_session.commit() return { 'success': 1, 'from_id': u.from_id, 'to_id': u.u_id, 'msg': 'success' } #print(create_post(1, 1000, 100, "dsf"))
def create_user(username, password, first, last, img_url="null"): if len(password) > 16: return { 'success': 0, 'id': 'null', 'msg': 'password is too long, make less than 16' } u = User(username, password, first, last, img_url) # make sure it is not duplicate if db_session.query(User).filter(User.username == username).first(): return {'success': 0, 'id': 'null', 'msg': 'user already exist'} db_session.add(u) # make sure we update our database is updated with the id db_session.flush() db_session.commit() return {'success': 1, 'id': u.id, 'msg': 'success'}
def edit(): userid = session.get('userid') username = session.get('username') if not userid or not username or not session.get('is_admin'): return redirect(url_for('admin.login')) article_id = int(request.args.get('articleId', 0)) title = request.form.get('title') content = request.form.get('content') if not article_id: return abort(404) ## 从数据库取出博客内容 draft = db_session.query(Draft).filter( Draft.article_id == article_id).first() if not draft: article = db_session.query(Article).filter( Article.id == article_id).first() if not article: return abort(404) draft = Draft(article.id, article.user_id, article.user_name, article.title, article.content) db_session.add(draft) db_session.flush() ## 浏览器发送的表单里没有数据,则把编辑页面发送给用户 if not title or not content: tag_list = get_article_taglist(article_id) return render_template('admin/publish.html', active='publish', article=draft, tag_list=tag_list) ## 表单里有数据,需要更新数据库 draft.title = title draft.content = htmlHelper.purge_background(content) db_session.flush() ## 更新文章的标签表 tag_list = request.form.getlist('tags') dao.update_tags(article_id, tag_list) return redirect('/admin/draft?draftId=%d' % draft.id)
def _create_base_product(self): """ Creates base product without relationships that require its existence formerly. Needs objects from INDEPENDENT object names to exist first. Product is then flushed to the database but not committed. :return: product, instance of a product """ product_class = get_class_by_table_name(f'{self.product_type}_product') if product_class: # build kwargs for product product_kwargs = {} for field in self.product_config[BASE_FIELDS]: if field in self.product_config[SINGLE_RELATIONS]: product_kwargs[field] = self.objects[field] else: product_kwargs[field] = self.data[field] product, _ = get_or_create(product_class, **product_kwargs) db_session.add(product) db_session.flush() self.objects['product_id'] = product.id return product raise ValueError('Unknown product class')
def edit() : userid = session.get('userid') username = session.get('username') if not userid or not username or not session.get('is_admin'): return redirect(url_for('admin.login')) article_id = int(request.args.get('articleId', 0)) title = request.form.get('title') content = request.form.get('content') if not article_id : return abort(404) ## 从数据库取出博客内容 draft = db_session.query(Draft).filter(Draft.article_id == article_id).first() if not draft : article = db_session.query(Article).filter(Article.id == article_id).first() if not article : return abort(404) draft = Draft(article.id, article.user_id, article.user_name, article.title, article.content) db_session.add(draft) db_session.flush(); ## 浏览器发送的表单里没有数据,则把编辑页面发送给用户 if not title or not content : tag_list = get_article_taglist(article_id) return render_template('admin/publish.html', active = 'publish', article=draft, tag_list = tag_list) ## 表单里有数据,需要更新数据库 draft.title = title draft.content = htmlHelper.purge_background(content) db_session.flush() ## 更新文章的标签表 tag_list = request.form.getlist('tags') dao.update_tags(article_id, tag_list) return redirect('/admin/draft?draftId=%d' % draft.id)