2018-05-04 13:53:29 +02:00
|
|
|
#!/usr/bin/env python3
|
2017-04-19 15:14:20 +02:00
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
|
|
|
'''
|
|
|
|
Flask functions and routes for the trending modules page
|
|
|
|
'''
|
|
|
|
import redis
|
|
|
|
import json
|
|
|
|
import flask
|
2017-08-23 12:20:39 +02:00
|
|
|
import os
|
|
|
|
from datetime import datetime
|
2017-04-19 15:14:20 +02:00
|
|
|
from flask import Flask, render_template, jsonify, request, Blueprint
|
|
|
|
|
|
|
|
import Paste
|
|
|
|
|
|
|
|
# ============ VARIABLES ============
|
|
|
|
import Flask_config
|
|
|
|
|
|
|
|
app = Flask_config.app
|
|
|
|
cfg = Flask_config.cfg
|
|
|
|
max_preview_char = Flask_config.max_preview_char
|
|
|
|
max_preview_modal = Flask_config.max_preview_modal
|
2018-05-17 11:00:05 +02:00
|
|
|
r_serv_metadata = Flask_config.r_serv_metadata
|
2017-08-23 12:20:39 +02:00
|
|
|
|
|
|
|
#init all lvlDB servers
|
|
|
|
curYear = datetime.now().year
|
2018-05-07 14:50:40 +02:00
|
|
|
int_year = int(curYear)
|
2017-08-23 12:20:39 +02:00
|
|
|
r_serv_db = {}
|
|
|
|
# port generated automatically depending on available levelDB date
|
|
|
|
yearList = []
|
|
|
|
|
2018-05-07 14:50:40 +02:00
|
|
|
for x in range(0, (int_year - 2018) + 1):
|
|
|
|
|
|
|
|
intYear = int_year - x
|
|
|
|
|
|
|
|
yearList.append([str(intYear), intYear, int(curYear) == intYear])
|
2017-08-23 12:20:39 +02:00
|
|
|
r_serv_db[intYear] = redis.StrictRedis(
|
2018-05-07 14:50:40 +02:00
|
|
|
host=cfg.get("ARDB_DB", "host"),
|
|
|
|
port=cfg.getint("ARDB_DB", "port"),
|
|
|
|
db=intYear,
|
2018-05-04 13:53:29 +02:00
|
|
|
decode_responses=True)
|
2018-05-07 14:50:40 +02:00
|
|
|
|
2017-08-23 12:20:39 +02:00
|
|
|
yearList.sort(reverse=True)
|
2017-04-19 15:14:20 +02:00
|
|
|
|
|
|
|
browsepastes = Blueprint('browsepastes', __name__, template_folder='templates')
|
|
|
|
|
|
|
|
# ============ FUNCTIONS ============
|
|
|
|
|
|
|
|
def getPastebyType(server, module_name):
|
|
|
|
all_path = []
|
|
|
|
for path in server.smembers('WARNING_'+module_name):
|
|
|
|
all_path.append(path)
|
2018-05-03 16:21:33 +02:00
|
|
|
|
2017-04-19 15:14:20 +02:00
|
|
|
return all_path
|
|
|
|
|
|
|
|
|
2017-08-23 12:20:39 +02:00
|
|
|
def event_stream_getImportantPasteByModule(module_name, year):
|
2017-04-19 15:14:20 +02:00
|
|
|
index = 0
|
2017-08-23 12:20:39 +02:00
|
|
|
all_pastes_list = getPastebyType(r_serv_db[year], module_name)
|
2018-05-17 11:00:05 +02:00
|
|
|
paste_tags = []
|
|
|
|
bootstrap_label = []
|
|
|
|
bootstrap_label.append('primary')
|
|
|
|
bootstrap_label.append('success')
|
|
|
|
bootstrap_label.append('danger')
|
|
|
|
bootstrap_label.append('warning')
|
|
|
|
bootstrap_label.append('info')
|
|
|
|
bootstrap_label.append('dark')
|
2018-05-04 13:53:29 +02:00
|
|
|
|
2017-04-19 15:14:20 +02:00
|
|
|
for path in all_pastes_list:
|
|
|
|
index += 1
|
|
|
|
paste = Paste.Paste(path)
|
2018-04-17 16:06:32 +02:00
|
|
|
content = paste.get_p_content()
|
2017-04-19 15:14:20 +02:00
|
|
|
content_range = max_preview_char if len(content)>max_preview_char else len(content)-1
|
|
|
|
curr_date = str(paste._get_p_date())
|
|
|
|
curr_date = curr_date[0:4]+'/'+curr_date[4:6]+'/'+curr_date[6:]
|
2018-05-17 11:00:05 +02:00
|
|
|
p_tags = r_serv_metadata.smembers('tag:'+path)
|
|
|
|
l_tags = []
|
|
|
|
for tag in p_tags:
|
|
|
|
tag = tag.split('=')
|
|
|
|
if len(tag) > 1:
|
|
|
|
if tag[1] != '':
|
|
|
|
tag = tag[1][1:-1]
|
|
|
|
# no value
|
|
|
|
else:
|
|
|
|
tag = tag[0][1:-1]
|
|
|
|
# use for custom tags
|
|
|
|
else:
|
|
|
|
tag = tag[0]
|
|
|
|
|
|
|
|
l_tags.append(tag)
|
|
|
|
|
2017-04-19 15:14:20 +02:00
|
|
|
data = {}
|
|
|
|
data["module"] = module_name
|
|
|
|
data["index"] = index
|
|
|
|
data["path"] = path
|
|
|
|
data["content"] = content[0:content_range]
|
|
|
|
data["linenum"] = paste.get_lines_info()[0]
|
|
|
|
data["date"] = curr_date
|
2018-05-17 11:00:05 +02:00
|
|
|
data["l_tags"] = l_tags
|
|
|
|
data["bootstrap_label"] = bootstrap_label
|
2017-04-19 15:14:20 +02:00
|
|
|
data["char_to_display"] = max_preview_modal
|
|
|
|
data["finished"] = True if index == len(all_pastes_list) else False
|
|
|
|
yield 'retry: 100000\ndata: %s\n\n' % json.dumps(data) #retry to avoid reconnection of the browser
|
|
|
|
|
|
|
|
# ============ ROUTES ============
|
|
|
|
|
|
|
|
@browsepastes.route("/browseImportantPaste/", methods=['GET'])
|
|
|
|
def browseImportantPaste():
|
|
|
|
module_name = request.args.get('moduleName')
|
2017-08-23 12:20:39 +02:00
|
|
|
return render_template("browse_important_paste.html", year_list=yearList, selected_year=curYear)
|
2017-04-19 15:14:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
@browsepastes.route("/importantPasteByModule/", methods=['GET'])
|
|
|
|
def importantPasteByModule():
|
|
|
|
module_name = request.args.get('moduleName')
|
2018-05-07 14:50:40 +02:00
|
|
|
|
|
|
|
# # TODO: VERIFY YEAR VALIDITY
|
|
|
|
try:
|
|
|
|
currentSelectYear = int(request.args.get('year'))
|
|
|
|
except:
|
|
|
|
print('Invalid year input')
|
|
|
|
currentSelectYear = int(datetime.now().year)
|
2017-04-19 15:14:20 +02:00
|
|
|
|
|
|
|
all_content = []
|
|
|
|
paste_date = []
|
|
|
|
paste_linenum = []
|
|
|
|
all_path = []
|
2018-05-17 11:00:05 +02:00
|
|
|
paste_tags = []
|
2017-08-23 12:20:39 +02:00
|
|
|
allPastes = getPastebyType(r_serv_db[currentSelectYear], module_name)
|
2017-04-19 15:14:20 +02:00
|
|
|
|
2018-05-17 11:00:05 +02:00
|
|
|
bootstrap_label = []
|
|
|
|
bootstrap_label.append('primary')
|
|
|
|
bootstrap_label.append('success')
|
|
|
|
bootstrap_label.append('danger')
|
|
|
|
bootstrap_label.append('warning')
|
|
|
|
bootstrap_label.append('info')
|
|
|
|
bootstrap_label.append('dark')
|
|
|
|
|
2017-04-19 15:14:20 +02:00
|
|
|
for path in allPastes[0:10]:
|
|
|
|
all_path.append(path)
|
|
|
|
paste = Paste.Paste(path)
|
2018-04-17 16:06:32 +02:00
|
|
|
content = paste.get_p_content()
|
2017-04-19 15:14:20 +02:00
|
|
|
content_range = max_preview_char if len(content)>max_preview_char else len(content)-1
|
|
|
|
all_content.append(content[0:content_range].replace("\"", "\'").replace("\r", " ").replace("\n", " "))
|
|
|
|
curr_date = str(paste._get_p_date())
|
|
|
|
curr_date = curr_date[0:4]+'/'+curr_date[4:6]+'/'+curr_date[6:]
|
|
|
|
paste_date.append(curr_date)
|
|
|
|
paste_linenum.append(paste.get_lines_info()[0])
|
2018-05-17 11:00:05 +02:00
|
|
|
p_tags = r_serv_metadata.smembers('tag:'+path)
|
|
|
|
l_tags = []
|
|
|
|
for tag in p_tags:
|
|
|
|
tag = tag.split('=')
|
|
|
|
if len(tag) > 1:
|
|
|
|
if tag[1] != '':
|
|
|
|
tag = tag[1][1:-1]
|
|
|
|
# no value
|
|
|
|
else:
|
|
|
|
tag = tag[0][1:-1]
|
|
|
|
# use for custom tags
|
|
|
|
else:
|
|
|
|
tag = tag[0]
|
|
|
|
|
|
|
|
l_tags.append(tag)
|
|
|
|
|
|
|
|
paste_tags.append(l_tags)
|
2017-04-19 15:14:20 +02:00
|
|
|
|
|
|
|
if len(allPastes) > 10:
|
|
|
|
finished = False
|
|
|
|
else:
|
|
|
|
finished = True
|
|
|
|
|
|
|
|
return render_template("important_paste_by_module.html",
|
2018-04-17 16:06:32 +02:00
|
|
|
moduleName=module_name,
|
2017-08-23 12:20:39 +02:00
|
|
|
year=currentSelectYear,
|
2018-04-17 16:06:32 +02:00
|
|
|
all_path=all_path,
|
|
|
|
content=all_content,
|
|
|
|
paste_date=paste_date,
|
|
|
|
paste_linenum=paste_linenum,
|
|
|
|
char_to_display=max_preview_modal,
|
2018-05-17 11:00:05 +02:00
|
|
|
paste_tags=paste_tags,
|
|
|
|
bootstrap_label=bootstrap_label,
|
2017-04-19 15:14:20 +02:00
|
|
|
finished=finished)
|
|
|
|
|
2017-08-23 12:20:39 +02:00
|
|
|
@browsepastes.route("/_getImportantPasteByModule", methods=['GET'])
|
2017-04-19 15:14:20 +02:00
|
|
|
def getImportantPasteByModule():
|
|
|
|
module_name = request.args.get('moduleName')
|
2017-08-23 12:20:39 +02:00
|
|
|
currentSelectYear = int(request.args.get('year'))
|
|
|
|
return flask.Response(event_stream_getImportantPasteByModule(module_name, currentSelectYear), mimetype="text/event-stream")
|
2017-04-19 15:14:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
# ========= REGISTRATION =========
|
|
|
|
app.register_blueprint(browsepastes)
|