Skip to content

Commit

Permalink
Merge pull request #185 from ehenneken/record_update_diff
Browse files Browse the repository at this point in the history
calculate updated record diff in backend
  • Loading branch information
ehenneken committed Dec 9, 2020
2 parents 3094e3c + 96efb7e commit abbd40c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 83 deletions.
30 changes: 30 additions & 0 deletions adsws/feedback/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from flask import current_app
from flask.ext.mail import Message
import json
#from dictdiffer import diff
from jsondiff import diff

def send_feedback_email(name, sender, subject, data, attachments=None):
# Allow the default recipient to be overriden depending on email subject
Expand All @@ -23,6 +25,32 @@ def send_feedback_email(name, sender, subject, data, attachments=None):
current_app.logger.info('Successfully sent email: data submitted by {0}, sent to {1} (form: {2})'.format(sender, email, subject))
return msg

def make_diff(original, updated):

diffdata = diff(original, updated)

results = ''
if diffdata.has_key('comments'):
results += "\n\nComments: %s\n\n" % diffdata['comments']
for field, changes in diffdata.items():
if field == 'comments':
continue
results += ">>>> %s\n" % field
if isinstance(changes,dict):
for k,v in changes.items():
results += "{0} -- {1}\n".format(k,v)
elif isinstance(changes,list):
for item in changes:
try:
results += "{0}\t{1}\n".format(updated['bibcode'], item.replace('(bibcode) ','').replace('(reference) ',''))
except:
results += str(item) + "\n"
else:
results += str(changes) + "\n"
results += ">>>>\n"

return results

def err(error_dictionary):
"""
Formats the error response as wanted by the Flask app
Expand All @@ -31,3 +59,5 @@ def err(error_dictionary):
:return: tuple of error message and error number
"""
return {'error': error_dictionary['body']}, error_dictionary['number']


14 changes: 9 additions & 5 deletions adsws/feedback/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from adsws.feedback.utils import err
from adsws.accounts.utils import verify_recaptcha, get_post_data
from werkzeug.exceptions import BadRequestKeyError
from utils import send_feedback_email
from utils import send_feedback_email, make_diff
from urllib import unquote

API_DOCS = 'https://github.com/adsabs/adsabs-dev-api'
Expand Down Expand Up @@ -76,17 +76,21 @@ def create_email_body(post_data):
email_data.pop(key, None)
# Retrieve the appropriate template
template = current_app.config['FEEDBACK_TEMPLATES'].get(email_data.get('_subject'))
# For abstract corrections, the POST payload has a "diff" attribute that contains
# For abstract corrections, we determine a diff from the original and updated records.
# In case this fails we fall back on the POST data "diff" attribute that contains
# the updated fields in Github "diff" format, URL encoded. For display purposes,
# this needs to be decoded.
if post_data.has_key('diff'):
email_data['diff'] = unquote(post_data['diff'])
if post_data.get('_subject') == 'Updated Record':
try:
email_data['diff'] = make_diff(post_data['original'], post_data['new'])
except:
email_data['diff'] = unquote(post_data.get('diff',''))
# In the case of a new record the mail body will show a summary
# In this summary it's easier to show a author list in the form of a string
# We also attach the JSON data of the new record as a file
if post_data.get('_subject') == 'New Record':
try:
email_data['new']['author_list'] = ";".join([a['name'] for a in post_data['new']['authors']])
email_data['new']['author_list'] = ";".join([a for a in post_data['new']['authors']])
except:
email_data['new']['author_list'] = ""
# Construct the email body
Expand Down
2 changes: 1 addition & 1 deletion adsws/tests/stubdata/corrected_abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@
"diff": "%0A%20%20%3E%3E%3E%3E%20Title%0A%20%20test%0A%20%20%3C%3C%3C%3C%0A%0A%20%20%3E%3E%3E%3E%20Publication%0A%20%20test%0A%20%20%3C%3C%3C%3C%0A%0A%20%20%3E%3E%3E%3E%20PublicationDate%0A%20%202020-01%0A%20%20%3C%3C%3C%3C%0A%0A%20%20%3E%3E%3E%3E%20Comments%0A%20%20test%0A%20%20%3C%3C%3C%3C"
}

response = 'From: Tim Hostetler\nAddress: twhostetler0@gmail.com\n\nCorrection for 2021NewA...8301464S:\n\n\n >>>> Title\n test\n <<<<\n\n >>>> Publication\n test\n <<<<\n\n >>>> PublicationDate\n 2020-01\n <<<<\n\n >>>> Comments\n test\n <<<<'
response = 'From: Tim Hostetler\nAddress: twhostetler0@gmail.com\n\nCorrection for 2021NewA...8301464S:\n\n>>>> $delete\n2021NewA...8301464S\temail\n2021NewA...8301464S\trecaptcha\n2021NewA...8301464S\tentryType\n2021NewA...8301464S\tname\n>>>>\n>>>> collection\n2021NewA...8301464S\tastronomy\n2021NewA...8301464S\tphysics\n>>>>\n'
79 changes: 2 additions & 77 deletions adsws/tests/stubdata/new_abstract.py
Original file line number Diff line number Diff line change
@@ -1,79 +1,4 @@
# -*- coding: utf-8 -*-
data = {
"origin": "user_submission",
"g-recaptcha-response": "correct_response",
"_subject": "New Record",
"original": {
"entryType": "new",
"name": "",
"email": "",
"collection": [
"astronomy"
],
"bibcode": "",
"title": "",
"authors": [

],
"publication": "",
"publicationDate": "",
"urls": [
{
"value": ""
}
],
"abstract": "",
"keywords": [
{
"value": ""
}
],
"references": [
{
"value": ""
}
],
"comments": "",
"recaptcha": ""
},
"new": {
"collection": [
"astronomy"
],
"bibcode": "",
"title": "test",
"authors": [
{
"id": "Shekh, S. H._0",
"position": 0,
"name": "Shekh, S. H.",
"aff": "Department of Mathematics, S. P. M. Science and Gilani Arts Commerece College Ghatanji, Maharashtra 445301, India",
"orcid": ""
},
{
"id": "Shekh, S. H._0",
"position": 0,
"name": "Foo, Bar",
"aff": "Department of Mathematics, S. P. M. Science and Gilani Arts Commerece College Ghatanji, Maharashtra 445301, India",
"orcid": ""
},
],
"publication": "test",
"publicationDate": "2020-01",
"urls": [
{
"type": "none",
"value": ""
}
],
"abstract": "Present analysis dedicated to the dynamical investigation of anisotropic dark energy LRS Bianchi type-I cosmological model in the context of modified gravity in which Langrangian be the arbitrary function of Ricci scalar and Gouss-Bonnet invariant say f(R, G) gravity in the way of anisotropic fluid. The classification of the field equations towards f(R , G) =f<SUB>0</SUB>R<SUP>m</SUP><SUP>G 1 - m</SUP> make available that the model is purely accelerating corresponds to 0 ~ q ~ - 1 . We govern the features of the derived cosmological model in view of the hybrid law inflation in bounce form (which involve power and de-Sitter cosmology) for the average scale factor. Also discussed the singularity of the model with the help of curvature of the model. It is observed that the model is fully engaged with both matter which exist initially for short expansion and dark energy dominated era and rests existing in quintessence dominated era and for sufficiently large time derived model forecasts that the anisotropy of the model will damp out and the Universe will turn out to be isotropic one and also observed that for quintessence dominated era the temperature and entropy density of anticipated model are positive definite along with some physical and kinematical parameters of the bounce model is also discussed in details.",
"keywords": [],
"references": [],
"comments": "test"
},
"name": "Tim Hostetler",
"email": "twhostetler0@gmail.com",
"diff": ""
}
data = {u'origin': u'user_submission', u'g-recaptcha-response': u'correct_response', u'email': u'cgrant@cfa.harvard.edu', u'_subject': u'New Record', u'diff': u'', u'new': {u'bibcode': u'2020DPS....5241201D', u'publication': u'AAS Division of Planetary Science meeting #52, id. 412.01. Bulletin of the American Astronomical Society, Vol. 52, No. 6 e-id 2020n6i412p01', u'title': u'tittle', u'abstract': u'wonderful new abstract', u'comments': u'user comments', u'collection': [u'astronomy'], u'affiliation': [u'KinetX Aerospace, Inc., Greenbelt, MD', u'IOTA, Greenbelt, MD', u'Southwest Research Institute, Boulder, CO', u'IOTA, Greenbelt, MD', u'IOTA, Murrumbateman', u'JPL, Pasadena, CA', u'JPL, Pasadena, CA', u'Planetary Exploration Res. Center, Chiba Inst. of Technology, Chiba Japan', u'CRAAG, Algiers Observatory, Algiers Algeria, MD'], u'references': [], u'publicationDate': u'2020-10-00', u'urls': [], u'authors': [u'Dunham, D.', u'Dunham, J.', u'Buie, M.', u'Preston, S.', u'Herald, D.', u'Farnocchia, D.', u'Giorgini, J.', u'Arai, T.', u'Aissa, B.'], u'keywords': [], u'orcid': [u'', u'', u'', u'', u'', u'', u'', u'', u'']}, u'original': {u'bibcode': u'2020DPS....5241201D', u'publication': u'AAS Division of Planetary Science meeting #52, id. 412.01. Bulletin of the American Astronomical Society, Vol. 52, No. 6 e-id 2020n6i412p01', u'title': u'tittle', u'abstract': u'wonderful new abstract', u'comments': u'user comments', u'collection': [u'astronomy'], u'affiliation': [u'KinetX Aerospace, Inc., Greenbelt, MD', u'IOTA, Greenbelt, MD', u'Southwest Research Institute, Boulder, CO', u'IOTA, Greenbelt, MD', u'IOTA, Murrumbateman', u'JPL, Pasadena, CA', u'JPL, Pasadena, CA', u'Planetary Exploration Res. Center, Chiba Inst. of Technology, Chiba Japan', u'CRAAG, Algiers Observatory, Algiers Algeria, MD'], u'references': [], u'publicationDate': u'2020-10-00', u'urls': [], u'authors': [u'Dunham, D.', u'Dunham, J.', u'Buie, M.', u'Preston, S.', u'Herald, D.', u'Farnocchia, D.', u'Giorgini, J.', u'Arai, T.', u'Aissa, B.'], u'keywords': [], u'orcid': [u'', u'', u'', u'', u'', u'', u'', u'', u'']}, u'name': u'Edw\xeen H\xe8nn\xebk\u0119n'}

response = 'From: Tim Hostetler\nAddress: twhostetler0@gmail.com\n\nAbstract data for new record. Summary:\n\nCollection: astronomy\n\n%R \n%T test\n%A Shekh, S. H.;Foo, Bar\n%D 2020-01\n%J test\n%B Present analysis dedicated to the dynamical investigation of anisotropic dark energy LRS Bianchi type-I cosmological model in the context of modified gravity in which Langrangian be the arbitrary function of Ricci scalar and Gouss-Bonnet invariant say f(R, G) gravity in the way of anisotropic fluid. The classification of the field equations towards f(R , G) =f<SUB>0</SUB>R<SUP>m</SUP><SUP>G 1 - m</SUP> make available that the model is purely accelerating corresponds to 0 ~ q ~ - 1 . We govern the features of the derived cosmological model in view of the hybrid law inflation in bounce form (which involve power and de-Sitter cosmology) for the average scale factor. Also discussed the singularity of the model with the help of curvature of the model. It is observed that the model is fully engaged with both matter which exist initially for short expansion and dark energy dominated era and rests existing in quintessence dominated era and for sufficiently large time derived model forecasts that the anisotropy of the model will damp out and the Universe will turn out to be isotropic one and also observed that for quintessence dominated era the temperature and entropy density of anticipated model are positive definite along with some physical and kinematical parameters of the bounce model is also discussed in details.'
response = u'From: Edw\xeen H\xe8nn\xebk\u0119n\nAddress: cgrant@cfa.harvard.edu\n\nAbstract data for new record. Summary:\n\nCollection: astronomy\n\n%R 2020DPS....5241201D\n%T tittle\n%A Dunham, D.;Dunham, J.;Buie, M.;Preston, S.;Herald, D.;Farnocchia, D.;Giorgini, J.;Arai, T.;Aissa, B.\n%D 2020-10-00\n%J AAS Division of Planetary Science meeting #52, id. 412.01. Bulletin of the American Astronomical Society, Vol. 52, No. 6 e-id 2020n6i412p01\n%B wonderful new abstract'
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ alembic==0.8.1
redis==2.10.5
flask-consulate==0.1.2
email-validator==1.1.0
jsondiff==1.2.0

0 comments on commit abbd40c

Please sign in to comment.