def check_permissions(*args, **kwargs): # sys.stderr.write("about to check permissions sys\n") path = request.path method = request.method if method == 'GET' and path in ( '/health', '/bmreports', '/elexonportal/file/download/BESTVIEWPRICES_FILE'): return g.user = None user = None auth = request.authorization if auth is not None: pword_digest = hashlib.md5(auth.password).hexdigest() user = User.query.filter( User.email_address == auth.username, User.password_digest == pword_digest).first() if user is None: config_contract = Contract.get_non_core_by_name('configuration') try: email = config_contract.make_properties()['ips'][ request.remote_addr] user = User.query.filter(User.email_address == email).first() except KeyError: pass if user is not None: g.user = user role = user.user_role role_code = role.code path = request.path if role_code == "viewer": if path.startswith("/chellow/reports/") and \ path.endswith("/output/") and method in ("GET", "HEAD"): return elif role_code == "editor": return elif role_code == "party-viewer": if method in ("GET", "HEAD"): party = user.party market_role_code = party.market_role.code if market_role_code == 'C': hhdc_contract_id = GET_int("hhdc-contract-id") hhdc_contract = Contract.get_hhdc_by_id(hhdc_contract_id) if hhdc_contract.party == party and ( request.path + "?" + request.query_string) \ .startswith( "/chellow/reports/37/output/?" "hhdc-contract-id=" + str(hhdc_contract.id)): return elif market_role_code == 'X': if path.startswith( "/supplier_contracts/" + party.id): return if user is None: return Response( 'Could not verify your access level for that URL.\n' 'You have to login with proper credentials', 401, {'WWW-Authenticate': 'Basic realm="Chellow"'}) else: return Response('Forbidden', 403)
def content(contract_id, end_year, end_month, months, user): caches = {} sess = f = None try: sess = Session() contract = Contract.get_hhdc_by_id(sess, contract_id) finish_date = Datetime(end_year, end_month, 1, tzinfo=pytz.utc) + \ relativedelta(months=1) - HH start_date = Datetime(end_year, end_month, 1, tzinfo=pytz.utc) - \ relativedelta(months=months - 1) forecast_date = chellow.computer.forecast_date() running_name, finished_name = chellow.dloads.make_names( 'hhdc_virtual_bills.csv', user) f = open(running_name, mode='w', newline='') writer = csv.writer(f, lineterminator='\n') bill_titles = chellow.computer.contract_func( caches, contract, 'virtual_bill_titles')() header_titles = [ 'Import MPAN Core', 'Export MPAN Core', 'Start Date', 'Finish Date'] vb_func = chellow.computer.contract_func( caches, contract, 'virtual_bill') writer.writerow(header_titles + bill_titles) for era in sess.query(Era).distinct().filter( or_(Era.finish_date == null(), Era.finish_date >= start_date), Era.start_date <= finish_date, Era.hhdc_contract == contract).order_by(Era.supply_id): imp_mpan_core = era.imp_mpan_core if imp_mpan_core is None: imp_mpan_core_str = '' is_import = False else: is_import = True imp_mpan_core_str = imp_mpan_core exp_mpan_core = era.exp_mpan_core exp_mpan_core_str = '' if exp_mpan_core is None else exp_mpan_core chunk_start = hh_max(era.start_date, start_date) chunk_finish = hh_min(era.finish_date, finish_date) vals = [ imp_mpan_core_str, exp_mpan_core_str, hh_format(chunk_start), hh_format(chunk_finish)] supply_source = chellow.computer.SupplySource( sess, chunk_start, chunk_finish, forecast_date, era, is_import, caches) vb_func(supply_source) bill = supply_source.dc_bill for title in bill_titles: vals.append(str(bill.get(title, ''))) if title in bill: del bill[title] for k in sorted(bill.keys()): vals.append(k) vals.append(str(bill[k])) writer.writerow(vals) except BadRequest as e: f.write("Problem " + e.description + traceback.format_exc() + '\n') except: msg = "Problem " + traceback.format_exc() + '\n' f.write(msg) finally: f.close() os.rename(running_name, finished_name) if sess is not None: sess.close()
def content(contract_id, days_hidden, user): sess = f = writer = None try: sess = Session() running_name, finished_name = chellow.dloads.make_names( 'channel_snags.csv', user) f = open(running_name, mode='w', newline='') writer = csv.writer(f, lineterminator='\n') writer.writerow( ( 'Hidden Days', 'Chellow Id', 'Imp MPAN Core', 'Exp MPAN Core', 'Site Code', 'Site Name', 'Snag Description', 'Import Related?', 'Channel Type', 'Start Date', 'Finish Date', 'Days Since Snag Finished', 'Duration Of Snag (Days)', 'Is Ignored?')) contract = Contract.get_hhdc_by_id(sess, contract_id) now = Datetime.now(pytz.utc) cutoff_date = now - relativedelta(days=days_hidden) for snag, channel, era, supply, site_era, site in sess.query( Snag, Channel, Era, Supply, SiteEra, Site).join( Channel, Era, Supply, SiteEra, Site).filter( SiteEra.is_physical == true(), Era.hhdc_contract == contract, Snag.start_date < cutoff_date).order_by( Site.code, Supply.id, Channel.imp_related, Channel.channel_type, Snag.description, Snag.start_date, Snag.id): snag_start = snag.start_date snag_finish = snag.finish_date if snag_finish is None: snag_finish_str = '' duration = now - snag_start age_of_snag = datetime.timedelta(0) else: snag_finish_str = snag_finish.strftime("%Y-%m-%d %H:%M") duration = snag_finish - snag_start age_of_snag = now - snag_finish writer.writerow( ( str(days_hidden), str(snag.id), '' if era.imp_mpan_core is None else era.imp_mpan_core, '' if era.exp_mpan_core is None else era.exp_mpan_core, site.code, site.name, snag.description, str(channel.imp_related), channel.channel_type, snag_start.strftime("%Y-%m-%d %H:%M"), snag_finish_str, str(age_of_snag.days + age_of_snag.seconds / (3600 * 24)), str(duration.days + duration.seconds / (3600 * 24)), str(snag.is_ignored))) except: msg = traceback.format_exc() sys.stderr.write(msg) writer.writerow([msg]) finally: if sess is not None: sess.close() if f is not None: f.close() os.rename(running_name, finished_name)