def all(username): weibo_list = Blog.find_all(username=username) if weibo_list is not None: weibos = Blog.all_json(weibo_list) else: weibos = None return jsonify(weibos)
async def save_content(*, id, title, content, request): if not title or not title.strip(): raise APIValueError('title') if not content or not content.strip(): raise APIValueError('content') session = await get_session(request) uid = session.get('uid') if not uid: return {'success': False, 'msg': 'please login.'} if id == '0': blog = Blog(user_id=uid, user_name='', user_image='', name=title, summary='', content=content) await blog.save() return {'success': True, 'blog_id': blog.id} else: blogs = await Blog.findAll('id=?', [id]) if len(blogs) != 1: raise APIError('update:failed', 'id', 'id is not find.') blog = blogs[0] blog.name = title blog.content = content await blog.update() return {'success': True, 'blog_id': blog.id}
def _user_has_account(self): blog = Database.find_one("blogs", {"author": self.user}) if blog is not None: self.user_blog = Blog.from_mongo(blog["id"]) return True else: return False
def _user_has_account(self): blog = Database.find_one('blogs', {'author': self.user}) if blog is not None: self.user_blog = Blog.from_mongo(blog['id']) return True else: return False
def add(): form = request.get_json() form['user_id'] = current_user().id form['username'] = current_user().username b = Blog.new(form) response = b.json() response['redirect'] = url_for('blog_view.index') return jsonify(response)
def _view_blog(self): blog_to_see = input("Enter the ID of the blog you'd like to read: ") blog = Blog.from_mongo(blog_to_see) posts = blog.get_posts() for post in posts: print("Date: {}, title: {}\n\n{}".format(post['created_date'], post['title'], post['content']))
def _view_blog(self): blog_to_see = input("Enter ID of your blog you like it to read") blog = Blog.get_from_mongo(blog_to_see) posts = blog.get_posts() for post in posts: print("Date : {}, Tittle: {}\n\n{}".format(post['date'], post['tittle'], post['content']))
def delete(): blog_id = int(request.args.get('id')) b = Blog.remove(blog_id) blogComments = BlogComment.find_all(blog_id=blog_id) for blogComment in blogComments: BlogComment.remove(blogComment.id) return jsonify(b.json())
def _user_has_account(self): datainfoblog = Database.find_one( 'blogs', {'author': self.user }) # Check in database if user exists, and throws a boolean if datainfoblog is not None: self.user_blog = Blog.get_from_mongo(datainfoblog['id']) return True else: return False
def _view_blog(self): blog_to_see = raw_input( "Enter the ID of the blog you'd like to read: ") blog = Blog.get_from_mongo(blog_to_see) print 'Blog returned:', blog posts = blog.get_posts() for post in posts: print 'Date: ', post['created_date'] print 'Title: ', post['title'] print '' print post['content']
def get(self): parser = reqparse.RequestParser() parser.add_argument('name', type=str, help='Common Name of the plant you want to search') args = parser.parse_args() try: plant = json.loads(Plant.objects.get(name=args['name']).to_json()) plantings = json.loads( Planting.objects(plant_name=args['name']).to_json()) seen_plantings = dict() for planting in plantings: gname = planting['garden_name'] if gname in seen_plantings: if 'pictureURL' not in seen_plantings[ gname] and 'pictureURL' in planting: seen_plantings[gname]['pictureURL'] = planting[ 'pictureURL'] else: seen_plantings[gname] = planting try: seen_plantings[gname]['garden'] = json.loads( Garden.objects.get(name=gname).to_json()) except: del seen_plantings[gname] final_plantings = [] for key, value in seen_plantings.items(): final_plantings.append(value) blogs = json.loads( Blog.objects(tags__contains=args['name']).to_json()) final_blogs = [] for blog in blogs: if "date" in blog: blog["date"] = blog["date"]['$date'] if "username" in blog: blog["user"] = json.loads( User.objects.get(username=blog["username"]).to_json()) final_blogs.append(blog) plant['blogs'] = final_blogs plant['plantings'] = final_plantings except Exception as e: print(e) return {} return plant
async def api_create_blog(request, *, name, summary, content): check_admin(request) if not name or not name.strip(): raise APIValueError('name', 'name cannot be empty.') if not summary or not summary.strip(): raise APIValueError('summary', 'summary cannot be empty.') if not content or not content.strip(): raise APIValueError('content', 'content cannot be empty.') blog = Blog(user_id=request.__user__.id, user_name=request.__user__.name, user_image=request.__user__.image, name=name.strip(), summary=summary.strip(), content=content.strip()) await blog.save() return blog
def get(self): parser = reqparse.RequestParser() parser.add_argument('username', required=True, type=str, help='The username of the User') args = parser.parse_args() try: seen_plants = dict() favourite_plant_count = dict() user = json.loads(User.objects.get(username=args['username']).to_json()) gardens = json.loads(Garden.objects(username=args['username']).to_json()) for garden in gardens: plantings = json.loads(Planting.objects(garden_name=garden['name']).to_json()) for planting in plantings: if planting['plant_name'] in seen_plants: planting['plant'] = seen_plants[planting['plant_name']] favourite_plant_count[planting['plant_name']] += 1 else: plant = json.loads(Plant.objects.get(name=planting['plant_name']).to_json()) planting['plant'] = plant seen_plants[planting['plant_name']] = plant favourite_plant_count[planting['plant_name']] = 1 garden['plantings'] = plantings favourite_plants = [] for key, value in favourite_plant_count.items(): favourite_plants.append({"plant": seen_plants[key], "count": value}) blogs = json.loads(Blog.objects(username=args['username']).to_json()) user['favourite_plants'] = favourite_plants user['blogs'] = blogs user['gardens'] = gardens except Exception as e: print(e) return {} return user
def decorated_function(*args, **kwargs): u = current_user() model_name = request.path.split('/')[-2] # 根据不同请求方法,获取实例的 id if request.method == 'POST': data = request.json id = int(data.get('id')) else: id = int(request.args.get('id')) # 根据 model 的名字,判断从哪里获取实例 path_dict = dict( weibo=Weibo.find_by(id=id), comment=Comment.find_by(id=id), blog=Blog.find_by(id=id), blogComment=BlogComment.find_by(id=id), ) instance = path_dict.get(model_name) user_id = instance.user_id # 检查登录用户 id 与实例的 user_id 是否一致 if u.id == user_id: return f(*args, **kwargs) else: log("{} try to access {}'s data\r\n".format(u.username, instance.username)) return redirect('#')
def _prompt_user_for_account(self): title = input("Enter the blog title: ") description = input("Enter blog description: ") blog = Blog(author=self.user, title=title, description=description) blog.save_to_mongo() self.user_blog = blog
def detail(): blog_id = int(request.args.get('id')) b = Blog.find_by(id=blog_id) return jsonify(b.json())
def _prompt_user_for_account(self): title = raw_input("Enter blog title: ") description = raw_input("Enter blog description: ") blog = Blog(self.user, title, description) blog.save_to_mongo() self.user_blog = blog
def update(): form = request.json blog_id = int(form.get('id', -1)) if len(form) > 0: b = Blog.update(blog_id, form) return jsonify(b.json())