Flask_terms.py: Fixed mapping of notifcation e-mails to terms with uppercase letters

This commit is contained in:
Philipp Schmied 2018-02-28 09:19:27 +01:00
parent fd329aaba9
commit b6fcf51309

View file

@ -3,7 +3,7 @@
''' '''
Flask functions and routes for the trending modules page Flask functions and routes for the trending modules page
note: The matching of credential against supplied credential is done using Levenshtein distance note: The matching of credential against supplied credential is done using Levenshtein distance
''' '''
import redis import redis
@ -128,7 +128,7 @@ def mixUserName(supplied, extensive=False):
if len(usr) > 2: if len(usr) > 2:
filtered_usernames.append(usr) filtered_usernames.append(usr)
return filtered_usernames return filtered_usernames
# ============ ROUTES ============ # ============ ROUTES ============
@ -148,7 +148,7 @@ def terms_management():
# Map tracking if notifications are enabled for a specific term # Map tracking if notifications are enabled for a specific term
notificationEnabledDict = {} notificationEnabledDict = {}
# Maps a specific term to the associated email addresses # Maps a specific term to the associated email addresses
notificationEMailTermMapping = {} notificationEMailTermMapping = {}
@ -157,12 +157,12 @@ def terms_management():
trackReg_list_values = [] trackReg_list_values = []
trackReg_list_num_of_paste = [] trackReg_list_num_of_paste = []
for tracked_regex in r_serv_term.smembers(TrackedRegexSet_Name): for tracked_regex in r_serv_term.smembers(TrackedRegexSet_Name):
notificationEMailTermMapping[tracked_regex] = "\n".join(r_serv_term.smembers(TrackedTermsNotificationEmailsPrefix_Name + tracked_regex)) notificationEMailTermMapping[tracked_regex] = "\n".join(r_serv_term.smembers(TrackedTermsNotificationEmailsPrefix_Name + tracked_regex))
if tracked_regex not in notificationEnabledDict: if tracked_regex not in notificationEnabledDict:
notificationEnabledDict[tracked_regex] = False notificationEnabledDict[tracked_regex] = False
trackReg_list.append(tracked_regex) trackReg_list.append(tracked_regex)
value_range = Term_getValueOverRange(tracked_regex, today_timestamp, [1, 7, 31], per_paste=per_paste_text) value_range = Term_getValueOverRange(tracked_regex, today_timestamp, [1, 7, 31], per_paste=per_paste_text)
@ -182,13 +182,13 @@ def terms_management():
trackSet_list_values = [] trackSet_list_values = []
trackSet_list_num_of_paste = [] trackSet_list_num_of_paste = []
for tracked_set in r_serv_term.smembers(TrackedSetSet_Name): for tracked_set in r_serv_term.smembers(TrackedSetSet_Name):
notificationEMailTermMapping[tracked_set] = "\n".join(r_serv_term.smembers(TrackedTermsNotificationEmailsPrefix_Name + tracked_set)) notificationEMailTermMapping[tracked_set] = "\n".join(r_serv_term.smembers(TrackedTermsNotificationEmailsPrefix_Name + tracked_set))
if tracked_set not in notificationEnabledDict: if tracked_set not in notificationEnabledDict:
notificationEnabledDict[tracked_set] = False notificationEnabledDict[tracked_set] = False
trackSet_list.append(tracked_set) trackSet_list.append(tracked_set)
value_range = Term_getValueOverRange(tracked_set, today_timestamp, [1, 7, 31], per_paste=per_paste_text) value_range = Term_getValueOverRange(tracked_set, today_timestamp, [1, 7, 31], per_paste=per_paste_text)
@ -208,12 +208,12 @@ def terms_management():
track_list_values = [] track_list_values = []
track_list_num_of_paste = [] track_list_num_of_paste = []
for tracked_term in r_serv_term.smembers(TrackedTermsSet_Name): for tracked_term in r_serv_term.smembers(TrackedTermsSet_Name):
notificationEMailTermMapping[tracked_term] = "\n".join(r_serv_term.smembers(TrackedTermsNotificationEmailsPrefix_Name + tracked_term)) notificationEMailTermMapping[tracked_term] = "\n".join(r_serv_term.smembers(TrackedTermsNotificationEmailsPrefix_Name + tracked_term))
if tracked_term not in notificationEnabledDict: if tracked_term not in notificationEnabledDict:
notificationEnabledDict[tracked_term] = False notificationEnabledDict[tracked_term] = False
track_list.append(tracked_term) track_list.append(tracked_term)
value_range = Term_getValueOverRange(tracked_term, today_timestamp, [1, 7, 31], per_paste=per_paste_text) value_range = Term_getValueOverRange(tracked_term, today_timestamp, [1, 7, 31], per_paste=per_paste_text)
@ -235,9 +235,9 @@ def terms_management():
term_date = datetime.datetime.utcfromtimestamp(int(term_date)) if term_date is not None else "No date recorded" term_date = datetime.datetime.utcfromtimestamp(int(term_date)) if term_date is not None else "No date recorded"
black_list.append([blacked_term, term_date]) black_list.append([blacked_term, term_date])
return render_template("terms_management.html", return render_template("terms_management.html",
black_list=black_list, track_list=track_list, trackReg_list=trackReg_list, trackSet_list=trackSet_list, black_list=black_list, track_list=track_list, trackReg_list=trackReg_list, trackSet_list=trackSet_list,
track_list_values=track_list_values, track_list_num_of_paste=track_list_num_of_paste, track_list_values=track_list_values, track_list_num_of_paste=track_list_num_of_paste,
trackReg_list_values=trackReg_list_values, trackReg_list_num_of_paste=trackReg_list_num_of_paste, trackReg_list_values=trackReg_list_values, trackReg_list_num_of_paste=trackReg_list_num_of_paste,
trackSet_list_values=trackSet_list_values, trackSet_list_num_of_paste=trackSet_list_num_of_paste, trackSet_list_values=trackSet_list_values, trackSet_list_num_of_paste=trackSet_list_num_of_paste,
per_paste=per_paste, notificationEnabledDict=notificationEnabledDict, notificationEMailTermMapping=notificationEMailTermMapping) per_paste=per_paste, notificationEnabledDict=notificationEnabledDict, notificationEMailTermMapping=notificationEMailTermMapping)
@ -309,19 +309,19 @@ def terms_management_action():
action = request.args.get('action') action = request.args.get('action')
term = request.args.get('term') term = request.args.get('term')
notificationEmailsParam = request.args.get('emailAddresses') notificationEmailsParam = request.args.get('emailAddresses')
if action is None or term is None: if action is None or term is None:
return "None" return "None"
else: else:
if section == "followTerm": if section == "followTerm":
if action == "add": if action == "add":
# Strip all whitespace # Strip all whitespace
notificationEmailsParam = "".join(notificationEmailsParam.split()) notificationEmailsParam = "".join(notificationEmailsParam.split())
# Make a list of all passed email addresses # Make a list of all passed email addresses
notificationEmails = notificationEmailsParam.split(",") notificationEmails = notificationEmailsParam.split(",")
validNotificationEmails = [] validNotificationEmails = []
# check for valid email addresses # check for valid email addresses
for email in notificationEmails: for email in notificationEmails:
@ -329,15 +329,14 @@ def terms_management_action():
# has exactly one @ sign, and at least one . in the part after the @ # has exactly one @ sign, and at least one . in the part after the @
if re.match(r"[^@]+@[^@]+\.[^@]+", email): if re.match(r"[^@]+@[^@]+\.[^@]+", email):
validNotificationEmails.append(email) validNotificationEmails.append(email)
# add all valid emails to the set # add all valid emails to the set
for email in validNotificationEmails: for email in validNotificationEmails:
r_serv_term.sadd(TrackedTermsNotificationEmailsPrefix_Name + term, email) r_serv_term.sadd(TrackedTermsNotificationEmailsPrefix_Name + term.lower(), email)
print "added " + email + " for " + TrackedTermsNotificationEmailsPrefix_Name + term
# enable notifications by default # enable notifications by default
r_serv_term.sadd(TrackedTermsNotificationEnabled_Name, term.lower()) r_serv_term.sadd(TrackedTermsNotificationEnabled_Name, term.lower())
# check if regex/set or simple term # check if regex/set or simple term
#regex #regex
if term.startswith('/') and term.endswith('/'): if term.startswith('/') and term.endswith('/'):
@ -361,7 +360,7 @@ def terms_management_action():
else: else:
r_serv_term.sadd(TrackedTermsSet_Name, term.lower()) r_serv_term.sadd(TrackedTermsSet_Name, term.lower())
r_serv_term.hset(TrackedTermsDate_Name, term.lower(), today_timestamp) r_serv_term.hset(TrackedTermsDate_Name, term.lower(), today_timestamp)
elif action == "toggleEMailNotification": elif action == "toggleEMailNotification":
# get the current state # get the current state
if term in r_serv_term.smembers(TrackedTermsNotificationEnabled_Name): if term in r_serv_term.smembers(TrackedTermsNotificationEnabled_Name):
@ -370,7 +369,7 @@ def terms_management_action():
else: else:
# add it # add it
r_serv_term.sadd(TrackedTermsNotificationEnabled_Name, term.lower()) r_serv_term.sadd(TrackedTermsNotificationEnabled_Name, term.lower())
#del action #del action
else: else:
if term.startswith('/') and term.endswith('/'): if term.startswith('/') and term.endswith('/'):
@ -490,9 +489,9 @@ def terms_plot_top_data():
value = r_serv_term.hget(per_paste+str(timestamp), term) value = r_serv_term.hget(per_paste+str(timestamp), term)
curr_value_range = int(value) if value is not None else 0 curr_value_range = int(value) if value is not None else 0
value_range.append([timestamp, curr_value_range]) value_range.append([timestamp, curr_value_range])
to_return.append([term, value_range, tot_value, position]) to_return.append([term, value_range, tot_value, position])
return jsonify(to_return) return jsonify(to_return)
@ -567,7 +566,7 @@ def cred_management_action():
for Unum in uniq_num_set: for Unum in uniq_num_set:
levenRatio = 2.0 levenRatio = 2.0
username = r_serv_cred.hget(REDIS_KEY_ALL_CRED_SET_REV, Unum) username = r_serv_cred.hget(REDIS_KEY_ALL_CRED_SET_REV, Unum)
# Calculate Levenshtein distance, ignore negative ratio # Calculate Levenshtein distance, ignore negative ratio
supp_splitted = supplied.split() supp_splitted = supplied.split()
supp_mixed = supplied.replace(' ','') supp_mixed = supplied.replace(' ','')