def test_submit_report(self): # add leader leader = LeaderInfo(id='lxxx0', name='lea_http', place="desk", endpoint='http://localhost:50000', subordinates=[], missions=[]) self.app.post('/commander/subordinates', data=json.dumps(leader.to_dict()), content_type='application/json') # submit a report report = Report(purpose="some app", time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), place="desk", values="some values") response = self.app.post('/commander/subordinates/lxxx0/report', data=json.dumps(report.to_dict()), content_type='application/json') self.assertEqual(response.status_code, 200) actual = json.loads(response.data.decode("utf-8")) # assert expected = { "_status": { 'success': True, 'msg': "status is ok" }, "accepted": report.to_dict() } self.assertEqual(actual, expected)
def test_post_preview_setting(self): for x in range(1, 4): Team.create( id='00{}'.format(x), captain_id='User_cap', program_id=self.program.uid, ).put() def body(x): return { 'filename': 'file_00{}.html'.format(x), 'team_id': 'Team_00{}'.format(x), 'dataset_id': 'Dataset_00{}'.format(x), 'template': 'template.html', } headers = {'Authorization': 'Bearer ' + self.valid_jwt} preview_rsp = self.testapp.post_json('/api/reports', dict(body(1), preview=True), headers=headers) non_pre_rsp = self.testapp.post_json('/api/reports', dict(body(2), preview=False), headers=headers) default_rsp = self.testapp.post_json('/api/reports', body(3), headers=headers) preview_fetched = Report.get_by_id(json.loads(preview_rsp.body)['uid']) non_pre_fetched = Report.get_by_id(json.loads(non_pre_rsp.body)['uid']) default_fetched = Report.get_by_id(json.loads(default_rsp.body)['uid']) self.assertEqual(preview_fetched.preview, True) self.assertEqual(non_pre_fetched.preview, False) self.assertEqual(default_fetched.preview, True)
def create_reports(self): (other, teammate, contact, captain, super_admin, team, classroom) = self.create() classReport1 = Report.create( team_id=team.uid, classroom_id=classroom.uid, filename='foo.pdf', gcs_path='/mybucket/upload/12345.pdf', size=1000000, content_type='application/pdf', preview=False, ) classReport2 = Report.create( team_id=team.uid, classroom_id=classroom.uid, filename='bar.pdf', gcs_path='/mybucket/upload/23456.pdf', size=1000000, content_type='application/pdf', preview=False, ) teamReport = Report.create( team_id=team.uid, classroom_id=None, filename='team.pdf', gcs_path='/mybucket/upload/34567.pdf', size=1000000, content_type='application/pdf', preview=False, ) Report.put_multi((classReport1, classReport2, teamReport)) return (other, teammate, contact, captain, super_admin, team, classroom, classReport1, classReport2, teamReport)
def run(self): if 'timer' in self.mission.trigger.keys(): interval = self.mission.trigger['timer'] while not self.lock.wait(timeout=interval): m_id = self.mission.get_id() time = datetime.datetime.now(datetime.timezone.utc).isoformat() works = [] for sid, w in self.leader.work_cache: if w.purpose != m_id: continue works.append({ "time": w.time, "place": self.leader.subordinates[sid].place, "values": w.values }) report = Report(time, self.leader.place, self.mission.purpose, works) url = "{0}subordinates/{1}/report".\ format(self.leader.superior_ep, self.leader.id) res, err = rest.post(url, json=report.to_dict()) if err is not None: # TODO: エラー処理ちゃんとやる # 本当に接続先がダウンしてる場合、ただのDoSになってしまう logger.error('In WorkingThread, failed to post report') logger.error('> err: {0}', err) self.leader.work_cache = \ [(sid, w) for sid, w in self.leader.work_cache if w.purpose != m_id] else: pass
def test_duplicate_post(self): (other, teammate, contact, captain, super_admin, team, classroom) = self.create() today_str = datetime.date.today().strftime(config.iso_date_format) report_params = { 'filename': 'Team_filewhack.{}.html'.format(today_str), 'team_id': team.uid, 'dataset_id': 'Dataset_first', 'template': 'template.html', } response = self.testapp.post_json( '/api/reports', report_params, headers={'Authorization': 'Bearer ' + self.valid_jwt}, ) first_uid = json.loads(response.body)['uid'] self.assertIsNotNone(Report.get_by_id(first_uid)) # Post a second report with the same filename and team. This should # update the old report. report_params['dataset_id'] = 'Dataset_second' response = self.testapp.post_json( '/api/reports', report_params, headers={'Authorization': 'Bearer ' + self.valid_jwt}, ) second_uid = json.loads(response.body)['uid'] self.assertEqual(first_uid, second_uid) self.assertEqual( Report.get_by_id(first_uid).dataset_id, 'Dataset_second')
def run(self): if 'timer' in self.mission.trigger.keys(): interval = self.mission.trigger['timer'] while not self.lock.wait(timeout=interval): m_id = self.mission.get_id() time = datetime.datetime.now(datetime.timezone.utc).isoformat() works = [] for sid, w in self.leader.work_cache: if w.purpose != m_id: continue works.append({"time": w.time, "place": self.leader.subordinates[sid].place, "values": w.values}) report = Report(time, self.leader.place, self.mission.purpose, works) url = "{0}subordinates/{1}/report".\ format(self.leader.superior_ep, self.leader.id) res, err = rest.post(url, json=report.to_dict()) if err is not None: # TODO: エラー処理ちゃんとやる # 本当に接続先がダウンしてる場合、ただのDoSになってしまう logger.error('In WorkingThread, failed to post report') logger.error('> err: {0}', err) self.leader.work_cache = \ [(sid, w) for sid, w in self.leader.work_cache if w.purpose != m_id] else: pass
def create_dataset_reports(self): (other, teammate, contact, captain, super_admin, team, classroom) = self.create() classReport1 = Report.create( team_id=team.uid, classroom_id=classroom.uid, filename='foo.html', dataset_id='Dataset_class1', template='class_template', content_type='text/html', preview=False, ) classReport2 = Report.create( team_id=team.uid, classroom_id=classroom.uid, filename='bar.html', dataset_id='Dataset_class2', template='class_template', content_type='text/html', preview=False, ) teamReport = Report.create( team_id=team.uid, classroom_id=None, filename='team.html', dataset_id='Dataset_team', template='team_template', content_type='text/html', preview=False, ) Report.put_multi((classReport1, classReport2, teamReport)) return (other, teammate, contact, captain, super_admin, team, classroom, classReport1, classReport2, teamReport)
def test_rserve_skips_existing(self): program = Program.create( name="The Engagement Project", label="ep19", preview_url='foo.com', ) week = util.datelike_to_iso_string(datetime.date.today()) org = Organization.create(name="Organization", captain_id="User_cap", program_id=program.uid) org_to_skip = Organization.create(name="Organization", captain_id="User_cap", program_id=program.uid) Organization.put_multi([org, org_to_skip]) team = Team.create(name="Team", captain_id="User_cap", program_id=program.uid) team_to_skip = Team.create(name="Team", captain_id="User_cap", program_id=program.uid) Team.put_multi([team, team_to_skip]) cl = Classroom.create(name="Classroom", team_id=team.uid, code="foo", contact_id="User_contact") cl_to_skip = Classroom.create(name="Classroom", team_id=team.uid, code="foo", contact_id="User_contact") Classroom.put_multi([cl, cl_to_skip]) Report.put_multi([ Report.create(parent_id=org_to_skip.uid, filename="foo", issue_date=week), Report.create(parent_id=team_to_skip.uid, filename="foo", issue_date=week), Report.create(parent_id=cl_to_skip.uid, filename="foo", issue_date=week), ]) # Skips all the parents who have reports already this week. orgs, teams, classes = cron_rserve.get_report_parents( program, week, False) self.assertEqual(len(orgs), 1) self.assertEqual(len(teams), 1) self.assertEqual(len(classes), 1) # ...unless you force it, then they're all there. orgs, teams, classes = cron_rserve.get_report_parents( program, week, True) self.assertEqual(len(orgs), 2) self.assertEqual(len(teams), 2) self.assertEqual(len(classes), 2)
def get_report_by_name(report_name, conn, execution_id=None): cursor = conn.cursor() sql_report_by_name = "select id, name, report_query, mode, file_name, separator from Report where name = '%s';" sql_report_columns_by_id = "select sql_name, business_name, is_used_for_compare, is_business_key " \ "from Report_Columns where report_id = '%s';" # Fetch report information from persistence print "Info : Retrieving report from the database " + now() query_result = cursor.execute(sql_report_by_name % report_name).fetchone() if not query_result: print("Error : report with name '%s' not found" % report_name) exit(1) report_id = query_result["id"] report_name = query_result["name"] report_query = query_result["report_query"] report_mode = query_result["mode"] report_file_name = query_result["file_name"] report_separator = query_result["separator"] # Fetch column definitions query_result = cursor.execute(sql_report_columns_by_id % report_id).fetchall() columns_mapping = {} columns = [] for row in query_result: columns.append(row["sql_name"]) columns_mapping[row["sql_name"]] = { "business_name": row["business_name"] } columns_mapping[row["sql_name"]]["is_used_for_compare"] = str( row["is_used_for_compare"]) columns_mapping[row["sql_name"]]["is_business_key"] = str( row["is_business_key"]) report = Report(report_name, report_query, report_mode, columns, columns_mapping, report_separator, report_file_name, report_id) # Retrieve last Execution or the one requested bw user if not execution_id: execution = get_execution_by_report_id(report_id, conn) else: execution = get_execution_by_report_id(report_id, conn, execution_id) if execution: report.execution = execution else: print "Warning : empty exectution" report.execution = [] print "Info : Retrieval done " + now() return report
def submit_error(self, msg): time = datetime.datetime.now(datetime.timezone.utc).isoformat() report = Report(time=time, place="internal", purpose="_error", values=[{"type": "error_msg", "msg": msg}]) url = "{0}subordinates/{1}/report".format(self.superior_ep, self.id) rest.post(url, json=report.to_dict())
def save_report(self, *ignore): text_buffer = self.report.get_buffer() report_data = text_buffer.get_text( text_buffer.get_start_iter(), text_buffer.get_end_iter(), False) with database.transaction(): Report.create( report=report_data, created=self.datetime_create ) self.quit(self)
def submit_error(self, msg): time = datetime.datetime.now(datetime.timezone.utc).isoformat() report = Report(time=time, place="internal", purpose="_error", values=[{ "type": "error_msg", "msg": msg }]) url = "{0}subordinates/{1}/report".format(self.superior_ep, self.id) rest.post(url, json=report.to_dict())
def report_bathroom_by_id(self, id, description): if id is None or not description: return False b = Bathroom.query.filter_by(id=id).first() if b is None: return False rep = Report() rep.bathroom = b rep.description = description db.session.add(rep) db.session.commit() return True
def get(self): error = urllib.unquote(self.request.get('error')) startDate = get_startDay() reports = Report().all().filter('title', title) reports.order('-date') reports.filter('date >' ,startDate) template_values = { 'title' : title, 'e_msg':error, 'reports': simplejson.dumps([r.to_dict() for r in reports]), 'startDate':startDate } path = os.path.join(os.path.dirname(__file__), 'index.html') self.response.out.write(template.render(path, template_values))
def __init__(self, width=400, height=300, *args, **kwargs): super(ReporterWindow, self).__init__(*args, **kwargs) locale.setlocale(locale.LC_ALL, '') self.datetime_create = datetime.now() Report.create_table_if_not_exist() self.set_size_request(width, height) self.set_border_width(self.padding) self.set_position(Gtk.WindowPosition.CENTER) self.set_title(self.title) self.create_widgets() self.connect('key-press-event', self.handle_keys) self.show_all()
def get_team_reports(self, team_id): user = self.get_current_user() team = Team.get_by_id(team_id) if not team: return self.http_not_found() if not owns(user, team) and not has_captain_permission(user, team): return self.http_forbidden("Only team members can list reports.") # Limit access to preview reports, super admin only. show_preview_reports = True if user.super_admin else False # Returns both team- and class-level reports. all_reports = Report.get_for_team(team.uid, show_preview_reports) # Any team member can see the team reports... allowed_reports = [r for r in all_reports if not r.classroom_id] # ...but limit access to classroom reports. classroom_reports = [r for r in all_reports if r.classroom_id] classroom_ids = [r.classroom_id for r in classroom_reports] classrooms = {c.uid: c for c in Classroom.get_by_id(classroom_ids)} for report in classroom_reports: if has_contact_permission(user, classrooms[report.classroom_id]): allowed_reports.append(report) # Add a custom link for users to access each report. self.write([ dict(r.to_client_dict(), link=self.report_link(r)) for r in allowed_reports ])
def populate_report_table(): # Will read from report list, if items are missing we will replace with items below after analysis # Deletes all table rows to assure no dublicates Report.query.delete() db.session.commit() scope1_total = 0 scope1_net = 0 scope1_offsets = 0 scope3_total = 0 scope3_net = 0 scope3_offsets = 0 scope2_market_total = 0 scope2_market_net = 0 scope2_market_offsets = 0 scope2_location_total = 0 scope2_location_net = 0 scope2_location_offsets = 0 for report in reports: name_of_company = report['name'] year = report['year'] co_id = Company.query.filter_by(name=name_of_company).first().co_id verification_body = report['verification_body'] level_of_assur = report['level_of_assur'] gwp_stand = report['gwp_stand'] reporting_protocol = report['reporting_protocol'] for scope in report['scopes']: if scope['scope'] == 'Scope 1': scope1_total = scope['scope_total'] scope1_net = scope['scope_net'] scope1_offsets = scope['scope_offsets'] elif scope['scope'] == 'Optional': scope3_total = scope['scope_total'] scope3_net = scope['scope_net'] scope3_offsets = scope['scope_offsets'] else: if "Market" in scope['name']: scope2_market_total = scope['scope_total'] scope2_market_net = scope['scope_net'] scope2_market_offsets = scope['scope_offsets'] elif "Location" in scope['name']: scope2_location_total = scope['scope_total'] scope2_location_net = scope['scope_net'] scope2_location_offsets = scope['scope_offsets'] rep = Report(year=year, co_id=co_id, verification_body=verification_body, level_assurance=level_of_assur, gwp_standard=gwp_stand, rep_protocol=reporting_protocol, scope1_total=scope1_total, scope1_net=scope1_net, scope1_offsets=scope1_offsets, scope2_market_total=scope2_market_total, scope2_market_net=scope2_market_net, scope2_market_offsets=scope2_market_offsets, scope2_location_total=scope2_location_total, scope2_location_net=scope2_location_net, scope2_location_offsets=scope2_location_offsets, scope3_total=scope3_total, scope3_net=scope3_net, scope3_offsets=scope3_offsets) db.session.add(rep) db.session.commit()
def get_all_report(conn): """ This function returns a list of reports with one execution instance (the last one if any) Args: conn (Connection) : DB connection to use Return: [Report] : A list of reports (not all dependencies are instantiated) """ conn.row_factory = sqlite3.Row cursor = conn.cursor() reports = [] sql_get_all_report = 'select name, max(execution_date) as last_execution from Report left join Execution ' \ 'on Execution.report_id = Report.id ;' query_result = cursor.execute(sql_get_all_report).fetchall() for row in query_result: report = Report(str(row[0])) if row[1]: report.execution.append(Execution(parser.parse(row[1]))) reports.append(report) return reports
def test_get_pdf_token_allowed(self): """You don't need to be logged in at all, if your token is right.""" (other, teammate, contact, captain, super_admin, team, classroom, report_dict) = self.test_post_team_pdf() # Put in a classroom report for the same team so we know we can # correctly select the team-level one. classReport = Report.create( team_id=team.uid, classroom_id=classroom.uid, filename='classroom.pdf', gcs_path='/mybucket/upload/classroom.pdf', size=1000000, content_type='application/pdf', ) classReport.put() path = '/api/teams/{team_id}/reports/{filename}'.format( team_id=team.uid, filename=report_dict['filename'], ) endpoint_str = util.get_endpoint_str(method='GET', path=path) jwt = jwt_helper.encode({'allowed_endpoints': [endpoint_str]}), url = util.set_query_parameters(path, token=jwt) response = self.testapp.get(url) # asserts 200 self.assert_pdf_response(report_dict, response)
def test_post_team_pdf(self): today_str = datetime.date.today().strftime(config.iso_date_format) filename = 'Team_filewhack.{}.pdf'.format(today_str) (other, teammate, contact, captain, super_admin, team, classroom, gcs_path, file_size) = self.create_for_post(filename) response = self.testapp.post( '/api/reports', { 'team_id': team.uid, 'filename': filename }, upload_files=[('file', filename)], headers=self.login_headers(super_admin), ) report_dict = json.loads(response.body) fetched = Report.get_by_id(report_dict['uid']) self.assertEqual(fetched.team_id, team.uid) self.assertEqual(fetched.classroom_id, None) self.assertEqual(fetched.filename, filename) self.assertEqual(str(fetched.gcs_path), gcs_path) self.assertEqual(fetched.size, file_size) self.assertEqual(fetched.content_type, 'application/pdf') os.unlink(filename) return (other, teammate, contact, captain, super_admin, team, classroom, report_dict)
def test_post_team_dataset(self): today_str = datetime.date.today().strftime(config.iso_date_format) filename = 'Team_filewhack.{}.html'.format(today_str) dataset_id = 'Dataset_foo' template = 'template.html' (other, teammate, contact, captain, super_admin, team, classroom) = self.create() response = self.testapp.post_json( '/api/reports', { 'filename': filename, 'team_id': team.uid, 'dataset_id': dataset_id, 'template': template, }, headers={'Authorization': 'Bearer ' + self.valid_jwt}, ) fetched = Report.get_by_id(json.loads(response.body)['uid']) self.assertEqual(fetched.team_id, team.uid) self.assertIsNone(fetched.classroom_id) self.assertEqual(fetched.filename, filename) self.assertEqual(fetched.dataset_id, dataset_id) self.assertEqual(fetched.template, template)
def test_list_network_reports(self): network = Network.create(name="Foo Net", program_id="Program_foo") network.put() admin = User.create(email="*****@*****.**", owned_networks=[network.uid]) admin.put() template = 'network_tempate' filename = 'foo.html' report = Report.create( network_id=network.uid, filename=filename, dataset_id='Dataset_class1', template=template, content_type='text/html', preview=False, ) report.put() response = self.testapp.get( '/api/networks/{}/reports'.format(network.uid), headers=self.login_headers(admin), ) reports = json.loads(response.body) self.assertEqual( urlparse(reports[0]['link']).path, '/datasets/{}/{}/{}'.format(report.uid, template, filename), ) url, token = reports[0]['link'].split('?token=') payload, error = jwt_helper.decode(token) self.assertIn('GET //neptune' + urlparse(url).path, payload['allowed_endpoints'])
def set_up(self): # Let ConsistencyTestCase set up the datastore testing stub. super(TestApiClassrooms, self).set_up() application = webapp2.WSGIApplication(api_routes, config={ 'webapp2_extras.sessions': { 'secret_key': self.cookie_key } }, debug=True) self.testapp = webtest.TestApp(application) with mysql_connection.connect() as sql: sql.reset({ 'classroom': Classroom.get_table_definition(), 'participant': Participant.get_table_definition(), 'program': Program.get_table_definition(), 'report': Report.get_table_definition(), 'team': Team.get_table_definition(), 'user': User.get_table_definition(), }) self.program = Program.create( name="Engagement Project", label='ep18', min_cycles=3, active=True, preview_url='foo.com', ) self.program.put()
def release_and_notify(self, week): # Update all reports for a given week and set preview = False, _and_ # create notifications for all related users. # Capture which reports are previews before we release them. We'll base # the notifications on this list to make sure we're not notifying about # any reports that aren't previews. reports_to_release = Report.get_previews(week) # This is the "release" part. num_reports_released = Report.release_previews(week) # Now we have to load a bunch of data from the related teams, # classrooms, and users in order to create notifications. if len(reports_to_release) != num_reports_released: logging.error( "Report.get_previews() ({}) and Report.release_previews() " "({}) didn't hit the same number of rows.".format( len(reports_to_release), num_reports_released)) team_ids = {r.team_id for r in reports_to_release} classroom_ids = { r.classroom_id for r in reports_to_release if r.classroom_id } # Load all related teams and classrooms as a batch. teams = Team.get_by_id(team_ids) classrooms = Classroom.get_by_id(classroom_ids) t_index = {t.uid: t for t in teams} c_index = {c.uid: c for c in classrooms} p_index = {p.uid: p for p in Program.get()} notes = [] for r in reports_to_release: team = t_index.get(r.team_id) program = p_index.get(team.program_id) classroom = c_index.get(r.classroom_id, None) result = self.notify_for_single_report(program, team, classroom) # result might be a list or None if result: notes += result Notification.put_multi(notes) return num_reports_released
def create_for_delete(self): """Should delete the classroom and associated reports.""" contact = User.create(name='contact', email='*****@*****.**') captain = User.create(name='foo', email='*****@*****.**') team = Team.create(name='Team Foo', captain_id=captain.uid, program_id=self.program.uid) team.put() captain.owned_teams = [team.uid] contact.owned_teams = [team.uid] User.put_multi([captain, contact]) classroom = Classroom.create( name='Classroom Foo', code='trout viper', team_id=team.uid, contact_id=contact.uid, num_students=22, grade_level='9-12', ) classroom.put() report1 = Report.create( team_id=team.uid, classroom_id=classroom.uid, filename='report1.pdf', gcs_path='/upload/abc', size=10, content_type='application/pdf', ) report1.put() report2 = Report.create( team_id=team.uid, classroom_id=classroom.uid, filename='report2.pdf', gcs_path='/upload/def', size=10, content_type='application/pdf', ) report2.put() return (captain, contact, classroom, report1, report2)
def test_post_empty_report(self): """RServe posts "empty" reports to note why it hasn't produced a visible report. Test that the API accepts these for each kind of parent. """ rserve_user = User.create( id='rserve', email='*****@*****.**', user_type='super_admin', ) rserve_user.put() org = Organization.create(name='Organization', captain_id='User_cap', program_id=self.program.uid) org.put() team = Team.create( name='Team Foo', captain_id='User_cap', organization_ids=[org.uid], program_id=self.program.uid, ) team.put() classroom = Classroom.create(name='Class foo', team_id=team.uid, code='trout viper', contact_id='User_contact') classroom.put() url = '/api/reports' report_date = datetime.date.today().strftime('%Y-%m-%d') self.testapp.post_json( url, dict(self.empty_report_params(report_date, org.uid), organization_id=org.uid), headers=self.login_headers(rserve_user), ) self.testapp.post_json( url, dict(self.empty_report_params(report_date, team.uid), team_id=team.uid), headers=self.login_headers(rserve_user), ) self.testapp.post_json( url, dict(self.empty_report_params(report_date, classroom.uid), team_id=team.uid, classroom_id=classroom.uid), headers=self.login_headers(rserve_user), ) reports = Report.get() self.assertEqual(len(reports), 3) self.assertEqual(all(r.template == 'empty' for r in reports), True)
def test_full_delete_by_captain(self): captain, contact, classroom, report1, report2 = \ self.create_for_delete() url = '/api/classrooms/{}'.format(classroom.uid) headers = self.login_headers(captain) # Delete the classroom. self.testapp.delete(url, headers=headers, status=204) # Expect the classroom and related reports are gone from the db. self.assertIsNone(Classroom.get_by_id(classroom.uid)) self.assertIsNone(Report.get_by_id(report1.uid)) self.assertIsNone(Report.get_by_id(report2.uid)) # Api should show a 404. self.testapp.get(url, headers=headers, status=404) self.testapp.delete(url, headers=headers, status=404)
class Base: def __init__(self, database): self.model = Report(database) def on_get(self, req, resp): if authorize_as(req.auth, 'player'): resp.body = dumps(self.model.all()) else: raise HTTPUnauthorized('unauthorized', 'unauthorized') def on_post(self, req, resp): if authorize_as(req.auth, 'player'): body = loads(req.stream.read().decode('utf-8')) created = self.model.create(body) resp.status = HTTP_201 resp.body = dumps({'id': created.inserted_id}) else: raise HTTPUnauthorized('unauthorized', 'unauthorized')
def build_weekly_report_html(self, sender): with open(self.report_template_path, 'r') as f: html = unicode(f.read(), "utf-8") user = User.query_user(sender) report = Report.query_weekly_report(sender) if user and report: week_duration = Report.week_date_duration() html = html.replace(self.reporter_name_replacement, user.realname) html = html.replace(self.weekly_title_replacement, report.project_title) html = html.replace(self.weekly_description_replacement, report.description) html = html.replace(self.weekly_date_replacement, week_duration) contents = report.origin_report.splitlines() self.subject = u'工作周报-%s-技术-尚武研-员工平台-iOS-%s' \ % (user.realname, week_duration) self.sender_from = user.email self.sender_password = user.password finish_contents = u'' for content in contents: if content.startswith('-'): header = self.content_header_p header = header.replace(self.content_header_replacement, \ u'%s' % content[1:]) finish_contents += header else: body = self.content_body_p body = body.replace(self.content_body_replacement, content) finish_contents += body html = html.replace(self.weekly_finish_replacement, finish_contents) todo_texts = report.next_week_todo.split(u',') next_week_todo = u'' for todo in todo_texts: new_todo = self.content_body_p new_todo = new_todo.replace(self.content_body_replacement, todo) next_week_todo += new_todo html = html.replace(self.weekly_todo_replacement, next_week_todo) return self.send(html) else: return u'用户 %s 不存在或者本周周报还未生成' % sender
def test_list_preview(self): """Preview reports only visible to super admins.""" (other, teammate, contact, captain, super_admin, team, classroom) = self.create() classReport1 = Report.create( team_id=team.uid, classroom_id=classroom.uid, filename='foo.pdf', gcs_path='/mybucket/upload/12345.pdf', size=1000000, content_type='application/pdf', preview=True, ) teamReport = Report.create( team_id=team.uid, classroom_id=None, filename='team.pdf', gcs_path='/mybucket/upload/34567.pdf', size=1000000, content_type='application/pdf', preview=True, ) Report.put_multi((classReport1, teamReport)) # Contact and captain see an empty list. for user in (contact, captain): response = self.testapp.get( '/api/teams/{}/reports'.format(team.uid), headers=self.login_headers(user), ) self.assertEqual(response.body, '[]') # Super sees preview reports. response = self.testapp.get( '/api/teams/{}/reports'.format(team.uid), headers=self.login_headers(super_admin), ) self.assertEqual( set(d['uid'] for d in json.loads(response.body)), {teamReport.uid, classReport1.uid}, )
def create_widgets(self): vbox = Gtk.VBox() self.add(vbox) last_frame = Gtk.Frame() label_frame = Gtk.Label("<b>Прошлый отчет</b>") label_frame.set_use_markup(True) last_frame.set_label_widget(label_frame) last_vbox = Gtk.VBox() last_frame.add(last_vbox) vbox.pack_start(last_frame, False, False, 0) label_last_report_time = Gtk.Label("<b>Cоздан:</b> {}".format( Report.get_last_report_time(self.date_format))) label_last_report_time.set_alignment(0, .5) label_last_report_time.set_use_markup(True) last_vbox.pack_start(label_last_report_time, False, False, 0) label_last_report = Gtk.Label("<b>Содержание:</b>") label_last_report.set_alignment(0, .5) label_last_report.set_use_markup(True) last_vbox.pack_start(label_last_report, False, False, 0) last_report = Gtk.Label(Report.get_last_report()) last_report.set_alignment(0, .5) last_vbox.pack_start(last_report, False, False, 0) label_created = Gtk.Label("Текущий отчет: {}".format( self.datetime_create.strftime(self.date_format))) label_created.set_alignment(0, .5) vbox.pack_start(label_created, False, False, self.padding) self.report = Gtk.TextView() self.report.set_wrap_mode(Gtk.WrapMode.WORD) # self.report.connect('key-release-event', self.report_event) vbox.pack_start(self.report, False, False, 0) self.save_btn = Gtk.Button("Сохранить") self.save_btn.connect('clicked', self.save_report) vbox.pack_start(self.save_btn, False, False, padding=self.padding)
class Single: def __init__(self, database): self.model = Report(database) def on_get(self, req, resp, report_id): if authorize_as(req.auth, 'developer'): resource = self.model.find(report_id) if resource != None: resp.body = dumps(resource) else: resp.status = HTTP_404 else: raise HTTPUnauhtorized('unauthorized', 'unauthorized') def on_put(self, req, resp, report_id): if authorize_as(req.auth, 'developer'): body = loads(req.stream.read().decode('utf-8')) resource = self.model.update(body, report_id) if resource.modified_count == 1: resp.status = HTTP_204 else: raise HTTPUnauthorized('unauthorized', 'unauthorized')
def create_report(result): """Create and return a new user.""" url = result['url'] is_vulnerable = result['is_vulnerable'] if is_vulnerable == False: report = Report(url=url, is_vulnerable=is_vulnerable) else: url = result['url'] result['is_vulnerable'] exploit = result['exploit'] field_name = result['field_name'] form_type = result['form_type'] method = result['method'] report = Report(url=url, is_vulnerable=is_vulnerable, exploit=exploit, field_name=field_name, form_type=form_type, method=method) db.session.add(report) db.session.commit() return Report.query.filter_by(url=url).first()
def get(self, parent_type, rel_id, filename): # Always require a jwt that specifies this path/report. This allows # shareable, expiring links. if not self.get_current_user().super_admin: params = self.get_params({'token': str}) token = params.get('token', None) payload, error = jwt_helper.decode(token) if error == jwt_helper.EXPIRED: return self.http_forbidden("This link has expired.") elif not payload or error: return self.http_bad_request("Missing or invalid token.") allowed_endpoints = payload.get('allowed_endpoints', []) if self.get_endpoint_str() not in allowed_endpoints: return self.http_forbidden("Token does not allow endpoint.") if parent_type == 'organizations': parent_id = Organization.get_long_uid(rel_id), elif parent_type == 'teams': parent_id = Team.get_long_uid(rel_id), elif parent_type == 'classrooms': parent_id = Classroom.get_long_uid(rel_id), else: raise Exception("Unrecognized parent type: {}".format(parent_type)) results = Report.get(parent_id=parent_id, filename=filename) if len(results) == 0: return self.http_not_found() report = results[0] # We intentionally don't check permissions or ownership any further # here, because reports are shared by link, and we already checked for # a jwt in the query string. self.response.headers.update({ 'Content-Disposition': 'inline; filename={}'.format(report.filename), # Unicode not allowed in headers. 'Content-Type': str(report.content_type), }) try: with gcs.open(report.gcs_path, 'r') as gcs_file: self.response.write(gcs_file.read()) except gcs.NotFoundError: logging.error("Couldn't find a gcs file for report {}".format( report.to_dict())) self.http_not_found()
def test_delete(self): (other, teammate, contact, captain, super_admin, team, classroom, classReport1, classReport2, teamReport) = self.create_reports() url = '/api/reports/{}'.format(classReport1.uid) headers = self.login_headers(super_admin) # Delete the report. self.testapp.delete(url, headers=headers, status=204) # Expect the report is gone from the db. self.assertIsNone(Report.get_by_id(classReport1.uid)) # Api should show a 404. self.testapp.get(url, headers=headers, status=404) self.testapp.delete(url, headers=headers, status=404)
def set_up(self): # Let ConsistencyTestCase set up the datastore testing stub. super(TestApiReports, self).set_up() # Necessary to determine gcs upload bucket name. self.testbed.init_app_identity_stub() self.testbed.init_urlfetch_stub() self.testbed.init_blobstore_stub() application = self.patch_webapp(webapp2.WSGIApplication)( api_routes, config={ 'webapp2_extras.sessions': { 'secret_key': self.cookie_key } }, debug=True) self.testapp = webtest.TestApp(application) self.taskqueue_stub = self.testbed.get_stub( testbed.TASKQUEUE_SERVICE_NAME) with mysql_connection.connect() as sql: sql.reset({ 'classroom': Classroom.get_table_definition(), 'organization': Organization.get_table_definition(), 'program': Program.get_table_definition(), 'report': Report.get_table_definition(), 'team': Team.get_table_definition(), 'user': User.get_table_definition(), }) if self.valid_jwt is None: self.valid_jwt = jwt_helper.encode_rsa({ 'user_id': 'User_rserve', 'email': '*****@*****.**', 'user_type': 'super_admin', }) self.program = Program.create( name="Engagement Project", label='ep18', min_cycles=3, active=True, preview_url='foo.com', ) self.program.put()
def __call__(self, router_parse, sender, allow_group): """ 发送周报邮件的命令 Arguments: router_parse {urlparse} -- url parse 解析出来的 router sender {string} -- 由谁发出的发送邮件指令 """ if allow_group == '1' and not User.check_user_group_id( sender, allow_group): return u'恭喜您没有权限。哈哈哈哈。' path = router_parse.path query = router_parse.query query_dict = parse_query_2_dict(query) msg = None if path == '/create': report = WeekReporter(name=sender, \ next_week=query_dict.get('todo', u'继续完成相应需求'), \ title=query_dict.get('title'), \ desc=query_dict.get('desc')) msg = report.create_report() else: w_pr = Report.query_weekly_report(sender) if not w_pr: return u'%s:本周周报还未创建' % sender elif path == '/review': report_fix = w_pr.fix_report if w_pr.fix_report \ else w_pr.origin_report msg = u'周报标题:%s、描述:%s。\n%s\n下周任务:%s' \ % (w_pr.project_title, w_pr.description, \ report_fix, w_pr.next_week_todo) elif path == '/check': w_pr.report_checked() msg = u'%s:可以发送周报啦' % sender elif path == '/send': if w_pr.checked: w_mail = WeeklyMail() msg = w_mail.build_weekly_report_html(sender) else: msg = u'%s:请确认周报无误后再发送' % sender elif path == '/update': msg = w_pr.update_report(done=query_dict.get('done'), \ todo=query_dict.get('todo'), title=query_dict.get('title'), \ desc=query_dict.get('desc')) return msg if msg else WeeklyCommand.helper_info()
def get(self,title): startDate = get_startDay() reports = Report().all() reports.filter('title', title) reports.order('-date') reports.filter('date >' ,startDate) feed = FeedFMSParser() final_report = feed.list_items() final_report += [r.to_dict() for r in reports] json = simplejson.dumps(final_report) self.response.headers.add_header('content-type', 'application/json', charset='utf-8') self.response.out.write(json)
def set_up(self): # Let ConsistencyTestCase set up the datastore testing stub. super(TestApiTeams, self).set_up() application = webapp2.WSGIApplication( api_routes, config={ 'webapp2_extras.sessions': { 'secret_key': self.cookie_key } }, debug=True ) self.testapp = webtest.TestApp(application) with mysql_connection.connect() as sql: sql.reset({ 'classroom': Classroom.get_table_definition(), 'cycle': Cycle.get_table_definition(), 'program': Program.get_table_definition(), 'organization': Organization.get_table_definition(), 'report': Report.get_table_definition(), 'survey': Survey.get_table_definition(), 'team': Team.get_table_definition(), 'user': User.get_table_definition(), }) # See #848. Remove once resolved. self.demo_program = Program.create( name="Demo Program", label='demoprogram', min_cycles=3, active=True, preview_url='foo.com', ) self.demo_program.put() self.ep_program = Program.create( name="Engagement Project", label='ep19', min_cycles=3, active=True, preview_url='foo.com', ) self.ep_program.put()
def index(): _from = request.args.get('from') or "today" _to = request.args.get('to') or datetime.now() default_filter = filters.get(_from) if default_filter: _from = default_filter['from'] _to = default_filter['to'] _from, _to = _check(_from), _check(_to) if _from > _to: return abort(400, description='Incorrect range date: from {} to {}'.format(_from, _to)) reports = Report.select().where((Report.created > _from) & (Report.created < _to)) return render_template( "index.html", reports=reports, filters=((title['title'], value) for value, title in filters.items()), today=date.today().strftime(_format) )
def accept_report(sub_id): """ Accept new report --- parameters: - name: sub_id description: The report's author-id in: path type: string - name: report description: A report to be accepted in: body required: true schema: $ref: '#/definitions/Report' responses: 200: description: The report is accepted schema: properties: _status: description: Response status $ref: '#/definitions/ResponseStatus' accepted: description: The accepted report $ref: '#/definitions/Report' 404: description: The subordinate is not found schema: properties: _status: description: Response status $ref: '#/definitions/ResponseStatus' """ report = Report.make(request.json) commander.accept_report(sub_id, report) return jsonify(_status=ResponseStatus.Success, accepted=report.to_dict())
def post(self): request = self.request report = Report() #try: name = urllib.unquote(self.request.get('name')) water = urllib.unquote(self.request.get('water')) road = urllib.unquote(self.request.get('road')) text = urllib.unquote(self.request.get('text')) lat = urllib.unquote(self.request.get('lat')) lng = urllib.unquote(self.request.get('lng')) report.title = title report.name = name report.lat = float(lat) report.lng = float(lng) report.water = int(water) report.text = text report.road = bool(road) report.put() # except Exception, e: #self.redirect('/ThaiFlood2011/?error=Error, %s'%e) error = urllib.unquote(self.request.get('error')) startDate = get_startDay() reports = Report().all().filter('title', title) reports.order('-date') reports.filter('date >' ,startDate) template_values = { 'title' : title, 'e_msg':error, 'reports': simplejson.dumps([r.to_dict() for r in reports]), 'startDate':startDate } path = os.path.join(os.path.dirname(__file__), 'index.html') self.response.out.write(template.render(path, template_values))
def __init__(self, database): self.model = Report(database)