def newDepartment(): if request.method == 'POST': name = request.form['name'] address = request.form['address'] img_url = request.form['img_url'] description = request.form['description'] # create new department and commit to DB try: newDepartment = Department(name=name, address=address, img_url=img_url, description=description, user_id=login_session['user_id']) session.add(newDepartment) session.commit() # handle database exceptions except (DBAPIError, SQLAlchemyError) as e: flash("An exception occurred in the database. Please try again!") return redirect(url_for('showDepartments')) else: flash("Successfully created new department %s" % newDepartment.name) return redirect(url_for('showDepartments')) else: departments = session.query(Department).order_by(asc(Department.name)) return render_template("new_department.html", departments=departments)
def createUser(login_session): newUser = User(name=login_session['username'], email=login_session['email'], picture=login_session['picture']) session.add(newUser) session.commit() user = session.query(User).filter_by(email=login_session['email']).one() return user.id
def failed(self, message): self.processing = False self.error = message self.tries_left = self.tries_left - 1 self.next_try_at = datetime.now() + timedelta(seconds=1) session.add(self) session.commit()
def create_new_task(): '''Create new task''' # TODO: avoid duplicate data = request.get_json() url = correct_url(data['url']) logger.info('Creating new task with data {}'.format(data)) task = CareTask(name=data.get('name'), url=url, interval=data['interval'], roi=data.get('roi')) session.add(task) session.commit() rand_snapshot = data.get('snapshot') if rand_snapshot: os.rename('../snapshot/{}'.format(rand_snapshot), '../snapshot/{}-0.png'.format(task.id)) return 'success'
def perform(self): debit = self.payment.product.price balance = self.user.balance balance -= debit if balance < 0: message = f''' User doesn't have enought credits. product price: {debit}, current balance: {self.user.balance} ''' if self.tries_left > 0: self.failed(message) print('payment failed or incomplete') return else: self.user.balance = balance try: session.add(self.user) session.commit() return except Exception as ex: print(str(ex))
divs_lei = BS.find_all("div", {"class": "lei"}) for div in divs_lei: big_rank = "D" if ("A" in div.text): big_rank = "A" elif ("B" in div.text): big_rank = "B" elif ("C" in div.text): big_rank = "C" else: continue table = div.next_sibling.next_sibling rows = table.find_all("tr") for row in rows[1:]: tds = row.find_all("td") data = [x.text.strip() for x in tds] new_venue = Venue() new_venue.small_rank = int(data[0]) new_venue.abb = data[1] new_venue.name = data[2] new_venue.publishing_house = data[3] new_venue.url = data[4] new_venue.big_rank = big_rank try: session.add(new_venue) # print("Add: [%s:%s]" % (new_venue.abb,str(new_venue.big_rank)+str(new_venue.small_rank))) session.commit() except Exception as e: session.rollback() print(e)
# Query one task task = (session.query(Task).join(Story).join(User) .filter(Story.story_title == 'Story 001') .filter(User.user_name == 'User 003')).one() logger.info('Task in Story 001 and owned by User 003 is:') logger.info('\tid = ' + str(task.id)) logger.info('\ttask_title = ' + task.task_title) logger.info('\tstory_id = ' + str(task.story_id)) logger.info('\tuser_id = ' + str(task.user_id)) # Delete this task and query again session.delete(task) session.commit() num = (session.query(Task).join(Story).join(User) .filter(Story.story_title == 'Story 001') .filter(User.user_name == 'User 003')).count() logger.info(str(num) + " task queried") # Add the task back and query again session.add(Task(id=3, task_title='Task 003', story_id=1, user_id=3)) session.commit() task = (session.query(Task).join(Story).join(User) .filter(Story.story_title == 'Story 001') .filter(User.user_name == 'User 003')).one() logger.info('Task in Story 001 and owned by User 003 is:') logger.info('\tid = ' + str(task.id)) logger.info('\ttask_title = ' + task.task_title) logger.info('\tstory_id = ' + str(task.story_id)) logger.info('\tuser_id = ' + str(task.user_id))
from db_objects import Story, User, Task from db_session import session stories = [Story(id=1, story_title='Story 001'), Story(id=2, story_title='Story 002'), Story(id=3, story_title='Story 003')] session.add(stories[0]) session.add(stories[1]) session.add(stories[2]) users = [User(id=1, user_name='User 001'), User(id=2, user_name='User 002'), User(id=3, user_name='User 003')] tasks = [Task(id=1, task_title='Task 001', story_id=1, user_id=1), Task(id=2, task_title='Task 002', story_id=1, user_id=2), Task(id=3, task_title='Task 003', story_id=1, user_id=3), Task(id=4, task_title='Task 004', story_id=2, user_id=1), Task(id=5, task_title='Task 005', story_id=2, user_id=2), Task(id=6, task_title='Task 006', story_id=2, user_id=3), Task(id=7, task_title='Task 007', story_id=3, user_id=1), Task(id=8, task_title='Task 008', story_id=3, user_id=2), Task(id=9, task_title='Task 009', story_id=3, user_id=3)] session.add_all(users + tasks) session.commit()
changed = compare_img(t, old_snapshot_path, new_snapshot_path, diff_img_path) check_log.changed = changed if changed: logger.info('[Task {}] Notify change'.format(t.id)) task_name = t.name if not task_name: task_name = t.url email_subject = '{} changed'.format(task_name) notify_change(email_subject, t.url, diff_img_path, diff_img_name) else: logger.info('[Task {}] No previous snapshot.'.format( t.id)) check_log.success = True except Exception as e: logger.exception(e) logger.error('[Task {}] fail to check update'.format(t.id)) session.add(check_log) except Exception as e: logger.exception(e) logger.error('[Task {}] fail to check time past'.format(t.id)) session.commit() session.remove() # close_driver()