def search(collection, p, of, ot, so, rm): """Render search page.""" from invenio.legacy.search_engine import perform_request_search if 'action_browse' in request.args \ or request.args.get('action', '') == 'browse': return browse() if 'c' in request.args and len(request.args) == 1 \ and len(request.args.getlist('c')) == 1: return redirect(url_for('.collection', name=request.args.get('c'))) argd = argd_orig = wash_search_urlargd(request.args) argd['of'] = 'id' # fix for queries like `/search?p=+ellis` if 'p' in argd: argd['p'] = argd['p'].strip() # update search arguments with the search user preferences if 'rg' not in request.values and current_user.get('rg'): argd['rg'] = int(current_user.get('rg')) rg = int(argd['rg']) collection_breadcrumbs(collection) qid = get_search_query_id(**argd) recids = perform_request_search(req=request.get_legacy_request(), **argd) # back-to-search related code if request and not isinstance(request.get_legacy_request(), cStringIO.OutputType): # store the last search results page session['websearch-last-query'] = request.get_legacy_request() \ .unparsed_uri hit_limit = current_app.config['CFG_WEBSEARCH_PREV_NEXT_HIT_LIMIT'] if len(recids) > hit_limit: last_query_hits = None else: last_query_hits = recids # store list of results if user wants to display hits # in a single list, or store list of collections of records # if user displays hits split by collections: session["websearch-last-query-hits"] = last_query_hits ctx = dict( facets=facets.get_facets_config(collection, qid), records=len(get_current_user_records_that_can_be_displayed(qid)), qid=qid, rg=rg, create_nearest_terms_box=lambda: _create_neareset_term_box(argd_orig), easy_search_form=EasySearchForm(csrf_enabled=False), ot=ot ) return response_formated_records(recids, collection, of, **ctx)
def get_fs(self, uri): service_name = uri.split("://")[0] root = uri.split("://")[1] cloudutils_settings = current_user.get('cloudutils_settings', {}) credentials = cloudutils_settings.get(service_name, {}) #Fix, if credentials are None, this will never happen when using this # software but can happen if someone manually sets stuff if credentials == None: credentials = {} #Get the root directory, every user can have his own root directory inside of # every service settings if(root == None or root == "/" or root == ""): root = credentials.get("root", "/") Factory = FACTORIES.get(service_name)() callback_url = url_for('cloudutils.callback', service=service_name, _external = True) filesystem = Factory.build_fs(current_user, credentials, root, callback_url, request, session ) return filesystem
def set_vote_status(self, vote, user=current_user): # XXX: TODO: Really needs error catching if not vote: # unvote Vote.query.filter_by(voter=current_user.get(), votee=self).delete() current_app.logger.debug( ("Unvoting on {} as user " "{}").format(self.__class__.__name__, current_user.username)) return True else: # vote current_app.logger.debug( ("Voting on {} as user " "{}").format(self.__class__.__name__, current_user.username)) vote = Vote(voter=current_user.get(), votee_id=self.id) vote.save(sqlalchemy.exc.IntegrityError) return True
def create(): TripForm = models.trip_form(request.form) if request.method=="POST" and TripForm.validate(): app.logger.debug(request.form) newTrip = models.Trip() newTrip.startdate = request.form.get('startdate') newTrip.enddate = request.form.get('enddate') newTrip.location = request.form.get('location') newTrip.reminder = request.form.get('reminder') newTrip.tripname = request.form.get('tripname') newTrip.user = current_user.get() try: newTrip.save() except: e = sys.exc_info() app.logger.error(e) return redirect('/create') else: templateData = { 'allTrip' : models.Trip.objects(user=current_user.id), 'current_user' : current_user, 'form' : TripForm, 'formType' : 'New' } return render_template('create.html', **templateData)
def admin_main(): contentForm = models.content_form(request.form) if request.method == "POST" and contentForm.validate(): app.logger.debug(request.form) newContent = models.Content() newContent.title = request.form.get('title') newContent.content = request.form.get('content') #link to current user newContent.user = current_user.get() try: newContent.save() except: e = sys.exc_info() app.logger.error(e) return redirect('/admin') else: templateData = { 'allContent': models.Content.objects(user=current_user.id), 'current_user': current_user, 'form': contentForm, 'formType': 'New' } return render_template('admin.html', **templateData)
def view_api(): url = request.args.get('url') if url: result = _link_sheet(url) if not result: return render_template('viewapi.html', title="Invalid spreadsheet URL"), 400 key = result.get('key') return redirect(get_url('viewapi', secure=True, key=key)) else: spreadsheets = current_user.get('spreadsheets', []) key = request.args.get('key') user = _user_for_key(key) if user == current_user: sheetobj = next(i for i in spreadsheets if i['key'] == key) writekey = sheetobj['writekey'] title = "Your \"%s\" spreadsheet is ready" % sheetobj['title'] else: key=None writekey=None title="Your Spreadsheets" # setup google filepicker browser_api_key=settings.GOOGLE_BROWSER_API_KEY client_id=settings.GOOGLE_CLIENT_ID filepicker = request.args.get('filepicker') if filepicker: title="Pick a Spreadsheet" else: filepicker="" return render_template('viewapi.html', filepicker=filepicker, key=key, writekey=writekey, title=title, spreadsheets=spreadsheets, browser_api_key=browser_api_key, client_id=client_id)
def admin_main(): contentForm = models.content_form(request.form) if request.method=="POST" and contentForm.validate(): app.logger.debug(request.form) newContent = models.Content() newContent.title = request.form.get('title') newContent.content = request.form.get('content') #link to current user newContent.user = current_user.get() try: newContent.save() except: e = sys.exc_info() app.logger.error(e) return redirect('/admin') else: templateData = { 'allContent' : models.Content.objects(user=current_user.id), 'current_user' : current_user, 'form' : contentForm, 'formType' : 'New' } return render_template('admin.html', **templateData)
def is_record_editable(recid): if current_user.is_super_admin: return True if current_user.is_guest: return False (domain, owner_email, is_private, admin_can_edit_published_record) = _get_record_info(recid) # if private record, allow owner of the record if is_private and current_user['email'] == owner_email: return True # the user's groups are not updated unless we call reload() current_user.reload() if get_domain_admin_group(domain) in current_user.get('group', []): # if the current user is community admin if is_private: # allow community admin to edit private records return True if admin_can_edit_published_record: # some domains allow community admin to edit public records return True return False
def view_api(): url = request.args.get('url') if url: result = _link_sheet(url) if not result: return render_template('viewapi.html', title="Invalid spreadsheet URL"), 400 key = result.get('key') return redirect(get_url('viewapi', secure=True, key=key)) else: spreadsheets = current_user.get('spreadsheets', []) key = request.args.get('key') user = _user_for_key(key) if user == current_user: sheetobj = next(i for i in spreadsheets if i['key'] == key) writekey = sheetobj['writekey'] title = "Your \"%s\" spreadsheet is ready" % sheetobj['title'] else: key = None writekey = None title = "Your Spreadsheets" return render_template('viewapi.html', key=key, writekey=writekey, title=title, spreadsheets=spreadsheets)
def postfeedback(): """Handler to create a ticket from user feedback.""" feedback = request.form.get('feedback') if feedback == '': response = jsonify(success=False) response.status_code = 400 return response replytoaddr = request.form.get('replytoaddr', None) if replytoaddr is None: replytoaddr = current_user.get('email') if replytoaddr == '': response = jsonify(success=False) response.status_code = 403 return response content = '''Feedback: {feedback}'''.format(feedback=feedback) if send_email(fromaddr=cfg['CFG_SITE_SUPPORT_EMAIL'], toaddr=cfg['INSPIRELABS_FEEDBACK_EMAIL'], subject='INSPIRE Labs feedback', content=content, replytoaddr=replytoaddr, attachments=[]): return jsonify(success=True) else: response = jsonify(success=False) response.status_code = 500 return response
def newtrip(): ItemsForm = models.items_form(request.form) if request.method=="POST" and itemsForm.validate(): app.logger.debug(request.form) newItems = models.Items() newItems.item = request.form.get('item') newItems.itemslist = request.form.getlist('itemslist') newItems.quantity = request.form.get('quantity') #link to current user newItems.user = current_user.get() try: newItems.save() except: e = sys.exc_info() app.logger.error(e) return redirect('newtrip') else: templateData = { 'allItems' : models.Items.objects(user=current_user.id), 'current_user' : current_user, 'itemlists': itemlists, 'form' : ItemsForm, 'formType' : 'New' } return render_template('new_trip.html', **templateData)
def postfeedback(): """Handler to create a ticket for user feedback.""" subject = "INSPIRE Labs feedback" feedback = json.loads(request.form.get("data")) content = """ Feedback: {feedback} """.format( feedback=feedback ) # fd, temp_path = mkstemp(suffix=".png") # fh = os.fdopen(fd, "wb") # fh.write("".join(feedback_data[1].split(",")[1:]).decode('base64')) # fh.close() # attachments = [temp_path] attachments = [] if send_email( fromaddr=cfg["CFG_SITE_SUPPORT_EMAIL"], toaddr=cfg["INSPIRELABS_FEEDBACK_EMAIL"], subject=subject, content=content, replytoaddr=current_user.get("email"), attachments=attachments, ): return json.dumps({"success": True}), 200, {"ContentType": "application/json"} else: return json.dumps({"success": False}), 500, {"ContentType": "application/json"}
def set_report_status(self, reason, user=current_user): # XXX: TODO: Really needs error catching if not reason: # unreport Report.query.filter_by(reporter=current_user.get(), reportee=self).delete() current_app.logger.debug( ("Unreporting on {} as user " "{}").format(self.__class__.__name__, current_user.username)) return True else: # report them, update reason if needed current_app.logger.debug( ("Voting on {} as user " "{}").format(self.__class__.__name__, current_user.username)) report = Report(reporter_id=current_user.get().id, reportee_id=self.id, reason=reason) db.session.merge(report) return True
def unlink_gspread(key): user = _user_for_key(key) if not user: abort(404) sheetobj = next(i for i in user['spreadsheets'] if i['key'] == key) sheets = current_user.get('spreadsheets') or [] sheets.remove(sheetobj) current_user['spreadsheets'] = sheets current_user.save() return jsonify({'count': len(sheets)})
def report_status(self): if not current_user.is_anonymous(): report = Report.query.filter_by(reporter=current_user.get(), reportee=self).first() if report: return report.reason else: return False else: return None
def angular_root(path=None): logged_in = "true" if current_user.is_authenticated() else "false" user = get_joined(current_user.get()) if current_user.is_authenticated() else "undefined" # re-encode our flash messages and pass them to angular for display messages = session.pop('_messages', None) if messages is None: messages = [] return render_template('base.html', logged_in=logged_in, user=user, messages=messages)
def post(self): parser.parse_args() try: req = request.get_json() req['org_id'] = current_user.get('org_id', None) obj = model.Dataset(**req) except ValidationError as e: abort_on_validation_error(e) datasets[obj.id] = obj return obj.to_json()
def rss(collection, p, jrec, so, rm): from invenio.legacy.search_engine import perform_request_search of = 'xr' argd = wash_search_urlargd(request.args) argd['of'] = 'id' # update search arguments with the search user preferences if 'rg' not in request.values and current_user.get('rg'): argd['rg'] = current_user.get('rg') rg = int(argd['rg']) qid = get_search_query_id(**argd) recids = perform_request_search(req=request.get_legacy_request(), **argd) if so or rm: recids.reverse() ctx = dict(records=len(get_current_user_records_that_can_be_displayed(qid)), qid=qid, rg=rg) return response_formated_records(recids, collection, of, **ctx)
def sanger_order(): sender_email = current_user.get('email', '*****@*****.**') # Send an email with Sanger sequencing order msg = Message( 'Sanger sequencing of ' + request.form['hgnc_symbol'], sender=current_app.config.get('MAIL_USERNAME'), recipients=current_app.config.get('MAIL_RECEPIENTS'), cc=[sender_email], bcc=['*****@*****.**'] ) msg.html = request.form['message'] mail.send(msg) return jsonify(message=msg.html)
def current(): _sf_guard_user = current_user.get() _sf_guard_user_fields = [ 'username','created_at','last_login','is_active','is_super_admin', ] user = { 'id':current_user.id, 'permissions':current_user.get_permissions(), } for _field in _sf_guard_user_fields: user[_field] = getattr(_sf_guard_user,_field,None) if _sf_guard_user.is_super_admin: with thrift_client('eus') as eus: permissions = eus.walle_get_all_permissions() permissions = [p.name for p in permissions] user['permissions'] = permissions return user
def issues(issue_id=None): if request.method == 'POST': # Submit an issue to the Scout repo at GitHub # Build the body content body = """{body} submitted by **{author}**. """.format(body=request.json.get('body', '').encode('utf-8'), author=current_user.get('name').encode('utf-8')) # Create the new isssue issue = gh_issues.create(request.json['title'], body) return jsonify(id=issue.number, body=issue.body, title=issue.title, html=issue.body_html, url=issue.html_url) elif request.method == 'GET': if issue_id: # Find an existing issue issue = gh_issues.find(issue_id) payload = dict( id=issue.number, title=issue.title, body=issue.body, html=issue.body_html, created_at=issue.created_at.date().isoformat(), url=issue.html_url ) return jsonify(**payload) else: # Get all existing issues issues = [{ 'id': issue.number, 'title': issue.title, 'body': issue.body, 'html': issue.body_html, 'created_at': issue.created_at.date().isoformat(), 'url': issue.html_url } for issue in gh_issues.find()] return jsonify(issues=issues)
def is_record_editable(recid): if current_user.is_super_admin: return True if current_user.is_guest: return False (domain, owner_email, is_private) = _get_record_info(recid) # if private record, allow owner of the record if is_private and current_user['email'] == owner_email: return True # if private record, allow community admin # the user's groups are not updated unless we call reload() current_user.reload() if is_private and get_domain_admin_group(domain) in current_user.get('group', []): return True return False
def view_api(): url = request.args.get('url') if url: result = _link_sheet(url) if not result: return render_template('viewapi.html', title="Invalid spreadsheet URL"), 400 key = result.get('key') return redirect(get_url('viewapi', secure=True, key=key)) else: spreadsheets = current_user.get('spreadsheets', []) key = request.args.get('key') user = _user_for_key(key) if user == current_user: sheetobj = next(i for i in spreadsheets if i['key'] == key) writekey = sheetobj['writekey'] title = "Your \"%s\" spreadsheet is ready" % sheetobj['title'] else: key=None writekey=None title="Your Spreadsheets" return render_template('viewapi.html', key=key, writekey=writekey, title=title, spreadsheets=spreadsheets)
def get_locale(): """Compute the language needed to return the answer to the client.""" from invenio.base.i18n import wash_language required_ln = None passed_ln = request.values.get('ln', type=str) if passed_ln: ## If ln is specified explictly as a GET or POST argument ## let's take it! required_ln = wash_language(passed_ln) if passed_ln != required_ln: ## But only if it was a valid language required_ln = None if required_ln is None and 'ln' not in session: ## If there is no language saved into the session... user_language = current_user.get('language') if user_language: ## ... and the user is logged in, we try to take it from its ## settings. required_ln = user_language else: ## Otherwise we try to guess it from its request headers for value, quality in request.accept_languages: value = str(value) ln = wash_language(value) if ln == value or ln[:2] == value[:2]: required_ln = ln break else: ## Too bad! We stick to the default :-) required_ln = current_app.config.get('CFG_SITE_LANG') elif required_ln is None: required_ln = session.get('ln') assert required_ln is not None if required_ln != session.get('ln', current_app.config.get('CFG_SITE_LANG')): session['ln'] = required_ln return required_ln
def view_api(): url = request.args.get('url') if url: result = _link_sheet(url) if not result: return render_template('viewapi.html', title="Invalid spreadsheet URL"), 400 key = result.get('key') return redirect(get_url('viewapi', secure=True, key=key)) else: spreadsheets = current_user.get('spreadsheets', []) key = request.args.get('key') user = _user_for_key(key) if user == current_user: sheetobj = next(i for i in spreadsheets if i['key'] == key) writekey = sheetobj['writekey'] title = "Your \"%s\" spreadsheet is ready" % sheetobj['title'] else: key = None writekey = None title = "Your Spreadsheets" # setup google filepicker browser_api_key = settings.GOOGLE_BROWSER_API_KEY client_id = settings.GOOGLE_CLIENT_ID filepicker = request.args.get('filepicker') if filepicker: title = "Pick a Spreadsheet" else: filepicker = "" return render_template('viewapi.html', filepicker=filepicker, key=key, writekey=writekey, title=title, spreadsheets=spreadsheets, browser_api_key=browser_api_key, client_id=client_id)
def _link_sheet(url): ss = _ss_for_user(current_user, url) if ss: spreadsheets = current_user.get('spreadsheets', []) try: sheetobj = next(i for i in spreadsheets if i.get('id') == ss.id) except StopIteration: # create new sheet obj sheetobj = { 'url': url, 'key': shortuuid.uuid(), 'writekey': shortuuid.uuid(), 'id': ss.id, } spreadsheets.append(sheetobj) current_user['spreadsheets'] = spreadsheets sheetobj['title'] = ss.title current_user.save() return sheetobj else: return None
def remote_api(path): # Route incoming request to Tornado try: cookie = {'institute': ','.join(current_user.get('institutes', []))} except AttributeError: return jsonify(error='You are not logged in!'), 403 # Double check that user has access to the institute institute = request.args.get('institute') unauthorized = institute not in cookie['institute'].split(',') if request.args.get('institute') and unauthorized: return Response('Error'), 401 TORNADO_PORT = current_app.config.get('TORNADO_PORT') url = 'http://clinical-db.scilifelab.se:{port}/{path}?{query}'\ .format(port=TORNADO_PORT, path=path, query=request.query_string) mimetype = 'application/json' if request.method == 'GET': r = requests.get(url, cookies=cookie) elif request.method == 'POST': # POST request headers = {'Content-type': 'application/json'} r = requests.post(url, data=json.dumps(request.form.to_dict()), cookies=cookie, headers=headers) elif request.method == 'DELETE': r = requests.delete(url, cookies=cookie) else: return jsonify(error='Not a valid REST method.') # Send JSON response return Response(r.text, mimetype=mimetype), r.status_code, dict(r.headers)
def index(): # load plugins plugins = filter(lambda x: x.is_authorized and x.widget, map(lambda x: x(), _USER_SETTINGS.values())) closed_plugins = [] plugin_sort = (lambda w, x: x.index(w.name) if w.name in x else len(x)) dashboard_settings = current_user.get('dashboard_settings', {}) if current_user.is_super_admin: # Check for a new release of Invenio from invenio.ext.script import check_for_software_updates check_for_software_updates(flash_message=True) if dashboard_settings: order_left = dashboard_settings.get('orderLeft', []) or [] order_middle = dashboard_settings.get('orderMiddle', []) or [] order_right = dashboard_settings.get('orderRight', []) or [] extract_plugins = lambda x: [p for p in plugins if p.name in x if p] plugins_left = sorted(extract_plugins(order_left), key=lambda w: plugin_sort(w, order_left)) plugins_middle = sorted(extract_plugins(order_middle), key=lambda w: plugin_sort(w, order_middle)) plugins_right = sorted(extract_plugins(order_right), key=lambda w: plugin_sort(w, order_right)) closed_plugins = [p for p in plugins if not p in plugins_left and not p in plugins_middle and not p in plugins_right] plugins = [plugins_left, plugins_middle, plugins_right] else: plugins = sorted(plugins, key=lambda w: plugin_sort(w, plugins)) plugins = [plugins[i:i+3] for i in range(0, len(plugins), 3)] return render_template('accounts/index.html', plugins=plugins, closed_plugins=closed_plugins)
def create(): # get List form from models.py based on the photo photo_upload_list = models.photo_upload_list(request.form) # if form was submitted and it is valid... if request.method == "POST" and photo_upload_list.validate(): uploaded_file = request.files["photoupload"] # Uploading is fun # 1 - Generate a file name with the datetime prefixing filename # 2 - Connect to s3 # 3 - Get the s3 bucket, put the file # 4 - After saving to s3, save data to database if uploaded_file and allowed_file(uploaded_file.filename): # create filename, prefixed with datetime now = datetime.datetime.now() filename = now.strftime("%Y%m%d%H%M%s") + "-" + secure_filename(uploaded_file.filename) # thumb_filename = now.strftime('%Y%m%d%H%M%s') + "-" + secure_filename(uploaded_file.filename) # connect to s3 s3conn = boto.connect_s3(os.environ.get("AWS_ACCESS_KEY_ID"), os.environ.get("AWS_SECRET_ACCESS_KEY")) # open s3 bucket, create new Key/file # set the mimetype, content and access control b = s3conn.get_bucket(os.environ.get("AWS_BUCKET")) # bucket name defined in .env k = b.new_key(b) k.key = filename k.set_metadata("Content-Type", uploaded_file.mimetype) k.set_contents_from_string(uploaded_file.stream.read()) k.make_public() # save information to MONGO database # did something actually save to S3 if k and k.size > 0: createList = models.List() createList.listName = request.form.get("listName") createList.slug = slugify(createList.listName) createList.listDescription = request.form.get("listDescription") createList.postedby = request.form.get("postedby") createList.filename = filename # same filename of s3 bucket file # link to current user createList.user = current_user.get() try: createList.save() except: e = sys.exc_info() app.logger.error(e) return redirect("/create") else: return "uhoh there was an error " + uploaded_file.filename else: # get existing listsCreated listsCreated = models.List.objects.order_by("timestamp") # render the template templateData = { "current_user": current_user, "users": models.User.objects(), "listsCreated": listsCreated, "form": photo_upload_list, } app.logger.debug(current_user) return render_template("09create_list.html", **templateData)
def get_timezone(): """Returns timezone from user settings.""" return current_user.get('timezone')
def vote_status(self): if not current_user.is_anonymous(): return bool(Vote.query.filter_by(voter=current_user.get(), votee=self).first())
def index(): user_tz = current_user.get('timezone', pytz.utc) active_buffs = {} active_buff_charts = [] accepted_buffs = {} outstanding_buffs = [] all_paths = ['lastfm/scrobbles/echonest/totals', 'lastfm/scrobbles/echonest/energy/averages'] # Okay, time to add in custom paths! for datastream, href in API.get_custom_datastreams(current_user): if datastream != 'self': all_paths.append('custom/' + datastream + '/totals') all_paths.append('custom/' + datastream + '/averages') group_by_intervals = { "day": ["year","month","day"]#, "week": ["isoyear", "isoweek"], #"month": ["year", "month"], "year": ["year"] } continuous = True today = datetime.datetime.now(pytz.utc).replace( hour = 0, minute = 0, second = 0, microsecond = 0) # Keeping track of the max_end and min_start dates of the user's accepted # buffs so we can determine how wide the timeline needs to be in order to # get the amount of spacing between ticks we want. max_end = None min_start = None # Grab any buffs the user has previously accepted. for buff in CorrelationBuff.find_accepted(lazy = False): buff = CorrelationBuff(**buff) chart = CorrelationBuffChart.create(current_user, buff) start_timestamp = int(time.mktime( datetime.datetime(*buff.start, tzinfo = pytz.utc).timetuple())) # If the buff does not have a non-null end date, then we will just use # today. Null end dates indicate active buffs. if buff['end']: end_timestamp = int(time.mktime( pytz.UTC.localize(buff.end).timetuple())) else: end_timestamp = int(time.mktime( datetime.datetime.now(pytz.utc).timetuple())) active_buffs[buff.key] = buff active_buff_charts.append(chart) if max_end == None or end_timestamp > max_end: max_end = end_timestamp if min_start == None or start_timestamp < min_start: min_start = start_timestamp # Set up the JSON data the timeline expects. if buff.type_key not in accepted_buffs: accepted_buffs[buff.type_key] = { "icon": "/static/images/buffs/%s" % chart['icon'], "times": []} # Multiplying timestamps by 1000 because the timeline expects # milliseconds and Python uses seconds. accepted_buffs[buff.type_key]["times"].append({ "starting_time": start_timestamp * 1000, "ending_time": end_timestamp * 1000, "correlation": "%s%%" % int(round(buff["correlation"]*100)), "chart_data": chart["chart_data"], "chart_id": chart["chart_id"], "text": chart['text'], "title": chart['title'] }) # Grab any correlations the user has yet to accept or decline. outstanding_buffs = CorrelationBuff.find_outstanding(lazy = False) # Grab the latest buff we logged, whether it was accepted or not. last_buff = CorrelationBuff.find_one(None, sort = [('$end', pymongo.DESCENDING)]) a_year_ago = (today - datetime.timedelta(days = 365)) if last_buff: last_buff['end'] = pytz.UTC.localize(last_buff['end']) start_date = max(a_year_ago, last_buff['end'] + datetime.timedelta(days = 1)) else: start_date = a_year_ago # If there were active buffs, we want to ask for correlations from the start # date of the earliest one. That way we can see if the active buffs have # extended out any further. This may, of course, result in some completed # buffs being returned *again*, so we'll have to filter those out by making # sure we don't include any correlations with an end date before the end # date of last_buff. correlation_start_date = today if active_buffs: for buff in active_buffs.values(): buff_start = datetime.datetime(*buff['start'], tzinfo = pytz.utc) if buff_start < correlation_start_date: correlation_start_date = buff_start else: correlation_start_date = start_date # Look for new correlations. for interval, group_by in group_by_intervals.items(): intervalString = ",".join(sorted(group_by)) # Look for new correlations in all combinations of paths. for paths in itertools.combinations(all_paths, 2): # Just grabbing the correlations for this user from the last 30 days # or since the last logged buff, whichever is later. new_correlations = API.get_correlations(current_user, paths, group_by, correlation_start_date, continuous = True) # Cache all the new correlations and update active buff end times # where applicable. for correlation in new_correlations: end = dict(zip(correlation['group_by'], correlation['end'])) start = dict(zip(correlation['group_by'], correlation['start'])) end = datetime.datetime(end['year'], end['month'], end['day'], tzinfo = pytz.utc) start = datetime.datetime(start['year'], start['month'], start['day'], tzinfo = pytz.utc) # If the correlation end date is today, then we consider the buff # active. We want to change its end date to None. if today == end: correlation_end = None else: correlation_end = end del correlation['end'] buff = CorrelationBuff.find_or_create( user_id = current_user['_id'], **correlation) if 'end' in buff and buff['end']: buff['end'] = pytz.UTC.localize(buff['end']) # If this has an start date greater than start_date, then we know # we need to save it. If not, and if it doesn't correspond to an # active buff, then we can ignore it. The user will have already # been prompted with it, by definition. # Also, don't append outstanding buffs with IDs, since they'll # already be a part of the outstanding_buffs list due to our # earlier Pymongo query. if (start > start_date and buff['state'] == CorrelationBuff.OUTSTANDING and '_id' not in buff): outstanding_buffs.append(buff) # Update the buff end date, or save the new buff if it hasn't # been saved before. if (buff['end'] != correlation_end or '_id' not in buff): buff['end'] = correlation_end CorrelationBuff.save(buff) # Does this "new correlation" correspond to a previously accepted # active buff? Does the new correlation provide an end date for that # active buff? Then update the buff's end date. if buff.key in active_buffs and end != today: active_buffs[buff.key]['end'] = correlation_end CorrelationBuff.save(buff) # Format the correlations as buffs. outstanding_buffs = [CorrelationBuffChart.create(current_user, buff ) for buff in outstanding_buffs] return render_template('%s/index.html' % app.name, active_buff_charts = active_buff_charts, outstanding_buffs = outstanding_buffs, accepted_buffs = json.dumps(accepted_buffs.values() ) if accepted_buffs else None, OUTSTANDING = CorrelationBuff.OUTSTANDING, timeline_width = ((max_end - min_start)/10000 ) if max_end and min_start else None, start_year = (datetime.datetime.fromtimestamp(min_start).year ) if min_start else None)
def wrapper(*args, **kwargs): if app.config.get('PRIVATE') and not current_user.is_authenticated: return loginmanager.unauthorized() if not 'admin' in current_user.get('roles'): return loginmanager.unauthorized() return f(*args, **kwargs)