def settings(path='/'): if current_user.is_anonymous(): abort(401) if path != '/': path = '/' + path next = util.is_safe_url(request.values.get('next', path)) record = models.Pages.pull_by_url(next) if record is None: rec = {'url': next, 'author': current_user.id} else: rec = record.data if request.method == 'GET': if record is None: flash('There is no page here yet - create one.') return render_template('pagemanager/settings.html', tags=json.dumps(dropdowns( 'pagemanager', 'tags')), urls=json.dumps(dropdowns('pagemanager', 'url')), record=rec) elif (request.method == 'DELETE' or (request.method == 'POST' and request.values.get('submit', False) == 'Delete')): if record is None: abort(404) else: _sync_delete(record) flash('Page deleted') return redirect("/") elif request.method == 'POST': if record is None: record = models.Pages() if request.values.get('template', False): tmpl = models.Pages.pull_by_url(request.values['template']) if tmpl is not None and 'content' in tmpl.data: record.data['content'] = tmpl.data['content'] newdata = request.json if request.json else request.values # check the new url does not overwrite an already present url if 'url' not in newdata: flash("A URL is required.") elif (record.data.get('url', '') != newdata['url'] and models.Pages.pull_by_url(newdata['url']) is not None): # update record data so form is up to date but don't save it record.update_from_form(request) flash( "There is already a page present at the URL you specifed. You must delete that page first." ) else: record.save_from_form(request) flash('Settings updated') time.sleep(1) return render_template('pagemanager/settings.html', tags=json.dumps(dropdowns('pages', 'tags')), urls=json.dumps(dropdowns('pages', 'url')), record=record.data)
def settings(path="/"): if current_user.is_anonymous(): abort(401) if path != "/": path = "/" + path next = util.is_safe_url(request.values.get("next", path)) record = models.Pages.pull_by_url(next) if record is None: rec = {"url": next, "author": current_user.id} else: rec = record.data if request.method == "GET": if record is None: flash("There is no page here yet - create one.") return render_template( "pagemanager/settings.html", tags=json.dumps(dropdowns("pagemanager", "tags")), urls=json.dumps(dropdowns("pagemanager", "url")), record=rec, ) elif request.method == "DELETE" or (request.method == "POST" and request.values.get("submit", False) == "Delete"): if record is None: abort(404) else: _sync_delete(record) flash("Page deleted") return redirect("/") elif request.method == "POST": if record is None: record = models.Pages() if request.values.get("template", False): tmpl = models.Pages.pull_by_url(request.values["template"]) if tmpl is not None and "content" in tmpl.data: record.data["content"] = tmpl.data["content"] newdata = request.json if request.json else request.values # check the new url does not overwrite an already present url if "url" not in newdata: flash("A URL is required.") elif record.data.get("url", "") != newdata["url"] and models.Pages.pull_by_url(newdata["url"]) is not None: # update record data so form is up to date but don't save it record.update_from_form(request) flash("There is already a page present at the URL you specifed. You must delete that page first.") else: record.save_from_form(request) flash("Settings updated") time.sleep(1) return render_template( "pagemanager/settings.html", tags=json.dumps(dropdowns("pages", "tags")), urls=json.dumps(dropdowns("pages", "url")), record=record.data, )
def index(): #logofolder = os.path.dirname(os.path.abspath( __file__ )) + '/static/logos' #logos=sorted(os.listdir(logofolder)) logos = [] return render_template( 'index.html', logos=logos, orgs=json.dumps(dropdowns('record','collaboratorOrganisation.canonical')) ) return render_template('index.html')
def organisations(): """ Any request to the base of the organisations url space """ if 'q' in request.values: # FIXME: should be in the models layer query = { "query" : { "query_string" : { "query" : "collaboratorOrganisation.canonical:*" + request.values['q'] + "*" } }, "size" : 0, "facets" : { "orgs" : { "terms" : { "field" : "collaboratorOrganisation.canonical.exact", "size" : 25, "script" : "term.toLowerCase() contains '" + request.values['q'].lower() + "'" } } } } r = models.Record.query(q=query) resp = make_response(json.dumps(r['facets']['orgs']['terms'])) resp.mimetype = "application/json" return resp else: #logofolder = os.path.dirname(os.path.abspath( __file__ )).replace('/view','/static/logos') #logos=sorted(os.listdir(logofolder)) logos = [] return render_template( 'organisation/orgs.html', logos=logos, orgs=json.dumps(dropdowns('record','collaboratorOrganisation.canonical')) )
def settings(path='/'): if not current_user.is_super: abort(401) if path != '/': path = '/' + path next = util.is_safe_url(request.values.get('next',path)) record = models.Pages.pull_by_url(next) if record is None: rec = { 'url': next, 'author': current_user.id } else: rec = record.data if request.method == 'GET': if record is None: flash('There is no page here yet - create one.','info') return render_template( 'pagemanager/settings.html', tags = json.dumps(dropdowns('pagemanager','tags')), urls = json.dumps(dropdowns('pagemanager','url')), record = rec ) elif ( request.method == 'DELETE' or (request.method == 'POST' and request.values.get('submit','').lower() == 'delete') ): if record is None: abort(404) else: _sync_delete(record) flash('Page deleted') return redirect("/") elif request.method == 'POST': if record is None: record = models.Pages() if request.values.get('template',False): tmpl = models.Pages.pull_by_url(request.values['template']) if tmpl is not None and 'content' in tmpl.data: record.data['content'] = tmpl.data['content'] newdata = request.json if request.json else request.values # check the new url does not overwrite an already present url if 'url' not in newdata: flash("A URL is required.") elif (record.data.get('url','') != newdata['url'] and models.Pages.pull_by_url(newdata['url']) is not None ): # update record data so form is up to date but don't save it record.update_from_form(request) flash("There is already a page present at the URL you specifed. You must delete that page first.") else: record.save_from_form(request) flash('Settings updated') time.sleep(1) return render_template( 'pagemanager/settings.html', tags = json.dumps(dropdowns('pages','tags')), urls = json.dumps(dropdowns('pages','url')), record = record.data )
def adminitem(itype, iid=False): #try: klass = getattr(models, itype[0].capitalize() + itype[1:]) if iid: if iid.endswith('.json'): iid = iid.replace('.json', '') if iid == "new": rec = None else: rec = klass.pull(iid) if request.method == 'GET': if not util.request_wants_json(): return render_template( 'admin/' + itype + '.html', record=rec, dropdowns={ "clpeople": dropdowns('account', 'id'), "partners": [ i['_source']['id'] for i in models.Account.query( terms={ "partner": "yes" }, size=1000).get('hits', {}).get('hits', []) ], "seniors": [ i['_source']['id'] for i in models.Account.query( terms={ "senior": "yes" }, size=1000).get('hits', {}).get('hits', []) ], "contacts": dropdowns('project', ['externals', 'customer', 'funder']), "tags": dropdowns('project', ['tags']) }) elif rec is not None: resp = make_response(rec.json) resp.mimetype = "application/json" return resp else: abort(404) elif (rec and (request.method == 'DELETE' or (request.method == "POST" and request.form.get('submit', False).lower() == "delete"))): rec.delete() flash('Deleted') return redirect(url_for('.index')) elif request.method == 'POST': if rec is None: rec = klass() rec.save_from_form(request) if iid == "new": flash('Created') return redirect(url_for('.index') + itype + '/' + rec.id) else: flash('Updated') return redirect(url_for('.index') + itype + '/' + iid) else: return redirect(url_for('.index') + itype + '/new')
def organisation(mainorg, raw=False): """ Any request to a specific organisation's home page """ # TODO: # list all this orgs projects # list a blurb and website about this org # list the main contact of this org (and perhaps other users) # offer ability to update the page about this org # show this org snapshot data, top projects, recent funding by years, pubs # offer a download report overview of this org logo = mainorg.lower().replace(' ','_').replace("'",'').replace('university_','').replace('_university','').replace('_of','').replace('of_','').replace('_the','').replace('the_','').replace('_and','').replace('and_','').replace('_for','').replace('for_','').replace('_.','.') + '.png'; logofolder = os.path.dirname(os.path.abspath( __file__ )).replace('/view','/static/logos') logos=os.listdir(logofolder) if logo not in logos: logo = '' else: logo = '/static/logos/' + logo # logo = "" # FIXME: should be in the models layer qry = { "query": { "term": { "collaboratorOrganisation.canonical.exact": mainorg } }, "size": 1, "facets": { "collaborators":{ "terms_stats" : { "key_field" : "collaboratorOrganisation.canonical.exact", "value_field" : "project.fund.valuePounds", "size" : 0 } }, "value_stats" : { "statistical" : { "field" : "project.fund.valuePounds" } } } } r = models.Record.query(q=qry) org = { 'name': mainorg, 'logo': logo, 'projects': r.get('hits',{}).get('total',0), 'collaborators': len(r.get('facets',{}).get('collaborators',{}).get('terms',[])) - 1, 'totalfunding': "{:,.0f}".format(r.get('facets',{}).get('value_stats',{}).get('total',0)) } # TODO: post codes should perhaps be processed into the index data, to save # processing them here # get post code - lat long lookup table pcll = json.load(open("postcodes.json")) try: orgs = r.get('hits',{}).get('hits',[])[0]['_source']['collaboratorOrganisation'] for o in orgs: if o['canonical'] == mainorg: outcode = o['organisation']['address']['postCode'].replace(' ','') if len(outcode) == 7: outcode = outcode[0:4] elif len(outcode) == 6: outcode = outcode[0:3] pc = pcll[outcode] org['lat'] = pc['lat'] org['lng'] = pc['lng'] except: org['lat'] = 0 org['lng'] = 0 # get similar names for user fixing # TODO: this should be in model too similar = dropdowns('record','collaboratorOrganisation.canonical') n = mainorg.lower().replace('university','').replace('college','').replace('of','').replace('the','').replace(' ','').replace('.','') checklist = [] for s in similar: if mainorg != s and n in s.lower(): checklist.append(s) # TODO: should really have an org object with the above info in it and it # should be passed to the page instead of the mainorg string if raw: return org elif util.request_wants_json(): resp = make_response(json.dumps(org)) resp.mimetype = "application/json" return resp else: return render_template('organisation/org.html', org=org, checklist=checklist)