def signup(): check_thumb_in_request(files=request.files, form=request.form) new_user = User() new_user_dict = {**request.form} if 'thumb' in new_user_dict: del new_user_dict['thumb'] if 'publications' in new_user_dict: publications = json.loads(new_user_dict['publications']) new_user_dict['userPublications'] = [] for publication in publications: new_user_dict['userPublications'].append({ 'publication': publication, 'user': new_user }) thumb = read_thumb(files=request.files, form=request.form) check_thumb_quality(thumb) new_user.modify(new_user_dict) ApiHandler.save(new_user) save_thumb(new_user, thumb, 0, crop=get_crop(request.form)) login_user(new_user) stamp_session(new_user) return jsonify(as_dict(new_user, includes=USER_INCLUDES)), 201
def get_result( modelClass, includes=None, order_by=None, paginate=None, page=None, populate=None, query=None, refine=None, should_distinct=None, with_total_data_count=None ): if query is None: query = modelClass.query if issubclass(modelClass, SoftDeletableMixin): query = query.filter(modelClass.isSoftDeleted == False) if refine: query = refine(query) is_already_queried = False if order_by: check_order_by(order_by) if type(order_by).__name__ != 'function': query = query_with_order_by(query, order_by) else: elements = elements_by_with_computed_ranking(query, order_by) is_already_queried = True total_data_count = None if paginate or with_total_data_count: if is_already_queried: total_data_count = len(elements) else: total_data_count = query.count() if paginate: if page is not None: page = int(page) if is_already_queried: pagination = paginate_obj(elements, page, paginate) else: pagination = query.paginate(page, per_page=paginate, error_out=False)\ query = pagination.items data = [as_dict(obj, includes=includes) for obj in query] if populate: objects = list(map(populate, objects)) result = { "data": data } if paginate or with_total_data_count: result['total_data_count'] = total_data_count if paginate: result['has_more'] = total_data_count - page * paginate > 0 return result
def get_author_content(user_id): author_contents = AuthorContent.query.filter_by(userId=dehumanize(user_id)) return jsonify([ as_dict(author_content) for author_content in author_contents ])
def signup(): check_thumb_in_request(files=request.files, form=request.form) user_dict = dict({}, **request.form) if 'thumb' in user_dict: del user_dict['thumb'] publications = [] for index in [1, 2, 3]: publication_key = 'publication{}'.format(index) if publication_key in request.form: publication_url = request.form[publication_key] publication = Publication.query.filter_by(url=publication_url).first() if not publication: publication = Publication(url=publication_url) publications.append(publication) thumb = read_thumb(files=request.files, form=request.form) check_thumb_quality(thumb) new_user = User(**user_dict) ApiHandler.save(new_user, *publications) save_thumb(new_user, thumb, 0, crop=get_crop(request.form)) login_user(new_user) stamp_session(new_user) return jsonify(as_dict(new_user, includes=USER_INCLUDES)), 201
def edit_verdict(verdict_id): check_has_role(current_user, 'EDITOR') verdict = load_or_404(Verdict, verdict_id) verdict.modify(request.json) ApiHandler.save(verdict) return jsonify(as_dict(verdict, includes=VERDICT_INCLUDES)), 201
def signin(): json = request.get_json() identifier = json.get('identifier') password = json.get('password') user = get_user_with_credentials(identifier, password) login_user(user, remember=True) stamp_session(user) return jsonify(as_dict(user, includes=USER_INCLUDES)), 200
def create_verdict_user(): check_has_role(current_user, 'editor') verdict_user = VerdictUser() verdict_user.populate_from_dict(request.json) ApiHandler.save(verdict_user) return jsonify(as_dict(verdict_user)), 201
def create_verdict_reviewer(): check_has_role(current_user, 'EDITOR') verdict_reviewer = VerdictReviewer() verdict_reviewer.modify(request.json) ApiHandler.save(verdict_reviewer) return jsonify(as_dict(verdict_reviewer)), 201
def create_verdict(): check_has_role(current_user, 'EDITOR') verdict = Verdict() verdict.modify(request.json) verdict.user = current_user ApiHandler.save(verdict) return jsonify(as_dict(verdict, includes=VERDICT_INCLUDES)), 201
def create_verdict(): check_has_role(current_user, 'editor') verdict = Verdict() verdict.populate_from_dict(request.json) verdict.user = current_user ApiHandler.save(verdict) return jsonify(as_dict(verdict, includes=VERDICT_INCLUDES)), 201
def get_scrap(): try: content = content_from_url(request.args.get('url')) except ArticleException: api_errors = ApiErrors() api_errors.add_error('url', 'url is invalid') raise api_errors return jsonify(as_dict(content))
def create_organization(): content = dict(wikidata_from(request.json['name'])) organization = Organization.create_or_modify(content, 'entity') ApiHandler.save(organization) return jsonify(as_dict(organization)), 201
def claim_verdicts_from_airtable(verdicts_to_sync=None, max_verdicts=None, sync_async=False): if verdicts_to_sync is None: query = Verdict.query.filter(Verdict.scienceFeedbackUrl != None) if max_verdicts is not None: query = query.limit(max_verdicts) verdicts = query.all() else: verdicts = verdicts_to_sync if max_verdicts is not None: max_verdicts = len(verdicts) urls = [verdict.scienceFeedbackUrl for verdict in verdicts][:max_verdicts] if sync_async: claim_reviews = map_asynchronous(claim_review_from_url, urls) else: claim_reviews = [claim_review_from_url(url) for url in urls] for (index, verdict) in enumerate(verdicts): claim_review = claim_reviews[index] if not claim_review: continue for conclusion in claim_review['conclusions']: try: tag = Tag.create_or_modify({ '__SEARCH_BY__': ['label', 'type'], 'label': conclusion, 'type': TagType.CONCLUSION }) if tag.id is None: logger.info('Saving tag {}'.format(as_dict(tag))) ApiHandler.save(tag) verdict_tag = VerdictTag.create_or_modify({ '__SEARCH_BY__': ['tagId', 'verdictId'], 'tagId': humanize(tag.id), 'verdictId': humanize(verdict.id) }) verdict.verdictTags = verdict.verdictTags + [verdict_tag] except IntegrityError as e: logger.error('IntegrityError: {}, Conclusion: {}'.format( e, conclusion)) except InvalidRequestError as e: logger.error('InvalidRequestError: {}, Conclusion: {}'.format( e, conclusion)) except NotNullViolation as violation: logger.error('NotNullViolation: {}, Conclusion: {}'.format( violation, conclusion)) return verdicts
def get_tags(): query = Tag.query scopes = request.args.get('scopes') if scopes is not None: query = filter_tags_with_scopes(query, scopes.split(',')) tags = query.all() return jsonify([as_dict(tag, includes=TAG_INCLUDES) for tag in tags])
def modify_content(content_id): check_has_role(current_user, 'EDITOR') content = load_or_404(Content, content_id) content.modify(request.json) ApiHandler.save(content) return jsonify(as_dict(content, includes=CONTENT_INCLUDES)), 201
def soft_delete_article(article_id): check_has_role(current_user, 'editor') article = load_or_404(Article, article_id) article.soft_delete() ApiHandler.save(article) return jsonify(as_dict(article)), 201
def modify_article(article_id): check_has_role(current_user, 'editor') article = load_or_404(Article, article_id) article.modify(request.json) ApiHandler.save(article) return jsonify(as_dict(article, includes=ARTICLE_INCLUDES)), 201
def soft_delete_content(content_id): check_has_role(current_user, 'EDITOR') content = load_or_404(Content, content_id) content.soft_delete() ApiHandler.save(content) return jsonify(as_dict(content)), 201
def test_for_valid_id_humanized_synonym(self, app): # Given user = User(email="*****@*****.**", publicName="bar") # When ApiHandler.save(user) # Then user_dict = as_dict(user) humanized_id = humanize(user.user_id) assert user_dict['id'] == humanized_id
def post_role(): check_has_role(current_user, 'admin') user = load_or_404(User, request.json['userId']) role = Role() role.type = request.json['type'] role.user = user ApiHandler.save(role) return jsonify(as_dict(role)), 200
def edit_review(review_id): check_has_role(current_user, 'reviewer') review = load_or_404(Review, review_id) review.populate_from_dict(request.json) ApiHandler.save(review) save_tags(review, request.json.get('tagIds', [])) return jsonify(as_dict(review, includes=REVIEW_INCLUDES)), 201
def modify_review(review_id): check_has_role(current_user, 'REVIEWER') review = load_or_404(Review, review_id) review.modify(request.json) ApiHandler.save(review) save_tags(review, request.json.get('tagIds', [])) return jsonify(as_dict(review, includes=REVIEW_INCLUDES)), 201
def create_review(): check_has_role(current_user, 'REVIEWER') review = Review() review.modify(request.json) review.user = current_user ApiHandler.save(review) save_tags(review, request.json.get('tagIds', [])) return jsonify(as_dict(review, includes=REVIEW_INCLUDES)), 201
def get_tags(): query = Tag.query tag_type = request.args.get('type') if tag_type is not None: query = keep_tags_with_type(query, tag_type) scopes = request.args.get('scopes') if scopes is not None: query = keep_tags_with_scopes(query, scopes.split(',')) tags = query.all() return jsonify([as_dict(tag, includes=TAG_INCLUDES) for tag in tags])
def create_entity_from_row(entity_name): try: entity = entity_from_row_for( entity_name, request.json, request.json.get('index', request.json.get('airtableId'))) if entity: ApiHandler.save(entity) if entity.__class__ == Appearance: sync_content(entity.quotingContent) return jsonify(as_dict(entity)), 200 else: return jsonify({"error": "couldn't save the entity"}), 500 except Exception as e: logger.error(e) return jsonify({"exception": "couldn't complete your request"}), 500
def create_image(): check_has_role(current_user, 'reviewer') thumb = check_and_read_files_thumb(request.files) image = Image() image_dict = {"name": request.files['thumb'].filename} image.modify(image_dict) ApiHandler.save(image) save_thumb(image, thumb, 0) return jsonify(as_dict(image)), 201
def create_content(): check_has_role(current_user, 'EDITOR') content = content_from_url(request.json['url']) content.modify(**request.json) check_content_is_not_yet_saved(content) ApiHandler.save(content) # TODO: put it in a celery pipe subprocess.Popen( 'PYTHONPATH="." python scripts/manager.py screenshotmachine' + ' --url ' + str(content.url) + ' --id ' + str(content.id), shell=True, cwd=API_ROOT_PATH) return jsonify(as_dict(content, includes=CONTENT_INCLUDES)), 201
def create_article(): check_has_role(current_user, 'editor') content = dict(resolve_with_url(request.json['url']), **request.json) check_article_is_not_yet_saved(content) article = Article(**content) ApiHandler.save(article) # TODO: put it in a celery pipe p = subprocess.Popen( 'PYTHONPATH="." python scripts/manager.py screenshotmachine' + ' --url ' + str(article.url) + ' --id ' + str(article.id), shell=True, cwd=API_ROOT_PATH) return jsonify(as_dict(article, includes=ARTICLE_INCLUDES)), 201
def get_review(review_id): review = load_or_404(Review, review_id) return jsonify(as_dict(review, includes=REVIEW_INCLUDES))
def modify_profile(): current_user.populate_from_dict(request.json) ApiHandler.save(current_user) return jsonify(as_dict(current_user, includes=USER_INCLUDES)), 200