Permalink
Browse files

Added the backend part of the email tagging system.

Work on #198
  • Loading branch information...
1 parent fa6e2ce commit 717b2125900c3e62dc7ea759db19679ac6e032df @pajlada committed Mar 4, 2016
Showing with 121 additions and 1 deletion.
  1. +3 −0 app.py
  2. +117 −1 pajbot/web/routes/api.py
  3. +1 −0 requirements/web.txt
View
@@ -14,6 +14,7 @@
from pajbot.bot import Bot
from pajbot.web.routes import api
+import pajbot.web.routes
from pajbot.web.routes import admin
from pajbot.web.models import errors
from pajbot.models.db import DBManager
@@ -58,6 +59,8 @@
app.register_blueprint(api.page)
app.register_blueprint(admin.page)
+pajbot.web.routes.api.init(app)
+
assets = Environment(app)
# Basic CSS and Javascript:
@@ -89,7 +89,6 @@ def get_user(username):
return make_response(jsonify({'error': 'Not found'}), 404)
-
@page.route('/api/v1/pleblist/list')
def pleblist_list():
with DBManager.create_session_scope() as session:
@@ -562,3 +561,120 @@ def social_set(social_key, **options):
redis.hset('streamer_info', key, value)
return make_response(jsonify({'message': 'success!'}))
+
+def init(app):
+ from flask_restful import Resource, Api, reqparse
+
+ api = Api(app)
+
+ class APIEmailTags(Resource):
+ def __init__(self):
+ super().__init__()
+
+ self.get_parser = reqparse.RequestParser()
+ self.get_parser.add_argument('email', trim=True, required=True, location='args')
+
+ self.post_parser = reqparse.RequestParser()
+ self.post_parser.add_argument('email', trim=True, required=True)
+ self.post_parser.add_argument('tag', trim=True, required=True)
+
+ self.delete_parser = reqparse.RequestParser()
+ self.delete_parser.add_argument('email', trim=True, required=True)
+ self.delete_parser.add_argument('tag', trim=True, required=True)
+
+ self.delim = '|||'
+
+ def get(self):
+ args = self.get_parser.parse_args()
+
+ email = args['email'].lower()
+ streamer = StreamHelper.get_streamer()
+
+ key = '{streamer}:email_tags'.format(streamer=streamer)
+ redis = RedisManager.get()
+
+ tags_str = redis.hget(key, email)
+
+ payload = {}
+
+ if tags_str is None:
+ tags = []
+ else:
+ tags = json.loads(tags_str)
+
+ payload['tags'] = tags
+
+ return payload
+
+ def post(self):
+ # Add a single tag to the email
+ args = self.post_parser.parse_args()
+
+ email = args['email'].lower()
+ new_tag = args['tag'].lower()
+ if len(new_tag) == 0:
+ return {
+ 'message': 'The tag must be at least 1 character long.'
+ }, 400
+ streamer = StreamHelper.get_streamer()
+
+ key = '{streamer}:email_tags'.format(streamer=streamer)
+ redis = RedisManager.get()
+
+ tags_str = redis.hget(key, email)
+
+ if tags_str is None:
+ tags = []
+ else:
+ tags = json.loads(tags_str)
+
+ # Is the tag already active?
+ if new_tag in tags:
+ return {
+ 'message': 'This tag is already set on the email.'
+ }, 409
+
+ tags.append(new_tag)
+
+ redis.hset(key, email, json.dumps(tags))
+
+ return {
+ 'message': 'Successfully added the tag {} to {}'.format(new_tag, email)
+ }
+
+ def delete(self):
+ # Add a single tag to the email
+ args = self.delete_parser.parse_args()
+
+ email = args['email'].lower()
+ new_tag = args['tag'].lower()
+ streamer = StreamHelper.get_streamer()
+
+ key = '{streamer}:email_tags'.format(streamer=streamer)
+ redis = RedisManager.get()
+
+ tags_str = redis.hget(key, email)
+
+ if tags_str is None:
+ tags = []
+ else:
+ tags = json.loads(tags_str)
+
+ # Is the tag already active?
+ if new_tag not in tags:
+ return {
+ 'message': 'This tag is not set on the email.'
+ }, 409
+
+ tags.remove(new_tag)
+
+ if len(tags) > 0:
+ redis.hset(key, email, json.dumps(tags))
+ else:
+ redis.hdel(key, email)
+
+ return {
+ 'message': 'Successfully removed the tag {} from {}'.format(new_tag, email)
+ }
+
+ api.add_resource(APIEmailTags, '/api/v1/email/tags')
@@ -5,3 +5,4 @@ Flask-Assets
jsmin
markdown
uwsgidecorators
+flask-restful

0 comments on commit 717b212

Please sign in to comment.