def get(self): reqdata = NewsList.parser.parse_args() if reqdata['search_key'] is None: return [n.json() for n in NewsModel.find_all()] found_news = NewsModel.find_any(reqdata['search_key']) authors_news = [] for author in AuthorModel.find_by_name(reqdata['search_key']): authors_news += NewsModel.find_by_author(author.id) for news in authors_news: if news in found_news: found_news.remove(found_news.index(news)) return [n.json() for n in found_news + authors_news]
def delete(self, news_id): news_obj = NewsModel.find_by_id(news_id) if news_obj is not None: news_obj.delete_from_db() return {'msg': 'News deleted'} return {'message': 'News not found.'}, 404
def put(cls, news_id): news = NewsModel.find_by_id(news_id) if not news: raise ResourceNotFound fields_json = request.get_json() news.update(fields_json) return {"success": True, "data": news_schema.dump(news)}, 200
def post(cls, news_id): str_time = datetime.utcnow().strftime('%Y%m%d%H%M%S%f')[:-3] f = request.files['image'] f_name = str_time + secure_filename(f.filename) file_name = os.path.join("./static/" + f_name) f.save(file_name) news = NewsModel.find_by_id(news_id) news.img_url = file_name news.save_to_db() return {"success": True, "data": file_name}, 200
def put(self, news_id): reqdata = NewsResource.parser.parse_args() news_obj = NewsModel.find_by_id(news_id) if news_obj is not None: if len(reqdata['title']): news_obj.title = reqdata['title'] if len(reqdata['content']): news_obj.content = reqdata['content'] if len(reqdata['author_id']): news_obj.author_id = reqdata['author_id'] news_obj.save_to_db() return {'msg': 'News updated.'} return {'message': 'News not found.'}, 404
def get(cls): user_id = get_jwt_identity() if user_id != 1: raise NotAuthorized users = UserModel.get_list([]) users = user_schema.dump(users) pacients = PacientModel.get_list([]) pacients = pacient_schema.dump(pacients) clinical_stories = ClinicalStoryModel.get_list([]) clinical_stories = clinical_story_schema.dump(clinical_stories) news = NewsModel.get_list([]) news = news_schema.dump(news) data = { "users": users, "pacients": pacients, "clinical_stories": clinical_stories, "news": news } return {"success": True, "data": data}, 200
def post(self): reqdata = NewsResource.parser.parse_args() news = NewsModel(reqdata['title'], reqdata['content'], reqdata['author_id']) news.save_to_db() return news.json(), 201
def get(cls): news = NewsModel.get_list() return {"success": True, "data": news_schema_list.dump(news)}, 200