def register(): #パラメータ取得 agent_cd = request.form['agent_cd'] agent_name = request.form['agent_name'] #パラメータチェック if not agent_cd or not agent_name: flash('入力してください。') return redirect(url_for('login')) #UserAgent取得 header = request.headers.get('User-Agent') ua = woothee.parse(header) category = ua.get('category') name = ua.get('name') version = ua.get('version') os_name = ua.get('os') vendor = ua.get('vendor') os_version = ua.get('os_version') #DB登録処理 user_agent = UserAgent(agent_cd=agent_cd, agent_name=agent_name, category=category, name=name, version=version, os_name=os_name, vendor=vendor, os_version=os_version) try: db_session.add(user_agent) db_session.commit() except Exception as e: db_session.rollback() flash('既に登録済みです。') return redirect(url_for('login')) finally: db_session.close() return render_template('register.html')
def save_thing(thing): """just commit something into db""" db_session.add(thing) try: db_session.commit() except: db_session.rollback()
def post(self): """ 获取long_url转换short_url返回 :return: """ long_url = request.values.get('long_url') if not long_url: return make_response(jsonify({'error': 'not long_url'})) url_obj = None try: url_obj = UrlModel.get_url_by_long_url(long_url) except Exception as e: print(e) if url_obj: short_url = url_obj.short_url return make_response(jsonify({'short_url': short_url})) short_url = '/short/' short_url += ''.join(random.choice(string.ascii_uppercase + string.digits) for i in range(6)) try: one_url = UrlModel(long_url=long_url, short_url=short_url) db_session.add(one_url) db_session.commit() print('save url success') except Exception as e: print('save url error', str(e)) db_session.rollback() # add_url_route(app, short_url) return make_response(jsonify({'short_url': short_url}))
def update_category(): """ Updates a category. """ try: category_id = request.form['id'] c = CategoryModel.query.get(category_id) if c.name == "Uncategorised": return render_template( 'admin.html', categories=category_controller.list(), status=False, action='updated', operation='categories', message="You can't change the name of this category") c.name = request.form['title'] db_session.add(c) db_session.commit() status = True return render_template('admin.html', categories=category_controller.list(), status=status, action='updated', operation='categories') except IntegrityError as e: db_session.rollback() return render_template('admin.html', status=False, categories=category_controller.list(), message="This Categorie already exists ! ")
def add_slide(): current_app.logger.debug("debug aaddSlide") message = isValidURL(request.form['url']) if message is not None: return render_template('admin.html', categories=category_controller.list(), status=False, message=message) # Takes a default value in case screenshot not specified. if not request.form['screenshot']: screenshot = "img/badge-reserved.jpg" else: screenshot = request.form['screenshot'] try: slide_controller.create( title=request.form['title'], url=request.form['url'], description=request.form['description'], category=request.form['categorie'], screenshot=screenshot, ) redirect(url_for("admin")) return render_template('admin.html', categories=category_controller.list(), status=True, action='added') except IntegrityError as e: db_session.rollback() return render_template('admin.html', status=False, categories=category_controller.list(), message="This slide already exists ! ")
def add_subscriber(*args, **kwargs): """ add subscriber to datbase and job queue TODO: verify notify_time_str to be HH:MM. """ subscriber_dict = { 'email': kwargs.get('email'), 'station_id': kwargs.get('station_id'), 'endpoint': kwargs.get('endpoint'), 'dh_param': kwargs.get('p256dh'), 'auth': kwargs.get('auth') } notify_time_str = kwargs.get('notify_time') notify_time = dateutil.parser.parse(notify_time_str) notify_time = transform_notify_time(notify_time) subscriber_dict['notify_time'] = notify_time subscriber = Subscriber(**subscriber_dict) db_session.add(subscriber) # Commit to database, rollback if commit fails try: db_session.commit() except: db_session.rollback() raise InvalidSubscriberInputError() queue_registration_notification(subscriber) set_notification_job(subscriber) return subscriber
def addArticles(filename, db_name): articles = [] with open(filename, "rb") as csvfile: reader = csv.DictReader(csvfile) for row in reader: row = {k: v.decode("utf8") for k, v in row.items()} title = row['title'] db_id = row['db_id'] pub_date = row['pub_date'] publication = row['publication'] source_description = row.get('source_description') text = row['text'] try: db_session.add( ArticleMetadata(title=title, db_name=db_name, db_id=db_id, filename=db_id, pub_date=pub_date, publication=publication, source_description=source_description, text=text)) db_session.commit() except IntegrityError as detail: print(detail) db_session.rollback() continue
def item_store(category_name): """Store new item in DB""" if 'username' not in login_session: return redirect(url_for('login')) category = Category.query.filter(Category.name == category_name).first() if category.author_id != login_session['user_id']: return render_template('401.html', name='item') form = ItemForm() form.category_id.choices = [(c.id, c.name) for c in Category.query.order_by('name')] form.category_id.data = category.id if form.validate_on_submit(): try: new_item = Item(form.name.data, form.description.data, category.id, login_session['user_id']) if 'image' in request.files and request.files['image']: filename = images.save(request.files['image']) new_item.image_name = filename db_session.add(new_item) db_session.commit() return redirect(url_for('item_view', category_name=category_name, item_name=form.name.data)) except IntegrityError: db_session.rollback() flash("Item name must be unique value") except Exception, e: db_session.rollback() print e flash("Database error encountered")
def category_update(category_name): """Update category name and description in database""" if 'username' not in login_session: return redirect(url_for('login')) category = Category.query.filter(Category.name == category_name).first() if category.author_id != login_session['user_id']: return "" form = CategoryForm() if form.validate_on_submit(): try: category.name = form.name.data category.description = form.description.data db_session.add(category) db_session.commit() return redirect(url_for('category_view', category_name=form.name.data)) except IntegrityError: db_session.rollback() flash("Category name must be unique value") except Exception: db_session.rollback() flash("Database error encountered") flash_errors(form) return redirect(url_for('category_edit', category_name=category_name))
def create_company(params): db_session.add(Company(params['name'])) try: db_session.commit() except IntegrityError as e: db_session.rollback() raise DbException("duplicate name parameter", e)
def insert_orphan_file(path, movie_name): file = File(filepath=path, name=movie_name, movieid=ORPHAN_FILE) db_session.add(file) try: db_session.commit() except: db_session.rollback()
def take_order(id): _order = db_session.query(Order).filter(Order.id == id).first() # check if the order exists if _order is None: return generate_error_message("The order doesn't exist.") # check if the order is already taken elif _order.status == "TAKEN": return generate_error_message("Oops! The order has been taken.") try: # try to lock the order _order = db_session.query(Order).filter( Order.id == id, Order.status == "UNASSIGNED").with_for_update(nowait=True, of=Order).first() if _order is None: db_session.rollback() return generate_error_message("Oops! The order has been taken.") else: _order.status = "TAKEN" db_session.commit() except OperationalError: # someone has locked the order, assuming this one will be taken return generate_error_message("Oops! The order has been taken.") message = f"Order {_order.id} is successfully taken." app.logger.info(message) return Response(response=json.dumps({"status": "SUCCESS"}), status=200, mimetype='application/json')
def reg_pitch(): form = PitchForm(request.form) if request.method == 'POST': try: pitch = Pitch(form.proposal_name.data, form.proposal_outline.data, form.interests.data, form.launch_date.data, form.pitcher_id.data) print(pitch.pitcher_id) print(pitch.launch_date) db_session.add(pitch) db_session.commit() # catch duplicate entries except IntegrityError: db_session.rollback() flash("This pitch name is already registered with pitchCatch!") return render_template('reg_pitch.html', form=form) proposal_name = pitch.proposal_name flash("Your pitch is registered with pitchCatch!") return redirect(url_for('pitch_profile', proposal_name=proposal_name)) sponsor = db_session.query(Pitcher).all() return render_template('reg_pitch.html', form=form, sponsor=sponsor)
def reg_pitcher(): form = PitcherForm(request.form) if request.method == 'POST': try: pitcher = Pitcher( form.movement_name.data, form.movement_url.data, form.movement_description.data, form.interests.data, form.email.data, form.region.data, ) db_session.add(pitcher) db_session.commit() # catch duplicate entries except IntegrityError: db_session.rollback() flash("Your movement name is already registered with pitchCatch!") return render_template('reg_pitcher.html', form=form) movement_name = pitcher.movement_name flash("Welcome to pitchCatch!") return redirect(url_for('pitcher_profile', movement_name=movement_name)) return render_template('reg_pitcher.html', form=form)
def put(self, *args, **kwargs): '''更新信息''' data = json.loads(self.request.body.decode("utf-8")) name = data.get('name', None) content = data.get('content', None) email = data.get('email', None) advance_at = data.get('advance_at', None) expire_at = data.get('expire_at', None) try: event_info = { "content": content, "email": email, "advance_at": advance_at, "expire_at": expire_at, } db_session.query(EventReminder).filter( EventReminder.name == name).update(event_info) db_session.commit() resp = { 'status': 0, 'data': data, 'datetime': now_time, 'msg': '更新成功' } return self.write(resp) except Exception as e: print(e) db_session.rollback()
def reg_catcher(): form = CatcherForm(request.form) if request.method == 'POST': try: catcher = Catcher( form.developer_name.data, form.developer_url.data, form.developer_description.data, form.interests.data, form.frontend_interest.data, form.backend_interest.data, form.frontend_experience.data, form.backend_experience.data, form.prog_languages.data, form.email.data, form.region.data, ) db_session.add(catcher) db_session.commit() # catch duplicate entries except IntegrityError: db_session.rollback() flash("Your username is already registered with pitchCatch!") return render_template('reg_catcher.html', form=form) developer_name = catcher.developer_name flash("Welcome to pitchCatch!") return redirect( url_for('catcher_profile', developer_name=developer_name)) return render_template('reg_catcher.html', form=form)
def handle_internal_error(exception): """Rollback database transaction if any error occurs""" logging.error(exception) session.rollback() return jsonify({ 'message': 'An unexpected internal error has occurred' }), 500
def upload_images(): if request.method == 'POST': idads = request.form['idads'] images = request.files.getlist('images[]') for image in images: if image and allowed_file(image.filename): extension = image.filename.split('.')[-1] if extension == "jpg": mime = "image/jpeg" else: mime = "image/" + extension i = Image(idads=idads, extension=extension, mime=mime, filename=image.filename, image=image.read()) db_session.add(i) try: db_session.commit() except: db_session.rollback() raise Exception('DB commit is not OK') return "Images uploaded?"
def problems_del(problem_id): """ Deletes a problem Params: problem_id (int): the problem to delete Returns: a redirect to the problem view page """ problem = model.Problem.query.filter_by(id=int(problem_id)).scalar() if problem is None: error = "Failed to delete problem \'{}\' as it doesn't exist.".format( problem.slug) current_app.logger.info(error) flash(error, "danger") return redirect(url_for("problems.problems_view")) try: db_session.delete(problem) db_session.commit() flash("Deleted problem \'{}\'".format(problem.slug), "warning") except IntegrityError: db_session.rollback() error = "Failed to delete problem \'{}\' as it's referenced in another DB element".format( problem.slug) current_app.logger.info(error) flash(error, "danger") return redirect(url_for("problems.problems_view"))
def snapshot_issues(milestone_group): """ Fetches all of the issues for the given sprint and stores them in a database """ sprint = get_or_create_sprint(milestone_group[0][1]) print "Processing {}".format(sprint.name) if sprint.locked is True: print "Skipping '{}', it's locked".format(sprint.name) return snapshot = Snapshot(sprint) db_session.add(snapshot) have_updates = False for repo, milestone in milestone_group: url = 'https://api.github.com/repos/{}/{}/issues?state=all&milestone={}'.format(settings.ORG, repo, milestone['number']) while True: issues = _auth_get_request(url) for issue in issues.json(): is_updated = save_issue_snapshot(repo, issue, snapshot) have_updates = have_updates or is_updated next_page = _get_next_page(issues) if next_page: url = next_page else: break if have_updates: print "Have updates, committing snapshot" db_session.commit() else: print "No updates, reverting snapshot" db_session.rollback()
def main(): parser = argparse.ArgumentParser() parser.add_argument('identifiers', type=str, nargs='*', help='Disqus identifiers to create markers for') args = parser.parse_args() identifiers = args.identifiers if args.identifiers else sys.stdin for identifier in identifiers: m = re.match('\((\d+\.\d+),\s*(\d+\.\d+)\)', identifier) if not m: print("Failed processing: " + identifier) continue (latitude, longitude) = m.group(1, 2) marker = DiscussionMarker.parse({ 'latitude': latitude, 'longitude': longitude, 'title': identifier, 'identifier': identifier }) try: db_session.add(marker) db_session.commit() print("Added: " + identifier, end="") except: db_session.rollback() print("Failed: " + identifier, end="")
def db_save(data): try: commend = Commends(data) db_session.add(commend) db_session.commit() except sqlalchemy.exc.DataError as err: db_session.rollback()
def store_user_entered_placedata(user_id): if 'email' in session: u = UserData.query.filter(UserData.id == user_id).all() if u[0].email != session['email']: return redirect(url_for('login')) place = request.json[0]['place'] start_date = request.json[0]['start_date'] end_date = request.json[0]['end_date'] try: temp = UserPlace(user_id=int(user_id), place=place, start_date=start_date, end_date=end_date) db_session.add(temp) db_session.commit() except IntegrityError: db_session.rollback() friends = [i['name'] for i in request.json[0]['friends']] vfr = db_session.query(UserData.name) \ .join(UserPlace, UserPlace.user_id == UserData.id) \ .filter(and_( UserData.name.in_(friends), UserPlace.place == place)).all() return jsonify(visited_friends=vfr) else: return redirect(url_for('login'))
def contests_del(contest_id): """ Deletes a contest Params: contest_id (int): the contest to delete Returns: a redirect to the contest view page """ contest = model.Contest.query.filter_by(id=int(contest_id)).scalar() if contest is None: error = "Failed to delete contest '{}' as it doesn't exist.".format( contest.name) current_app.logger.info(error) flash(error, "danger") return redirect(url_for("contests.contests_view")) try: db_session.delete(contest) db_session.commit() flash("Deleted contest '{}'".format(contest.name), "warning") except IntegrityError: db_session.rollback() error = "Failed to delete contest '{}' as it's referenced in another DB element".format( contest.name) current_app.logger.info(error) flash(error, "danger") return redirect(url_for("contests.contests_view"))
def modify_all(): u = isLogin() if u == None: abort(404) try: session = request.environ['beaker.session'] password = request.json['password'] major = request.json['major'] em = request.json['email'] fb_id = request.json['fb_id'] if u['email'] != em and User(em).checkDup() == True: return make_response('', 409) if User(u['email'], '', '', '', int(u['id'])).modify( { 'email' : em, 'password' : password, 'major' : major, 'fid' : fb_id } ) > 0 : resp = make_response('', 200) resp.headers['Location'] = '/api/users/me' u = User(em, password) u.checkLogin() session['user'] = u.getUser() session.save() db_session.commit() return resp else: db_session.rollback() abort(404) except: db_session.rollback() abort(400)
def thewarbase(): db_query = Settings.query.order_by(Settings.WarbaseFaction).first() the_id = "No faction stored" if not db_query else db_query.WarbaseFaction the_form = FactionIDForm(obj=db_query, WarbaseFaction=the_id) get_members = WarBase.query.order_by(WarBase.Level.desc()).all() if request.method == "POST": if the_form.validate_on_submit(): try: get_input = request.form['WarbaseFaction'] if not db_query: form = Settings(WarbaseFaction=get_input) db_session.add(form) db_session.commit() flash('Saved successfully, you may now hit update.', 'success') if db_query: if str(db_query.WarbaseFaction) != str(get_input): db_query.WarbaseFaction = get_input all_mems = WarBase.query.order_by(WarBase.Level.desc()).all() for person in all_mems: db_session.delete(person) db_session.commit() get_members = WarBase.query.order_by(WarBase.Level.desc()).all() flash('Saved successfully, you may now hit update.', 'success') else: flash(f'{get_input} is already the faction saved, hit update.', 'danger') except Exception as e: db_session.rollback() flash(f'Error updating faction {e}.', 'danger') return render_template('warbase.html', members=get_members, form=the_form) return render_template('warbase.html', members=get_members, form=the_form) if request.method == "GET": return render_template('warbase.html', members=get_members, form=the_form)
def save_thing(thing): '''just commit something into db''' db_session.add(thing) try: db_session.commit() except: db_session.rollback()
def clarifications_del(clar_id): """ Deletes a clarification Params: clar_id (int): the clarification to delete Returns: a redirect to the clarification view page """ clar = model.Clarification.query.filter_by(id=clar_id).scalar() if clar is None: error = "Failed to delete clarification '{}' as it doesn't exist.".format( clar_id) current_app.logger.info(error) flash(error, "danger") return redirect(url_for("clarifications.clarifications_view")) try: db_session.delete(clar) db_session.commit() flash("Deleted clarification '{}'".format(clar_id), "warning") except IntegrityError: db_session.rollback() error = "Failed to delete clarification '{}' as it's referenced in another DB element".format( clar_id) current_app.logger.info(error) flash(error, "danger") return redirect(url_for("clarifications.clarifications_view"))
def scrap_n_save_data(self, table_dom, issue_id, source_id): table = pq(table_dom)("tr td") article_id = str(uuid.uuid4().hex) r = lambda: random.randint(0, 255) try: if table.children("p").text() is not None and table.children( ".main-url").text() is not None and table.children( ".article-headline").attr("href"): article = Article( id=article_id, object_id=article_id, pre_content=table.children("p").text(), article_view_content=None, issue_id=issue_id, source_id=source_id, main_url=table.children(".main-url").text(), url=table.children(".article-headline").attr("href"), title=table.children(".article-headline").text(), img=str( "http://via.placeholder.com/300x300/{0}/{1}?text={2}". format('#%02X%02X%02X' % (r(), r(), r()), "#FFFFFF", "A"))) db_session.add(article) db_session.commit() except Exception as e: db_session.rollback() print(e) finally: db_session.close()
def scrap_response(self, source_id, source_url, issue_number): issue = Issue.query.filter(Issue.url == "{0}{1}".format( source_url, "issue-{0}".format(issue_number))).first() if issue is None: try: fetch_response = requests.get("{0}{1}".format( source_url, "issue-{0}".format(issue_number))) raw = pq(fetch_response.content.decode('utf-8')) issue_id = str(uuid.uuid4().hex) issue = Issue(id=issue_id, object_id=issue_id, issue_number=str(issue_number), source_id=source_id, url=str("{0}{1}".format( source_url, "issue-{0}".format(issue_number)))) db_session.add(issue) db_session.commit() raw(".issues table").map( lambda e, table_row: AndroidWeeklyScrapper. scrap_n_save_data(self, table_row, issue_id, source_id)) except Exception as e: db_session.rollback() print(e) return True else: return False
def post(self, *args, **kwargs): '''新增一条事件''' data = json.loads(self.request.body.decode("utf-8")) name = data.get('name', None) content = data.get('content', None) email = data.get('email', None) advance_at = data.get('advance_at', None) expire_at = data.get('expire_at', None) try: name_info = db_session.query(EventReminder).filter( EventReminder.name == name).first() if name_info: return self.write(dict(status=-1, msg="name already exist...")) else: print('1111') db_session.add( EventReminder(name=name, content=content, email=email, advance_at=advance_at, expire_at=expire_at)) db_session.commit() resp = {'status': 0, 'msg': '添加成功'} return self.write(resp) except Exception as e: print(e) db_session.rollback()
def populate_stations(self, *args, **kwargs): """ Initial Data Populate without parameters via api.get_all_stations """ stations = self.api.get_all_stations() for station in stations: station_data = { 'station_id': station['station_id'], 'ip_address': [models.IPAddress(value=ip) for ip in station['ip_address']], 'station_name': station['station_name'], 'status': station['status'], 'latitude': station['latitude'], 'longitude': station['longitude'], 'time_stamp': string_to_datetime(station['time_stamp']), } station_db_instance = models.Station(**station_data) db_session.add(station_db_instance) # Rollback if commit fails try: db_session.commit() except: db_session.rollback() raise StationPopulationError()
def upload_images(): if request.method == 'POST': idads = request.form['idads'] images = request.files.getlist('images[]') for image in images: if image and allowed_file(image.filename): extension = image.filename.split('.')[-1] if extension == "jpg": mime = "image/jpeg" else: mime = "image/"+extension i = Image(idads=idads, extension=extension, mime=mime, filename=image.filename, image=image.read()) db_session.add(i) try: db_session.commit() except: db_session.rollback() raise Exception('DB commit is not OK') return "Images uploaded?"
def snapshot_issues(repo, milestone): """ Fetches all of the issues for the given sprint and stores them in a database """ sprint = get_or_create_sprint(milestone) print "Processing {} ({})".format(sprint.name, milestone['number']) if sprint.locked is True: print "Skipping '{}', it's locked".format(sprint.name) return snapshot = Snapshot(sprint) db_session.add(snapshot) url = 'https://api.github.com/repos/{}/{}/issues?state=all&milestone={}'.format( settings.ORG, repo, milestone['number']) have_updates = False while True: issues = _auth_get_request(url) for issue in issues.json(): is_updated = save_issue_snapshot(repo, issue, snapshot) have_updates = have_updates or is_updated next_page = _get_next_page(issues) if next_page: url = next_page else: break if have_updates: print "Have updates, committing snapshot" db_session.commit() else: print "No updates, reverting snapshot" db_session.rollback()
def register(): form = RegisterForm() if request.method == "GET": return render_template("register.html", form=form) elif request.method == "POST": if form.validate_on_submit(): if models.User.query.get(form.username.data): try: user = models.User.query.get(form.username.data) except: db_session.rollback() return render_template("register.html", form=form, message="Error Ocurred") if form.password.data == user.password: login_user(user) return redirect(url_for("index")) else: return render_template("register.html", form=form, message="User Already Exists!") else: newUser = models.User(username=form.username.data, password=form.password.data) app.logger.debug("New User: "******"index")) else: abort(404)
def put_batch(id): req = request.get_json() try: batch = user_restrict( db_session.query(models.Batch), models.UserBatchLink).filter(models.Batch.id == id).one() params = { 'brew_date', 'rack_date', 'package_date', 'pre_boil_gravity', 'original_gravity', 'final_gravity' } for param in params & req.keys(): setattr(batch, param, req[param]) db_session.commit() return jsonify(hal.item(batch, href='/batch/{id}'.format(id=id))) except sqlalchemy.orm.exc.NoResultFound: db_session.rollback() return make_response(('Batch not found', 404)) except: db_session.rollback() raise
def languages_del(lang_id): """ Deletes a language Params: lang_id (int): the language to delete Returns: a redirect to the language view page """ lang = model.Language.query.filter_by(id=util.i(lang_id)).scalar() if lang is None: error = "Failed to delete language \'{}\' as it does not exist.".format( lang_id) current_app.logger.info(error) flash(error, "danger") return redirect(url_for("languages.languages_view")) try: db_session.delete(lang) db_session.commit() flash("Deleted language \'{}\'".format(lang.name), "warning") except IntegrityError: db_session.rollback() error = "Failed to delete language \'{}\' as it's referenced in another DB element".format( lang_id) current_app.logger.info(error) flash(error, "danger") return redirect(url_for("languages.languages_view"))
def check_lastfm(self): s = requests.session() r = s.get(LastFmPlugin.urls['recent_tracks']) resp = json.loads(r.text) try: tracks = resp['recenttracks']['track'] except KeyError: return ev_src = self.event_source('lastfm', 'Last.fm') ev_type = self.event_type(ev_src, 'TrackPlayed') for track in tracks: try: slug = track['date']['uts'] + track['mbid'] except KeyError: continue ev = models.Event(datetime.datetime.utcfromtimestamp( int(track['date']['uts'])), slug, ev_type.id, json.dumps(track)) try: db_session.add(ev) db_session.commit() except IntegrityError: db_session.rollback() dispatcher.add_timer(10, self.check_lastfm)
def main(): parser = argparse.ArgumentParser() parser.add_argument('identifiers', type=str, nargs='*', help='Disqus identifiers to create markers for') args = parser.parse_args() identifiers = args.identifiers if args.identifiers else sys.stdin for identifier in identifiers: m = re.match('\((\d+\.\d+),\s*(\d+\.\d+)\)', identifier) if not m: logging.error("Failed processing: " + identifier) continue (latitude, longitude) = m.group(1, 2) marker = DiscussionMarker.parse({ 'latitude': latitude, 'longitude': longitude, 'title': identifier, 'identifier': identifier }) try: db_session.add(marker) db_session.commit() logging.info("Added: " + identifier, end="") except: db_session.rollback() logging.warn("Failed: " + identifier, end="")
def on_data(self, data): """ Called by StreamListener whenever a new tweet arrives """ try: tweet_json = json.loads(data) coordinates = tweet_json.get('coordinates', {}).get( 'coordinates', None) if tweet_json.get('coordinates') else None place = tweet_json.get('place').get('full_name') if tweet_json.get( 'place') else '' if coordinates is None: # If we don't have coordinates we dont care about this tweet return True tweet = Tweet(id=tweet_json.get('id'), tweet_text=tweet_json.get('text'), user_id=tweet_json.get('user').get('id'), coordinates=coordinates, created_at=tweet_json.get('created_at'), json=tweet_json, last_update=datetime.now(), place=place) tweet.save() except: print_exc() db_session.rollback() return True
def parseImage(self, response): idads = response.meta['idads'] craglink = response.url img = Image.query.filter(Image.craglink == craglink).first() if img: db_session.delete(img) db_session.commit() extension = craglink.split('.')[-1] filename = craglink.split('/')[-1] if extension == "jpg": mime = "image/jpeg" else: mime = "image/" + extension image = response.body img = Image(extension = extension, mime = mime, filename = filename, craglink = craglink, idads = idads, image = image) db_session.add(img) try: db_session.commit() except: db_session.rollback() raise Exception("DB commit is not OK")
def connect_player(player_id): """ For a specific player, looks for another available player and connects to him In this setting, only players with same session number and pair can be connected, but this can be removed """ # We get the corresponding player try: player = Player.query.get(player_id) except: # if the player does not exist, we stop return 0 connected = False while not connected: # We loop until we find an available player print(" [ {} waiting for connections the lock... ]".format(player_id)) # We wait for the connection lock app.lock_connections.acquire() print(" [ {} has acquired the connections lock... ]".format(player_id)) # We refresh the player values to see if it has been connected already player = Player.query.get(player_id) conn = player.connection if conn.connected_player_id is not None: # The player has already been connected connected = True app.lock_connections.release() else: # We look for the other player q = db_session.query(Connection).join(Connection.players).filter(Connection.player_id!=player_id, Connection.status==0, Player.session_nr==player.session_nr, Player.pair==player.pair) try: conn_with = q.first() # We connect the 2 players and assign the roles conn.status = 1 conn.connected_player_id = conn_with.players.id conn.role = 0 conn_with.status = 1 conn_with.connected_player_id = conn.players.id conn_with.role = 1 db_session.commit() connected = True print("Connected {} with {}".format(conn.player_id, conn_with.player_id)) except Exception as e: # No player was found, we cancel any database modification db_session.rollback() print("No other player available... New attempt...") finally: # We release the lock app.lock_connections.release() print(" [ {} has released the connections lock ]".format(player_id)) if not connected: # We wait before attempting again time.sleep(1) return 0
def purgeExperimentResponse(): db_session.query(ExperimentResponse).delete() try: db_session.commit() except: db_session.rollback() raise return False
def _get_posts(s, batch, county_id, n, p): url = 'http://api.arbetsformedlingen.se/af/v0/platsannonser/matchning?lanid=%s&antalrader=%s&sida=%s' % (county_id, n, p) response = s.get(url=url) try: obj = json.loads(response.text) for index, item in enumerate(obj["matchningslista"]["matchningdata"]): try: post = Post.query.filter_by(external_id=str(item["annonsid"])).first() if post: app.logger.info('Post %s already exists, adding to new batch.' % post.external_id) else: post = Post() post.external_id = str(item["annonsid"]) post.published = parse(item["publiceraddatum"]) # profession is mandatory post.profession = Profession.query.filter_by(name=item["yrkesbenamning"]).one() if "antalplatser" in item: try: post.num_jobs = int(item["antalplatser"]) except ValueError: app.logger.warning('Could not parse number of jobs for %s' % post.external_id) # municipality is optional try: post.municipality = Municipality.query.filter_by(id=int(item["kommunkod"])).one() except (NoResultFound, KeyError): app.logger.warning('No municipality match "%s", post annonsid=%s, saving with unspecified.' % (item["kommunkod"], post.external_id)) post.municipality = Municipality.query.filter_by(id="Ospecifierad arbetsort").one() post.match_data = item db_session.add(post) post.batches.append(batch) db_session.commit() db_session.flush() except KeyError as e: app.logger.error('Attribute "%s" missing from dict. Aborting.' % e) app.logger.debug(json.dumps(item, indent=4)) except IntegrityError: app.logger.warning('Post already exists for %s' % item["annonsid"]) db_session.rollback() except ValueError as e: app.logger.error(e) app.logger.debug(json.dumps(item, indent=4)) except NoResultFound as e: app.logger.error('Could not find match for %s. Aborting.' % item["annonsid"]) app.logger.debug(e) # fixme: this will abort parsing if server runs out of memory pages = obj["matchningslista"]["antal_sidor"] if p < pages: app.logger.debug('Retrieving page %s of %s' % (p + 1, pages)) _get_posts(s, batch, county_id, n, p + 1) except ValueError: app.logger.error('Could not parse json response from API. Aborting.') app.logger.debug(response.text)
def init_source(self): try: self.source = models.EventSource('github', 'Github.com') db_session.add(self.source) db_session.commit() except IntegrityError: db_session.rollback() self.source = models.EventSource.query.filter( models.EventSource.name=='github').one()
def deleteProcessorRequest(access_token): db_session.query(ProcessorRequest).filter(ProcessorRequest.token==access_token).delete() try: db_session.commit() except: db_session.rollback() raise return False return True
def insert_movie(the_movie): movie = Movie.from_tmdb(the_movie, True) db_session.add(movie) try: db_session.commit() return movie.movieid except: db_session.rollback() raise
def ad_add(): idcrag = request.form['idcrag'].strip() description = request.form['description'].strip() title = request.form['title'].strip() posting_time = request.form['posting_time'].strip() scheduled_action = request.form['scheduled_action'].strip() repost_timeout = request.form['repost_timeout'].strip() prev_action = "" prev_act_time = "" prev_act_stat = "" status = "Not posted" idusers = request.form['idusers'].strip() idcategory = request.form['category'].strip() idarea = request.form['area'].strip() replymail = None #request.form['replymail'] allowed_actions = "None,add" contact_phone = request.form['contact_phone'].strip() contact_name = request.form['contact_name'].strip() postal = request.form['postal'].strip() specific_location = request.form['specific_location'].strip() haslicense = request.form['has_license'].strip() license_info = request.form['license'].strip() a = Ad(idcrag, description, title, posting_time, scheduled_action, repost_timeout, prev_action, prev_act_time, prev_act_stat, status, idusers, idcategory, idarea, replymail, allowed_actions, contact_phone, contact_name, postal, specific_location, haslicense, license_info) db_session.add(a) try:db_session.commit() except Exception as e: db_session.rollback() logging.error("Application did not create "+a.__repr__()+ " becouse " + e.message) raise Exception('DB commit is not OK') return a.idads.__str__()
def reset_database(self): """Clean the db.""" db_session.query(Movie).delete() db_session.query(File).delete() try: db_session.commit() except: db_session.rollback() logging.error('Error cleaning up the database.') raise
def user_delete(user_id): u = User.query.filter(User.idusers == user_id).first() db_session.delete(u) try:db_session.commit() except Exception as e: db_session.rollback() logging.error("Application did not delete "+u.__repr__()+ " becouse " + e.message) raise Exception('DB commit is not OK') return "User deleted"
def ad_delete(ad_id): a = Ad.query.filter(Ad.idads == ad_id).first() db_session.delete(a) try:db_session.commit() except Exception as e: db_session.rollback() logging.error("Application did not delete "+a.__repr__()+ " becouse " + e.message) raise Exception('DB commit is not OK') return "Ad deleted"
def deleteSpeciality(): SpecObj = request.get_json() deleteSpeciality = Speciality.query.get(SpecObj['name']) db_session.delete(deleteSpeciality) try: db_session.commit() except SQLAlchemyError as exception: db_session.rollback() response = {'returnCode': "SUCCESS", 'data':{}, 'errorCode':None} return jsonify(response)
def delete_image(idimages): i = Image.query.filter(Image.idimages == idimages).first() db_session.delete(i) try: db_session.commit() except: db_session.rollback() raise Exception("DB commit is not OK") return "Image deleted"
def addSpeciality(): specJson = request.get_json() speciality = Speciality(specJson['name']) db_session.add(speciality) try: db_session.commit() except SQLAlchemyError as exception: db_session.rollback() page = {'returnCode': "SUCCESS", 'data':{}, 'errorCode':None} return jsonify(page)
def deleteDoctor(): doctorObj = request.get_json() deleteDoctorObj = Doctor.query.get(doctorObj['id']) db_session.delete(deleteDoctorObj) try: db_session.commit() except SQLAlchemyError as exception: db_session.rollback() page = {'returnCode': "SUCCESS", 'data':{}, 'errorCode':None} return jsonify(page)
def remove_movies(self, movie_dict): """Remove movie in movie_dict from the db.""" for path, idz in movie_dict.iteritems(): logging.info("removing... %s", path.encode('utf-8')) db_session.query(File).filter(File.filepath == path).delete() try: db_session.commit() except: db_session.rollback() logging.error('Error removing files from the database.') raise
def addIdentifier(catalog, redirect, clientid): identifier = Identifier(id=clientid, redirect=redirect, catalog=catalog) try: db_session.add(identifier) db_session.commit() except: db_session.rollback() raise return False return True
def addProcessorRequest(state, catalog, resource, resource_uri, redirect, expiry, query): prorec = ProcessorRequest(state=state, catalog=catalog, resource=resource, resource_uri=resource_uri, redirect=redirect, expiry=expiry, query=query, status="pending") try: db_session.add(prorec) db_session.commit() except: db_session.rollback() raise return False return True
def create_task(): data = request.get_json() task = Task.create_from_dict(data) try: db_session.add(task) db_session.commit() except IntegrityError as e: db_session.rollback() return jsonify(dict(message=str(e))), 409 return jsonify(task.to_dict()), 201
def create_user(): data = request.get_json() try: user = User.create_from_dict(data) db_session.add(user) db_session.commit() except IntegrityError: db_session.rollback() return jsonify(dict(message='Login already in use')), 409 return jsonify(user.to_dict()), 201