delete twitter account and campaign
This commit is contained in:
parent
12a946c88a
commit
9d95c8a411
@ -2,14 +2,13 @@ import requests
|
||||
import json
|
||||
from requests_oauthlib import OAuth1Session
|
||||
from flask import Blueprint, current_app, request, jsonify
|
||||
from rsa import verify
|
||||
from ayct_backend.twitter.models import *
|
||||
from ayct_backend.campaign.models import *
|
||||
from ayct_backend.firebase import verify_token
|
||||
|
||||
campaign = Blueprint('campaign', __name__)
|
||||
|
||||
@campaign.route('/campaign', methods=['GET'])
|
||||
@campaign.route('/', methods=['GET'])
|
||||
def get_twitter_campaigns():
|
||||
decoded_token = verify_token(request.headers)
|
||||
if not decoded_token:
|
||||
@ -23,19 +22,17 @@ def get_twitter_campaigns():
|
||||
|
||||
for campaign in twitter_campaigns:
|
||||
campaign_data = {}
|
||||
campaign_data['campaign_id'] = campaign.id
|
||||
campaign_data['campaign_name'] = campaign.campaign_name
|
||||
campaign_data['twitter_account_id'] = campaign.twitter_account_id
|
||||
campaign_data['user_input'] = campaign.user_input
|
||||
campaign_data['generated_content'] = campaign.generated_content
|
||||
campaign_data['twitter_post_id'] = campaign.twitter_post_id
|
||||
campaigns.append(campaign_data)
|
||||
|
||||
return jsonify({
|
||||
"twitter_campaigns": campaigns
|
||||
}), 200
|
||||
|
||||
@campaign.route('/campaign', methods=['POST'])
|
||||
def add_twitter_account():
|
||||
@campaign.route('/', methods=['POST'])
|
||||
def add_twitter_campaign():
|
||||
decoded_token = verify_token(request.headers)
|
||||
if not decoded_token:
|
||||
return "Not authorised!", 401
|
||||
@ -43,11 +40,13 @@ def add_twitter_account():
|
||||
user_id = decoded_token['sub']
|
||||
|
||||
content_type = request.headers.get('Content-Type')
|
||||
if (content_type == 'application/json'):
|
||||
if content_type != 'application/json':
|
||||
return "Content-type not supported!", 400
|
||||
|
||||
request_json = request.json
|
||||
if not request_json['campaign_name'] or not request_json['user_input'] or not request_json['twitter_account_id']:
|
||||
return "Invalid request!", 400
|
||||
else:
|
||||
|
||||
consumer_key = current_app.config["TWITTER_CONSUMER_KEY"]
|
||||
consumer_secret = current_app.config["TWITTER_CONSUMER_SECERT"]
|
||||
|
||||
@ -86,18 +85,87 @@ def add_twitter_account():
|
||||
).json()['data']
|
||||
|
||||
# save campaign to database
|
||||
new_twitter_campaign = TwitterCampaign(
|
||||
campaign_name = request_json['campaign_name'],
|
||||
twitter_campaign = TwitterCampaign(
|
||||
user_id = user_id,
|
||||
campaign_name = request_json['campaign_name'],
|
||||
twitter_account_id = request_json['twitter_account_id'],
|
||||
user_input = request_json['user_input'],
|
||||
generated_content = tweet_text,
|
||||
)
|
||||
|
||||
campaign_db.session.add(twitter_campaign)
|
||||
campaign_db.session.flush()
|
||||
campaign_db.session.refresh()
|
||||
|
||||
twitter_campaign_post = TwitterPost(
|
||||
campaign_id = twitter_campaign.id,
|
||||
user_id = user_id,
|
||||
post_content = tweet_text,
|
||||
twitter_post_id = response['id']
|
||||
)
|
||||
|
||||
campaign_db.session.add(new_twitter_campaign)
|
||||
campaign_db.session.add(twitter_campaign_post)
|
||||
campaign_db.session.commit()
|
||||
|
||||
return "Campaign succesfully created.", 201
|
||||
else:
|
||||
|
||||
@campaign.route('/', methods=['DELETE'])
|
||||
def delete_twitter_campaign():
|
||||
decoded_token = verify_token(request.headers)
|
||||
if not decoded_token:
|
||||
return "Not authorised!", 401
|
||||
|
||||
user_id = decoded_token['sub']
|
||||
|
||||
content_type = request.headers.get('Content-Type')
|
||||
if content_type != 'application/json':
|
||||
return "Content-type not supported!", 400
|
||||
|
||||
request_json = request.json
|
||||
if not request_json['campaign_id']:
|
||||
return "Invalid request!", 400
|
||||
|
||||
twitter_campaign = TwitterAccount.query.filter_by(user_id=user_id, id=request_json['campaign_id'])
|
||||
|
||||
if not twitter_campaign:
|
||||
return "Account not found!", 404
|
||||
|
||||
twitter_db.session.delete(twitter_campaign)
|
||||
twitter_db.session.commit()
|
||||
|
||||
return "Twitter campaign succesfully deleted.", 200
|
||||
|
||||
@campaign.route('/details', methods=['GET'])
|
||||
def get_twitter_campaign_details():
|
||||
decoded_token = verify_token(request.headers)
|
||||
if not decoded_token:
|
||||
return "Not authorised!", 401
|
||||
|
||||
user_id = decoded_token['sub']
|
||||
|
||||
content_type = request.headers.get('Content-Type')
|
||||
if content_type != 'application/json':
|
||||
return "Content-type not supported!", 400
|
||||
|
||||
request_json = request.json
|
||||
if not request_json['campaign_id']:
|
||||
return "Invalid request!", 400
|
||||
|
||||
twitter_campaign = TwitterCampaign.query.filter_by(user_id=user_id, id=request_json['campaign_id']).first()
|
||||
campaign_posts = TwitterPost.query.filter_by(user_id=user_id, campaign_id=request_json['campaign_id'])
|
||||
|
||||
posts = []
|
||||
for post in campaign_posts:
|
||||
post_data = {}
|
||||
post_data['post_id'] = post.post_id
|
||||
post_data['campaign_id'] = post.campaign_id
|
||||
post_data['post_content'] = post.post_content
|
||||
post_data['twitter_post_id'] = post.twitter_post_id
|
||||
posts.append(post_data)
|
||||
|
||||
return jsonify({
|
||||
"campaign_id": twitter_campaign.id,
|
||||
"campaign_name": twitter_campaign.campaign_name,
|
||||
"user_input": twitter_campaign.user_input,
|
||||
"twitter_account_id": twitter_campaign.twitter_account_id,
|
||||
"posts": posts
|
||||
}), 200
|
||||
|
@ -5,10 +5,17 @@ campaign_db = SQLAlchemy()
|
||||
class TwitterCampaign(campaign_db.Model):
|
||||
__tablename__ = 'twitter_campaign'
|
||||
|
||||
campaign_id = campaign_db.Column(campaign_db.Integer, primary_key=True)
|
||||
id = campaign_db.Column(campaign_db.Integer, primary_key=True)
|
||||
user_id = campaign_db.Column(campaign_db.String(64), nullable=False)
|
||||
campaign_name = campaign_db.Column(campaign_db.String(64), nullable=False)
|
||||
twitter_account_id = campaign_db.Column(campaign_db.String(32), nullable=False)
|
||||
user_input = campaign_db.Column(campaign_db.String(100), nullable=False)
|
||||
generated_content = campaign_db.Column(campaign_db.String(300), nullable=False)
|
||||
|
||||
class TwitterPost(campaign_db.Model):
|
||||
__tablename__ = 'twitter_post'
|
||||
|
||||
id = campaign_db.Column(campaign_db.Integer, primary_key=True)
|
||||
campaign_id = campaign_db.Column(campaign_db.Integer, nullable=False)
|
||||
user_id = campaign_db.Column(campaign_db.String(64), nullable=False)
|
||||
post_content = campaign_db.Column(campaign_db.String(300), nullable=False)
|
||||
twitter_post_id = campaign_db.Column(campaign_db.String(32), unique=True, nullable=False)
|
||||
|
@ -36,11 +36,13 @@ def add_twitter_account():
|
||||
user_id = decoded_token['sub']
|
||||
|
||||
content_type = request.headers.get('Content-Type')
|
||||
if (content_type == 'application/json'):
|
||||
if content_type != 'application/json':
|
||||
return "Content-type not supported!", 400
|
||||
|
||||
json = request.json
|
||||
if not json['veryfier'] or not json['oauth_token'] or not json['oauth_token_secret']:
|
||||
return "Invalid request!", 400
|
||||
else:
|
||||
|
||||
consumer_key = current_app.config["TWITTER_CONSUMER_KEY"]
|
||||
consumer_secret = current_app.config["TWITTER_CONSUMER_SECERT"]
|
||||
|
||||
@ -54,7 +56,12 @@ def add_twitter_account():
|
||||
)
|
||||
oauth_tokens = oauth.fetch_access_token(access_token_url)
|
||||
|
||||
new_twitter_account = TwitterAccount(
|
||||
twitter_account = TwitterAccount.query.filter_by(user_id=user_id, twitter_account_id=oauth_tokens['user_id'])
|
||||
|
||||
if twitter_account:
|
||||
return "Account already exists!", 400
|
||||
|
||||
twitter_account = TwitterAccount(
|
||||
user_id = user_id,
|
||||
twitter_account_id = oauth_tokens['user_id'],
|
||||
username = oauth_tokens['screen_name'],
|
||||
@ -62,9 +69,33 @@ def add_twitter_account():
|
||||
access_token_secret = oauth_tokens['oauth_token_secret']
|
||||
)
|
||||
|
||||
twitter_db.session.add(new_twitter_account)
|
||||
twitter_db.session.add(twitter_account)
|
||||
twitter_db.session.commit()
|
||||
|
||||
return "Twitter account succesfully added.", 201
|
||||
else:
|
||||
|
||||
@twitter.route('/account', methods=['DELETE'])
|
||||
def delete_twitter_account():
|
||||
decoded_token = verify_token(request.headers)
|
||||
if not decoded_token:
|
||||
return "Not authorised!", 401
|
||||
|
||||
user_id = decoded_token['sub']
|
||||
|
||||
content_type = request.headers.get('Content-Type')
|
||||
if content_type != 'application/json':
|
||||
return "Content-type not supported!", 400
|
||||
|
||||
request_json = request.json
|
||||
if not request_json['twitter_account_id']:
|
||||
return "Invalid request!", 400
|
||||
|
||||
twitter_account = TwitterAccount.query.filter_by(user_id=user_id, twitter_account_id=request_json['twitter_account_id'])
|
||||
|
||||
if not twitter_account:
|
||||
return "Account not found!", 404
|
||||
|
||||
twitter_db.session.delete(twitter_account)
|
||||
twitter_db.session.commit()
|
||||
|
||||
return "Twitter account succesfully deleted.", 200
|
||||
|
@ -1,4 +1,3 @@
|
||||
from enum import unique
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
|
||||
twitter_db = SQLAlchemy()
|
||||
@ -6,7 +5,7 @@ twitter_db = SQLAlchemy()
|
||||
class TwitterAccount(twitter_db.Model):
|
||||
__tablename__ = 'twitter_account'
|
||||
|
||||
account_id = twitter_db.Column(twitter_db.Integer, primary_key=True)
|
||||
id = twitter_db.Column(twitter_db.Integer, primary_key=True)
|
||||
user_id = twitter_db.Column(twitter_db.String(64), nullable=False)
|
||||
twitter_account_id = twitter_db.Column(twitter_db.String(32), nullable=False)
|
||||
username = twitter_db.Column(twitter_db.String(16), nullable=False)
|
||||
|
Loading…
Reference in New Issue
Block a user