Example #1
0
def api_tweet():
    try:
        data = json.loads(
            request.data, object_hook=lambda x: defaultdict(lambda: None, x)
        )
    except JSONDecodeError:
        return error_response(["Invalid request."])

    tweet_text = data["text"] or ""
    tweet_text = tweet_text.strip().replace("\r\n", "\n")
    tweet_len = len(tweet_text)

    if tweet_len < 1:
        return error_response(["Your tweet must be at least one character."])

    if tweet_len > 120:
        return error_response(["Your tweet must be at most 120 characters."])

    image_id = data["imageId"]

    if image_id is not None:
        image_exists = Image.query.filter_by(id=image_id).count() > 0

        if not image_exists:
            return error_response(["Image not found"])

    tweet = Tweet(
        text=tweet_text, poster_id=int(current_user.get_id()), image_id=image_id
    )

    db.session.add(tweet)
    db.session.commit()

    return success_response(tweet.to_dict())
Example #2
0
def tweet_create():
    data = request.json or request.form
    if 'party' not in data or 'person' not in data or 'tweet' not in data:
        return bad_request('must include party & person & tweet')
    tweet = Tweet(party=data.get("party"),
                  person=data.get("person"),
                  tweet=data.get("tweet"))
    db.session.add(tweet)
    db.session.commit()
    return jsonify(tweet.to_dict())