Finalize migration tool.

Retrieve comments by GET and POST
pull/6/head
Yax 10 years ago
parent 4c4ee442d3
commit 612fb85fa3

@ -3,28 +3,43 @@
import logging import logging
from flask import request, jsonify from flask import request, jsonify
from playhouse.shortcuts import model_to_dict
from app import app from app import app
from app.models.site import Site from app.models.site import Site
from app.models.comment import Comment from app.models.comment import Comment
from app.helpers.hashing import md5
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@app.route("/comments", methods=['POST']) @app.route("/comments", methods=['GET', 'POST'])
def query_comments(): def query_comments():
query = request.json
token = query['token']
url = query['url']
logger.info('token=%s url=%s' % (token, url))
comments = [] comments = []
for comment in Comment.select(Comment).join(Site).where( try:
(Comment.url == url) & if request.method == 'POST':
(Site.token == token)).order_by(Comment.published): token = request.json['token']
comments.append(model_to_dict(comment)) url = request.json['url']
else:
token = request.args.get('token', '')
url = request.args.get('url', '')
r = jsonify({'data': comments}) logger.info('retrieve comments for token %s, url %s' % (token, url))
r.status_code = 200 for comment in Comment.select(Comment).join(Site).where(
(Comment.url == url) &
(Site.token == token)).order_by(Comment.published):
d = {}
d['author'] = comment.author_name
d['content'] = comment.content
if comment.author_site:
d['site'] = comment.author_site
if comment.author_email:
d['avatar'] = md5(comment.author_email.strip().lower())
d['date'] = comment.published.strftime("%Y-%m-%d %H:%M:%S")
comments.append(d)
r = jsonify({'data': comments})
r.status_code = 200
except:
logger.warn('bad request')
r = jsonify({'data': []})
r.status_code = 400
return r return r

@ -0,0 +1,16 @@
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import hashlib
import config
def salt(value):
string = '%s%s' % (value, config.SALT)
dk = hashlib.sha256(string.encode())
return dk.hexdigest()
def md5(value):
dk = hashlib.md5(value.encode())
return dk.hexdigest()

@ -1,15 +1,13 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
import hashlib
import config import config
import functools import functools
from config import DB_URL
from playhouse.db_url import connect from playhouse.db_url import connect
def get_db(): def get_db():
return connect(DB_URL) return connect(config.DB_URL)
def provide_db(func): def provide_db(func):
@ -21,12 +19,6 @@ def provide_db(func):
return new_function return new_function
def hash(value):
string = '%s%s' % (value, config.SALT)
dk = hashlib.sha256(string.encode())
return dk.hexdigest()
@provide_db @provide_db
def setup(db): def setup(db):
from app.models.site import Site from app.models.site import Site

@ -6,3 +6,5 @@ DB_URL = "mysql://stacosys_user:stacosys_password@localhost:3306/stacosys"
HTTP_ADDRESS = "0.0.0.0" HTTP_ADDRESS = "0.0.0.0"
HTTP_PORT = 8000 HTTP_PORT = 8000
SALT = "BRRJRqXgGpXWrgTidBPcixIThHpDuKc0"

@ -17,6 +17,7 @@ for path in paths:
# import database models # import database models
from app.services.database import provide_db from app.services.database import provide_db
from app.helpers.hashing import salt
from app.models.site import Site from app.models.site import Site
from app.models.comment import Comment from app.models.comment import Comment
@ -57,14 +58,14 @@ def convert_comment(db, site, filename):
# create DB record # create DB record
comment = Comment(site=site, author_name=d['author'], content=content) comment = Comment(site=site, author_name=d['author'], content=content)
if 'email' in d: if 'email' in d:
comment.author_email = d['email'] comment.author_email = d['email'].strip()
if 'site' in d: if 'site' in d:
comment.author_site = d['site'] comment.author_site = d['site'].strip()
if 'url' in d: if 'url' in d:
if d['url'][:7] == 'http://': if d['url'][:7] == 'http://':
comment.url = d['url'][7:] comment.url = d['url'][7:].strip()
elif d['url'][:8] == 'https://': elif d['url'][:8] == 'https://':
comment.url = d['url'][8:] comment.url = d['url'][8:].strip()
# else: # else:
# comment.url = d['article'] # comment.url = d['article']
if 'date' in d: if 'date' in d:
@ -86,7 +87,7 @@ def convert(db, site_name, url, comment_dir):
except Site.DoesNotExist: except Site.DoesNotExist:
pass pass
site = Site.create(name=site_name, url=url, token='') site = Site.create(name=site_name, url=url, token=salt(url))
for dirpath, dirs, files in os.walk(comment_dir): for dirpath, dirs, files in os.walk(comment_dir):
for filename in files: for filename in files:

Loading…
Cancel
Save