def new_post(): title = request.form["title"] url = request.form["url"] text = request.form["text"] time = datetime.datetime.now() user = decode_auth_token(request.cookies.get('token')) if user is None: error = "ERROR: You must be logged to make a new post" return redirect("submit?error={0}".format(error)) elif url != '' and text == '' and title != '' and not Contribution.exists(repository, url): contribution = Contribution(title, url, None, time, user, ContributionTypes.NEW.value, 0) elif text != '' and url == '' and title != '': contribution = Contribution(title, None, text, time, user, ContributionTypes.ASK.value, 0) elif text != '' and url != '': error = "ERROR: You can only fill URL or Text but not both" return redirect("submit?error={0}".format(error)) elif url != '' and text == '' and Contribution.exists(repository, url): contribution_id = Contribution.get_contribution_id_by_URL(repository, url) return redirect("contribution?id={0}".format(contribution_id)) elif text == '' and url == '' and title != '': error = "ERROR: You have to fill either URL or Text" return redirect("submit?error={0}".format(error)) else: error = "ERROR: You must fill title" return redirect("submit?error={0}".format(error)) contribution.save(repository) return redirect('')
def create_new_new(): if 'Authorization' not in request.headers: return jsonify('Unauthorized'), 401 username = decode_auth_token(request.headers['Authorization']) if username is None: return jsonify(''), 401 json = request.get_json() new = Contribution(title=json['title'], url=json['url'], text=None, time=datetime.datetime.now(), username=username, kind=ContributionTypes.NEW.value) if Contribution.exists(repository, new.url): new = Contribution.get_contribution_by_url(repository, new.url) return jsonify(new.toJSON()), 409 new.save(repository) return jsonify(new.toJSON())