def get(cls, uuid: str): source = SourceModel.find_by_uuid(uuid) if not source: return {"message": "Source not found."}, 404 return source_schema.dump(source)
def delete(cls, uuid: str): source = SourceModel.find_by_uuid(uuid) if not source: return {"message": "Source not found."}, 404 source.delete_from_db() return {"message": "Source deleted."}, 200
def process_post(cls, data): with app.app_context(): url = data["url"] source_id = data["source"] coordinates = data["latlong"] source = SourceModel.find_by_uuid(source_id) request = requests.get(url) html = BeautifulSoup(request.text, "html.parser") content = "" tagsText = "" tagsArray = [] title = html.select_one(source.title_selector) if source.subtitle_selector: subtitle = html.select_one(source.subtitle_selector) if source.content_selector: elements_body = html.select(source.content_selector) for el in elements_body: content += el.text + "\n\n" if source.tags_selector: elements_tags = html.select(source.tags_selector) for el in elements_tags: tagsArray.append(el.text.replace("\n", "")) tagsText = ",".join(tagsArray) if source.author_selector: author = html.select(source.author_selector) body = { "source_id": source_id, "state_id": source_id, "municipality_id": source_id, "title": title.text.replace("\n", ""), "subtitle": subtitle.text.replace("\n", ""), "location": { "type": "Point", "coordinates": coordinates }, "content": content, "tags": tagsText.upper(), "author": author.text.replace("\n", ""), "link": url, "timestamp": datetime.now(), } es.index(index="contents", body=body)
def put(self, source_id): data = Source.parser.parse_args() source = SourceModel.find_by_id(source_id) if source is None: source = SourceModel(data) else: source.name = data['name'] source.url = data['url'] source.save_to_db() return source.json(), 201
def put(cls, uuid: str): source = SourceModel.find_by_uuid(uuid) source_json = request.get_json() source_schema.load(source_json) if not source: return {"message": "Source not found."}, 404 source.name = source_json["name"] source.site = source_json["site"] source.title_selector = source_json["title_selector"] source.subtitle_selector = source_json["subtitle_selector"] source.content_selector = source_json["content_selector"] source.tags_selector = source_json["tags_selector"] source.author_selector = source_json["author_selector"] source.save_to_db() return {"message": "Source updated successfully"}, 200
def post(self): data = Source.parser.parse_args() if SourceModel.find_by_name(data.name): return { 'message': 'An source with name "{}" already exists.'.format(data.name) }, 400 source = SourceModel(data) try: source.save_to_db() except: return {"message": "An error occurred inserting the source."}, 500 return source.json(), 201
def get(cls): return {"sources": source_list_schema.dump(SourceModel.find_all())}
def delete(self, source_id): source = SourceModel.find_by_id(source_id) if source: source.delete_from_db() return {'message': 'Source deleted'}
def get(self, source_id): source = SourceModel.find_by_id(source_id) if source: return source.json() return {'message': 'Source not found'}, 404
def mutate(self, info, pointID, url, name=None): point, pointRoot = PointModel.getCurrentByRootKey(pointID) newPointVersion = point.update( user=info.context.current_user, sourcesToAdd=[SourceModel(parent=point.key, url=url, name=name)]) return AddSource(point=newPointVersion)