def post(self, topic_id): session = Session() roles = oidc.user_getfield('cognito:groups') if oidc.user_getfield( 'cognito:groups') else [] username = oidc.user_getfield('username') kargs = request.get_json(silent=True) if not kargs.get('desc'): json_abort(400, "desc missing") topic = session.query(Topic).get(topic_id) if not topic: json_abort(404) now = datetime.datetime.now() if topic.start_time <= now: json_abort(403, "Voting already started. No changes allowed") grant = session.query(RoleGrant).filter( RoleGrant.topic_id == topic_id).filter( RoleGrant.role.in_(roles)).all() logger.debug("{}, {}, {}".format(topic.user, username, topic.user != username)) if topic.user != username and 'admin' not in roles and not grant: json_abort(403) option = TopicOption(**kargs) option.topic_id = topic_id session.add(option) session.commit() logger.debug(option.to_dict()) session = Session() if not oidc.is_api_request(): url = url_for('api_topic', id=topic_id) data = {"url": url, "message": "Success. Redirecting to %s" % url} return jsonify(data) return jsonify(option)
def put(self, topic_id, id): session = Session() roles = oidc.user_getfield('cognito:groups') if oidc.user_getfield( 'cognito:groups') else [] username = oidc.user_getfield('username') kargs = request.get_json(silent=True) if not kargs.get('role'): json_abort(400, "role missing") topic = session.query(Topic).get(topic_id) if not topic: json_abort(404, "Topic doesn't exist") now = datetime.datetime.now() if topic.start_time <= now: json_abort(403, "Voting already started. No changes allowed") grant = session.query(RoleGrant).filter( RoleGrant.topic_id == topic_id).filter( RoleGrant.role.in_(roles)).all() if 'admin' not in roles and not grant: json_abort(403) invite = session.query(Invite).get(id) if kargs.get('role'): invite.role = kargs.get('role') session.add(invite) session.commit() logger.debug(invite.to_dict()) session = Session() if not oidc.is_api_request(): url = url_for('api_topic', id=topic_id) data = {"url": url, "message": "Success. Redirecting to %s" % url} return jsonify(data) return jsonify(invite)
def get(self, ws_id): session = Session() ws = session.query(Workspace).get(int(ws_id)) labeled = get_labeled(session, ws) pages = list(session.query(Page).filter_by(workspace=ws.id)) ws_data = { 'id': ws.id, 'name': ws.name, 'pages': [ {'id': page.id, 'url': page.url, 'labeled': { selector: label['text'] for selector, label in labeled.get(page.url, {}).items()}, } for page in pages], } with tempfile.NamedTemporaryFile('wb', delete=False) as tempf: with zipfile.ZipFile(tempf, mode='w') as archive: archive.writestr('meta.json', json.dumps(ws_data, indent=True)) for page in pages: save_page_for_offline(archive, session, page) try: with open(tempf.name, 'rb') as f: contents = f.read() finally: os.unlink(tempf.name) self.set_header('Content-Disposition', 'attachment; filename="workspace_{}.zip"'.format(ws.id)) self.set_header('Content-Type', 'application/zip') self.write(contents)
def get(self, ws_id): session = Session() ws = session.query(Workspace).get(int(ws_id)) url = self.get_argument('url') referer = self.get_argument('referer', None) page = session.query(Page).filter_by(workspace=ws.id, url=referer or url).one() headers = self.request.headers.copy() for field in ['cookie', 'referer', 'host']: try: del headers[field] except KeyError: pass if referer: headers['referer'] = referer session = Session() response = get_response(session, page, url) if response is None: httpclient = AsyncHTTPClient() response = yield httpclient.fetch(url, raise_error=False) response = save_response(session, page, url, response, is_main=referer is None) reason = None if response.code in status_codes else 'Unknown' self.set_status(response.code, reason=reason) proxy_url_base = self.reverse_url('proxy', ws.id) def proxy_url(resource_url): return '{}?{}'.format( proxy_url_base, urlencode({ 'url': resource_url, 'referer': page.url, })) html_transformed, body = transformed_response_body( response, inject_scripts_and_proxy, proxy_url) self.write(body) proxied_headers = {'content-type'} # TODO - other? if response.headers: for k, v in response.headers.get_all(): if k.lower() in proxied_headers: if html_transformed and k.lower() == 'content-type': # change encoding (always utf8 now) v = 'text/html; charset=UTF-8' self.set_header(k, v) self.finish()
def get(self, ws_id): session = Session() ws = session.query(Workspace).get(int(ws_id)) labeled = get_labeled(session, ws) ws_data = { 'id': ws.id, 'name': ws.name, 'labels': [label.text for label in session.query(Label).filter_by(workspace=ws.id)], 'urls': [page.url for page in session.query(Page).filter_by(workspace=ws.id)], 'labeled': labeled, } self.write(ws_data)
def __init__(self): print 'Initializing Driver' session = Session() self.modules = session.query(LightModule).all() self.strips = {s.name: s for s in session.query(Strip).all()} session.close() print 'starting engine' self.engine = LightEngine(self.modules) print 'engine running' self.sp = Spotify() print 'spotify loaded' if SIM: self.sim = Client('127.0.0.1:7890')
def put(self, id): session = Session() username = oidc.user_getfield('username') grant = session.query(RoleGrant).get(id) kargs = request.get_json(silent=True) if kargs.get('id'): topic = session.query(Topic).get(kargs['topic_id']) if not topic.user == username: json_abort(403) grant.topic = topic if kargs.get('role'): grant.role = kargs.get('role') session.commit() logger.debug(grant) return jsonify(grant)
def put(self, id): session = Session() username = oidc.user_getfield('username') topic = session.query(Topic).get(id) if topic: now = datetime.datetime.now() if topic.start_time <= now and now <= topic.end_time: json_abort(403) if topic.user != username: json_abort(403) kargs = request.get_json(silent=True) logger.debug(kargs) fields = Topic.get_form_fields() for field in fields: if kargs.get(field): setattr(topic, field, kargs[field]) now = datetime.datetime.now() minimum = datetime.timedelta(minutes=5) if kargs.get('start_time') and (parse(kargs.get('start_time'), yearfirst=True) - now) < minimum: json_abort(400, "You can't edit a topic 5 minutes before start.") if topic.start_time >= topic.end_time: json_abort(400, "End time can not be less than Start time.") session.commit() if not oidc.is_api_request(): url = url_for('api_topic', id=int(id)) data = { "url": url, "message": "Success. Redirecting to %s" % url } return jsonify(data) return jsonify(topic)
def user(session, slug): current_user = None current_user_id = session.get("user_id") sa_session = Session() if (current_user_id): current_user = sa_session.query(User).filter( User.id == current_user_id).first() user_being_viewed = sa_session.query(User).filter( func.lower(User.slug) == func.lower(slug)).first() if (not user_being_viewed): bottle.abort(404, "URL Not Found") return br.render_html(br.BaseLayout( {"current_user": current_user and current_user.to_dict()}, [br.UserPage(props)]), title="Grappling Mind Mapper")
def delete(self, id): session = Session() roles = oidc.user_getfield('cognito:groups') if oidc.user_getfield('cognito:groups') else [] topic = session.query(Topic).get(id) if topic: now = datetime.datetime.now() if now > topic.start_time: json_abort(403, "Voting started. Can't delete") if 'admin' not in roles: json_abort(403) topic = session.query(Topic).get(id) session.delete(topic) session.commit() logger.debug(topic.to_dict()) if not oidc.is_api_request(): url = url_for('api_topic', id=int(id)) data = { "url": url, "message": "Success. Redirecting to %s" % url } return jsonify(topic)
def save(self, data): if not SAVE_TO_DB: return session = Session() if not session.query(Item).filter_by(title=data['title']).first(): obj = Item(**data) session.add(obj) session.commit()
async def submit_task(course_id: int, activity_id: str, token: str = Form(...), content: str = Form(...), type: Optional[str] = Form( ActivityType.multiple_choice)): s = Session() sender = validate_user(s, token, False) try: course = s.query(Course).filter(Course.id == course_id).one() except NoResultFound: raise HTTPException(status_code=404, detail='Invalid course id') try: activity = s.query(Activity).filter( Activity.course_id == course_id, Activity.course_specific_id == activity_id).one() except NoResultFound: activity = Activity(course_specific_id=activity_id, type=type, course=course) s.add(activity) sub = Submission(sender=sender, activity=activity, content=content) s.add(sub) try: correct = s.query(Submission.content).filter( Submission.activity == activity, Submission.sender.has(is_prof=True)).first()[0] except NoResultFound: correct = "" s.commit() return { 'message': 'OK', 'id': sub.id, "correct_answer": correct, "activity_id": activity_id, "content": content }
def get(self): session = Session() records = session.query(Countries).all() countries = [record.get_as_dict() for record in records] response = dict(data=countries) return response, 200
def post(self): session = Session() username = oidc.user_getfield('username') roles = oidc.user_getfield('cognito:groups') if oidc.user_getfield( 'cognito:groups') else [] kargs = request.get_json(silent=True) logger.debug(kargs) vote_jwt = kargs.get('vote') if not vote_jwt: json_abort(400, "Vote missing") if not oidc.is_api_request(): json_abort(403) secret = oidc.get_access_token().split('.')[-1] payload = jwt.decode(vote_jwt, secret, algorithms=['HS256']) fields = ['token', 'topic_id', 'option_id'] for field in fields: if not payload.get(field): json_abort(400, "%s missing in token" % field) topic_id = payload.get('topic_id') topic = session.query(Topic).get(topic_id) if not topic: json_abort(404, description="Topic not found") now = datetime.datetime.now() if topic.start_time > now and topic.end_time < now: json_abort(400, description="Voting not begun yet") mapper = session.query(Mapper).filter( Mapper.topic_id == topic_id).filter(Mapper.user == username).all() if mapper: json_abort(409) invite = session.query(Invite).filter( Invite.topic_id == topic_id).filter(Invite.role.in_(roles)).all() if not invite and topic.user != username: json_abort(403) vote = Vote(topic_id=payload['topic_id'], option_id=payload['option_id'], token=payload['token']) mapper = Mapper(user=username, topic_id=topic_id) session.add(vote) session.add(mapper) session.commit() logger.debug(vote) return jsonify(vote)
def get_labeled(session: Session, ws: Workspace) -> Dict: labeled = {} for element_label, page_url, label_text in ( session.query(ElementLabel, Page.url, Label.text).join(Page) .filter(Page.workspace == ws.id) .all()): labeled.setdefault(page_url, {})[element_label.selector] = { 'selector': element_label.selector, 'text': label_text, } return labeled
def delete(self, id): session = Session() username = oidc.user_getfield('username') grant = session.query(RoleGrant).get(id) if not grant: json_abort(404) if not grant.topic.user == username: json_abort(403) session.delete(grant) session.commit() logger.debug(grant) return jsonify(grant)
def delete(self, topic_id, id): session = Session() roles = oidc.user_getfield('cognito:groups') if oidc.user_getfield( 'cognito:groups') else [] username = oidc.user_getfield('username') kargs = request.get_json(silent=True) topic = session.query(Topic).get(topic_id) if not topic: json_abort(404, "Topic doesn't exist") now = datetime.datetime.now() if topic.start_time <= now and now <= topic.end_time: json_abort(403, "Voting already started. No changes allowed") grant = session.query(RoleGrant).filter( RoleGrant.topic_id == topic_id).filter( RoleGrant.role.in_(roles)).all() if 'admin' not in roles and not grant and topic.user != username: json_abort(403) option = session.query(TopicOption).get(id) session.delete(option) session.commit() logger.debug(option) return jsonify(option)
def get(self, id=None): session = Session() roles = oidc.user_getfield('cognito:groups') if oidc.user_getfield( 'cognito:groups') else [] username = oidc.user_getfield('username') if id is not None: grant = session.query(RoleGrant).get(id) if grant: if 'admin' not in roles and grant.topic.user != username: abort(403) logger.debug(grant) return jsonify(grant) else: if 'admins' in roles: grants = session.query(RoleGrant).all() return jsonify(grants) grants = session.query( RoleGrant, Topic).filter(RoleGrant.topic_id == Topic.id).filter( Topic.user == username).all() grants = [id for id, _ in grants] return jsonify(grants)
def post(self): session = Session() data = json.loads(self.request.body.decode('utf8')) ws = session.query(Workspace).get(data['wsId']) page = session.query(Page).filter_by( workspace=ws.id, url=data['url']).one() element_label = session.query(ElementLabel).filter_by( page=page.id, selector=data['selector']).one_or_none() if data.get('label') is not None: label = session.query(Label).filter_by( workspace=ws.id, text=data['label']).one() if element_label is None: element_label = ElementLabel( page=page.id, selector=data['selector'], label=label.id) else: element_label.label = label.id session.add(element_label) elif element_label is not None: session.delete(element_label) session.commit() self.write({'ok': True})
def post(self): session = Session() data = json.loads(self.request.body.decode('utf8')) if data.get('id'): workspace = session.query(Workspace).get(data['id']) else: workspace = Workspace() workspace.name = data.get('name', '') session.add(workspace) session.commit() workspace.update_labels(session, data['labels']) workspace.update_urls(session, data['urls']) session.commit() self.write({'id': workspace.id})
def get(self): session = Session() records = session.query(Cities).all() cities = list() for record in records: city = dict() city["id"] = record.gid city["name"] = record.name cities.append(city) response = dict(data=cities) return response, 200
async def list_submissions(course_id: int, activity_id: str, token: str = Form(...)): s = Session() prof = validate_user(s, token, True) all_submissions = s.query(Submission.content, User.lms_user) \ .join(Submission.sender) \ .filter(Submission.activity.has(course_id=course_id)) \ .filter(Submission.activity.has(course_specific_id=activity_id)) \ .all() all_submissions = [r._asdict() for r in all_submissions] return all_submissions
def Print(): Base.metadata.create_all(engine) session2 = Session() # 将 ./User_Files/To_Print/ 中的文件名导入到预打印日志中 cmd = "ls -t ./User_Files/To_Print/ > ./log/ToPrint_filename" subprocess.call(cmd, shell=True) ToPrint = open("./log/ToPrint_filename", 'r+') direction_option = "" # 打印方向参数 for line in ToPrint: printed_order = session2.query(Order).filter( Order.File_Dir == line[:-1]) # 查询当前打印订单对象 if printed_order.Print_Direction == 2: direction_option = "-o landscape" try: # 开始尝试打印 print_cmd = 'lp -d {} -n {} -o fitplot {} ./User_Files/To_Print/{}'.format( Printer_Name, printed_order.Print_Copies, direction_option, line[:-1]) returnCode = subprocess.call(print_cmd, shell=True) if returnCode != 0: error = commands.getoutput(print_cmd) raise IOError(error) except Exception as e: # 捕获错误,并将错误写入错误日志中 with open('./log/print_error_log', 'a') as f: f.write( str(datetime.datetime.now()) + " " + line[:-1] + " " + str(e) + "\n") else: # 将打印成功的文件移动到 ./User_Files/Finished_Print 这个目录中 subprocess.call( 'mv ./User_Files/To_Print/{} ./User_Files/Finished_Print/'. format(line[:-1]), shell=True) # 在数据库中修改打印状态为3,表示已经打印 printed_order.Print_Status = 3 session2.commit() # session2.close() # 在./log/print_access_log 中写入打印成功日志 with open('./log/print_access_log', 'a') as f: f.write( str(datetime.datetime.now()) + " " + line[:-1] + " " + "Successfully-Added-To-Printer")
def post(self): session = Session() username = oidc.user_getfield('username') kargs = request.get_json(silent=True) logger.debug(kargs) if not kargs.get('topic_id'): json_abort(400) topic = session.query(Topic).get(kargs['topic_id']) if not topic: json_abort(400) if not topic.user == username: json_abort(403) grant = RoleGrant(**kargs) session.add(grant) session.commit() logger.debug(grant) return jsonify(grant)
def get(self, id): """Get city by ID and return as unique resource. For example: http://localhost:5000/api/cities/1 :param id {int}: unique id of the city to return :return: """ session = Session() city = session.query(Cities).get(id) if city: response = dict(data=city.get_as_dict()) else: return "City with id={} does not exist!".format(id), HTTP_NOT_FOUND_CODE return response, HTTP_OK_CODE
def process_input(header=True): csv_file = open(tmp_file, 'r', encoding='utf-8') csv_reader = None try: csv_reader = list(csv.reader(csv_file, delimiter=',')) except: print("Error in opening csv file. Please check the format/encoding!!") quit() line_no = 0 if header: line_no += 1 csv_reader = csv_reader[1:] session = Session() all_reviews = session.query(Review).all() reviews_set = {(review.product_id, str(review.review_text).lower()) for review in all_reviews} reviews_in_file = set() print("Processing input file..") for line in csv_reader: line_no += 1 review_text = line[0] product_id = line[4] search_key = (product_id, str(review_text).lower()) if search_key in reviews_set: print("Review at line: {} is already in db!!".format(line_no)) elif search_key in reviews_in_file: print("Review at line: {} is duplicate in file!!".format(line_no)) else: review_ob = Review(product_id, review_text) session.add(review_ob) reviews_in_file.add(search_key) print("Committing data...") session.commit() session.close() try: # Remove downloaded file os.remove(tmp_file) except: pass
def Query(): # 查询排队方式为自动排队的订单 Base.metadata.create_all(engine) session = Session() All_Order = session.query(Order).filter(Order.Time_Way == 1).all() if All_Order: for i in range(len(All_Order)): if All_Order[i].Print_Status == 1: # 尝试下载订单中的文件 try: url = "http://rooins.careyou.xin/static/Upload_Files/" + All_Order[ i].File_Dir r = requests.get(url) if r.status_code != 200: # 判断url若不是200,则记录错误到日志 raise IOError('{} {} {}'.format( r.status_code, r.reason, r.url)) else: with open( './User_Files/To_Print/' + All_Order[i].File_Dir, 'wb') as f: f.write(r.content) except Exception as e: # 将错误写入下载错误日志 with open('./log/download_error_log', 'a') as f: f.write( str(datetime.datetime.now()) + " " + All_Order[i].File_Dir + " " + str(e) + "\n") else: # 将下载成功写入下载成功日志 with open("./log/download_log", "a") as f: f.write( str(datetime.datetime.now()) + " " + All_Order[i].File_Dir + " " + "success-download" + "\n") # 在数据库中做出标记,文件已下载成功 All_Order[i].Print_Status = 2 finally: session.commit()
def process_reviews(): session = Session() client = language.LanguageServiceClient() reviews_to_analyze = session.query(Review).filter_by( review_analyzed=False).all() total = len(reviews_to_analyze) processed = 0 step = 1 throttle_limit = 500 print("Processing Reviews...") print("Processed {}/{} ".format(processed, total), end='\r') start_time = time.time() one_minute = 60 for review in reviews_to_analyze: try: analyze_review(client, review, session) except: pass review.review_analyzed = True session.add(review) processed += 1 # if processed % step == 0: print("Processed {}/{} ".format(processed, total), end='\r') if processed % throttle_limit == 0: end_time = time.time() time_taken = end_time - start_time if time_taken < one_minute: time.sleep(one_minute - time_taken) start_time = time.time() print("Processed {}/{} ".format(processed, total)) print("Committing data...") session.commit() session.close() print("Processed data stored successfully!!")
session = Session() dirname = os.path.dirname(__file__) ds = DataProcessor(os.path.join(dirname, dataSource)) ds.process() for i, row in ds.artistsByYear().iterrows(): record = FactArtistByYear(year=row[0], artist=row[1], titles=row[2]) try: session.add(record) session.commit() except IntegrityError: print('FactArtistByYear Record exists for year {}'.format(row[0])) session.rollback() query = session.query(FactArtistByYear) print('{} records exist in FactArtistByYear'.format(query.count())) for i, row in ds.genreByYear().iterrows(): record = FactGenreByYear(year=row[0], genre=row[1], titles=row[2]) try: session.add(record) session.commit() except IntegrityError: print('FactGenreByYear Record exists for year {}'.format(row[0])) session.rollback() query = session.query(FactGenreByYear) print('{} records exist in FactGenreByYear'.format(query.count())) for i, row in ds.songByYear().iterrows(): record = FactSongByYear(year=row[0], song=row[1], popularity=row[2])
''' Created on Jul 3, 2018 @author: Pravesh ''' from config import Session from tables import Issue session=Session() result=session.query(Issue).filter(Issue.id=="1").first() print(result)