def _process_house_item(self, item): item['area'] = int(item['area'][:-1]) item['rent'] = int(item['rent']) session.add(House(**item)) session.commit() session.close() return item
def reduction_started(self): logger.info("Run %s has started reduction" % self._data_dict['run_number']) reduction_run = self.find_run() if reduction_run: if str(reduction_run.status.value) == "Error" or str( reduction_run.status.value) == "Queued": reduction_run.status = StatusUtils().get_processing() reduction_run.started = datetime.datetime.now() session.add(reduction_run) session.commit() else: logger.error( "An invalid attempt to re-start a reduction run was captured. Experiment: %s, " "Run Number: %s, Run Version %s" % (self._data_dict['rb_number'], self._data_dict['run_number'], self._data_dict['run_version'])) else: logger.error( "A reduction run started that wasn't found in the database. Experiment: %s, Run Number: %s, " "Run Version %s" % (self._data_dict['rb_number'], self._data_dict['run_number'], self._data_dict['run_version']))
def _create_variables(self, instrument, script, variable_dict, is_advanced): variables = [] for key, value in variable_dict.iteritems(): str_value = str(value).replace('[', '').replace(']', '') if len(str_value) > 300: raise DataTooLong variable = Variable(name=key, value=str_value, type=VariableUtils().get_type_string(value), is_advanced=is_advanced, help_text=self._get_help_text( 'standard_vars', key, instrument.name, script)) instrument_variable = InstrumentVariable(start_run=0, instrument=instrument, variable=variable, tracks_script=1) session.add(variable) session.add(instrument_variable) session.commit() variables.append(instrument_variable) return variables
def to_html(self): # not good to have imports here... from queries import get_question, get_last_question_response, get_peer_tasks_for_grader, get_self_tasks_for_student from auth import validate_user self.set_metadata() user = validate_user() if user.type == "guest": return "Sorry, but only students can view peer assessment questions." vars = {"question": get_question(self.question_id)} # if peer assessment was assigned if len(self.peer_pts): peer_tasks = get_peer_tasks_for_grader(self.question_id, user.stuid) vars['peer_responses'] = [get_last_question_response(self.question_id, task.student) for task in peer_tasks] # if self assessment was assigned if self.self_pts is not None: # add the task if it hasn't been already response = get_last_question_response(self.question_id, user.stuid) if response and not get_self_tasks_for_student(self.question_id, user.stuid): session.add(GradingTask(grader=user.stuid, student=user.stuid, question_id=self.question_id)) session.commit() vars['self_response'] = response # jinja2 to get template for peer review questions from jinja2 import Environment, PackageLoader env = Environment(autoescape=True, loader=PackageLoader("ohms", "templates")) template = env.get_template("peer_review_question.html") return template.render(**vars)
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 delete_category(category_id): """ Deleting category by id """ category_to_delete = session.query(Category)\ .filter(Category.id == category_id).first() session.delete(category_to_delete) session.commit()
def delete_movie(id): session.query(Movie)\ .filter(Movie.id == id)\ .delete() session.commit() message = 'Movie {} was deleted!'.format(id) dic = {'message': message} return jsonify(dic)
def update_from_xml(self, xml): node = ET.fromstring(xml) for i, q in enumerate(node.iter('question')): q_object = Question.from_xml(q) q_object.homework = self self.questions.append(q_object) session.commit()
def submit_response(self, stuid, responses): from queries import get_peer_tasks_for_grader, get_self_tasks_for_student self.set_metadata() if pdt_now() <= self.homework.due_date: # helper function that validates and then updates a task def check_and_update_task(task): if task.grader != stuid: raise Exception("You are not authorized to grade this response.") try: assert(0 <= float(responses[2*i]) <= task.question.points) except: raise Exception("Please enter a score between %f and %f." % (0, task.question.points)) if not responses[2*i+1].strip(): raise Exception("Please enter comments for all responses.") task.time = pdt_now() task.score = responses[2*i] task.comments = responses[2*i+1] return task i = 0 score = 0. # peer tasks should be first while i < len(self.peer_pts): tasks = get_peer_tasks_for_grader(self.question_id, stuid) task = check_and_update_task(tasks[i]) if task.comments is not None: score += self.peer_pts[i] i += 1 # then we have self tasks if self.self_pts is not None: tasks = get_self_tasks_for_student(self.question_id, stuid) if tasks: task = check_and_update_task(tasks[0]) if task.comments is not None: score += self.self_pts comments = "Watch this space for your peers' judgment of your feedback. " if self.rate_pts: comments += "Your peers' judgment is worth %f points, so you cannot earn all %s points yet." % (self.rate_pts, self.points) session.commit() return { 'locked': (pdt_now() > self.homework.due_date), 'submission': QuestionResponse( time=pdt_now(), score=score, comments=comments ) } else: raise Exception("The deadline for submitting this homework has passed.")
def _process_job_item(self, item): item['salary_lower'] = int(item['salary_lower'][:-1]) item['salary_upper'] = int(item['salary_upper'][:-1]) item['experience_lower'] = int(item['experience_lower']) item['experience_upper'] = int(item['experience_upper']) session.add(Job(**item)) session.commit() session.close() return item
def save_run_variables(self, instrument_vars, reduction_run): logger.info('Saving run variables for ' + str(reduction_run.run_number)) run_variables = map( lambda ins_var: self.derive_run_variable(ins_var, reduction_run), instrument_vars) for run_variable in run_variables: session.add(run_variable) session.commit() return run_variables
def signup(): form = RegisterForm() if form.validate_on_submit(): new_user = User(form.username.data, form.email.data, form.password.data) session.add(new_user) session.commit() flash('Thanks for registrering, You are a registered user now !!') return redirect('/login') return render_template('register.html', titles='Register', form=form)
def add_category(): """ Adding new category to DB with fields from request form * better replace with arguments, to make method more independent """ name = flask.request.form['name'] owner = user_info()['id'] new_category = Category(name, owner) session.add(new_category) session.commit()
def update_category(category_id): """ Updating category in DB with fields from request form * better replace with arguments, to make method more independent """ name = flask.request.form['name'] category_to_update = session.query(Category).get(category_id) category_to_update.name = name session.add(category_to_update) session.commit()
def log_error_and_notify(message): """ Helper method to log an error and save a notifcation """ logger.error(message) notification = Notification(is_active=True, is_staff_only=True, severity='e', message=message) session.add(notification) session.commit()
def update(_id): content = request.get_json() nom = content['nom'] email = content['email'] password = content['password'] updated_user = Data.User(nom, email, password, kwargs={'id': _id}) updated_user.id = _id session.merge(updated_user) session.flush() session.commit() return "user updated "
def post(): print(request.is_json) content = request.get_json() nom = content['nom'] email = content['email'] password = content['password'] user = Data.User(nom, email, password) session.add(user) session.flush() session.commit() return 'User added'
def newCategory(): if not userIsLoggedIn(): return redirect(url_for('showLogin')) if request.method == 'POST': newCategory = Category(name=request.form['name']) newCategory.user_id = login_session['user_id'] session.add(newCategory) flash('New Category %s Successfully Created' % newCategory.name) session.commit() return redirect(url_for('showCategories')) else: return render_template('newCategory.html')
def _get_status(status_value): """ Helper method that will try to get a status matching the given name or create one if it doesn't yet exist """ status = session.query(Status).filter_by(value=status_value)[0] if status is None: new_status = Status(value=status_value) session.add(new_status) session.commit() status = session.query(InstrumentVariable).filter_by(value=status_value)[0] logger.warn("%s status was not found, created it." % status_value) return status
def add_user(self, user_id): """ Добавляем пользователя в БД. """ candidate_user = User(user_id=user_id, green_points=0, green_tickets=0) session.add(candidate_user) try: session.commit() return True except sqlalchemy.exc.IntegrityError: session.rollback() return False
def reduction_complete(self): try: logger.info("Run %s has completed reduction" % self._data_dict['run_number']) reduction_run = self.find_run() if reduction_run: if reduction_run.status.value == "Processing": reduction_run.status = StatusUtils().get_completed() reduction_run.finished = datetime.datetime.now() for name in ['message', 'reduction_log', 'admin_log']: setattr(reduction_run, name, self._data_dict.get(name, "")) if 'reduction_data' in self._data_dict: for location in self._data_dict['reduction_data']: reduction_location = ReductionLocation( file_path=location, reduction_run=reduction_run) session.add(reduction_location) session.commit() # Get any .png files and store them as base64 strings # Currently doesn't check sub-directories graphs = glob.glob(location + '*.[pP][nN][gG]') for graph in graphs: with open(graph, "rb") as image_file: encoded_string = 'data:image/png;base64,' + base64.b64encode( image_file.read()) if reduction_run.graph is None: reduction_run.graph = [encoded_string] else: reduction_run.graph.append( encoded_string) session.add(reduction_run) session.commit() else: logger.error( "An invalid attempt to complete a reduction run that wasn't processing has been " "captured. Experiment: %s, Run Number: %s, Run Version %s" % (self._data_dict['rb_number'], self._data_dict['run_number'], self._data_dict['run_version'])) else: logger.error( "A reduction run completed that wasn't found in the database. Experiment: %s, Run Number: " "%s, Run Version %s" % (self._data_dict['rb_number'], self._data_dict['run_number'], self._data_dict['run_version'])) except BaseException as e: logger.error("Error: %s" % e)
def insert_member(member): """ Persists member data into database :param member: the member data :return: """ assert member is not None, 'Member not specified' try: session.add(member) # Commit session session.commit() except (exc.SQLAlchemyError, exc.DBAPIError) as e: logging.error("Failed to insert member: [" + member.to_string() + "]: " + str(e))
def add_item(category_id): """ Adding item to DB with fields from request form * better replace with arguments, to make method more independent """ name = flask.request.form['name'] owner = user_info()['id'] description = flask.request.form['description'] new_item = Item(name, description, owner) new_item.category_id = category_id session.add(new_item) session.commit()
def update_item(item_id): """ Updating item in DB with fields from request form * better replace with arguments, to make method more independent """ name = flask.request.form['name'] description = flask.request.form['description'] item_to_update = session.query(Item).get(item_id) item_to_update.name = name item_to_update.description = description session.add(item_to_update) session.commit()
def deleteCategory(category_id): categoryToDelete = session.query( Category).filter_by(id=category_id).one() if not userIsOwner(categoryToDelete.user_id): return redirect(url_for('showLogin')) if request.method == 'POST': session.delete(categoryToDelete) flash('%s Successfully Deleted' % categoryToDelete.name) session.commit() return redirect(url_for('showCategories', category_id=category_id)) else: return render_template('deleteCategory.html', category=categoryToDelete)
def deleteItem(category_id, item_id): category = session.query(Category).filter_by(id=category_id).one() itemToDelete = session.query(Item).filter_by(id=item_id).one() if not userIsOwner(itemToDelete.user_id): return redirect(url_for('showLogin')) if request.method == 'POST': session.delete(itemToDelete) session.commit() flash('Menu Item Successfully Deleted') return redirect(url_for('showItems', category_id=category_id)) else: return render_template('deleteItem.html', item=itemToDelete)
def insert_location(location): """ Persists location data into database :param location: the location data :return: """ assert location is not None, 'Location not specified' try: session.add(location) # Commit session session.commit() except (exc.SQLAlchemyError, exc.DBAPIError) as e: logging.error("Failed to insert location: [" + location.to_string() + "]: " + str(e))
def add_points(self, user_id, item_id): """ Дабавляем бонусы за переработку/утилизация Если ранее пользователь не пользовался приложением он заносится в БД. """ user_id = session.query(User).filter(User.user_id == user_id).all() item_green_point = 12.5 User.green_points += item_green_point try: session.commit() return True except sqlalchemy.exc.IntegrityError: return False
def reduction_error(self): if 'message' in self._data_dict: logger.info( "Run %s has encountered an error - %s" % (self._data_dict['run_number'], self._data_dict['message'])) else: logger.info( "Run %s has encountered an error - No error message was found" % (self._data_dict['run_number'])) reduction_run = self.find_run() if not reduction_run: logger.error( "A reduction run that caused an error wasn't found in the database. Experiment: %s, " "Run Number: %s, Run Version %s" % (self._data_dict['rb_number'], self._data_dict['run_number'], self._data_dict['run_version'])) return reduction_run.status = StatusUtils().get_error() # TODO : Here, and in other places where datetime is used, need to make sure we are using GMT. reduction_run.finished = datetime.datetime.now() for name in ['message', 'reduction_log', 'admin_log']: setattr(reduction_run, name, self._data_dict.get(name, "")) session.add(reduction_run) session.commit() if 'retry_in' in self._data_dict: experiment = session.query(Experiment).filter_by( reference_number=self._data_dict['rb_number']).first() previous_runs = session.query(ReductionRun).filter_by( run_number=self._data_dict['run_number'], experiment=experiment).all() max_version = -1 for previous_run in previous_runs: current_version = previous_run.run_version if current_version > max_version: max_version = current_version # If we have already tried more than 5 times, we want to give up and we don't want to retry the run if max_version <= 4: self.retry_run(reduction_run, self._data_dict["retry_in"]) else: # Need to delete the retry_in entry from the dictionary so that the front end doesn't report a false # retry instance. del self._data_dict['retry_in'] self.notify_run_failure(reduction_run)
def get_new_yaks(): yaks = client.get_new_yaks(config['lat'], config['long']) for yak in yaks: cur_yak = session.query(db.Yak).filter_by(yid=yak.message_id).first() if not cur_yak: cur_yak = db.Yak(yid=yak.message_id, time=yak.time, text=yak.message, score=yak.number_of_likes, handle=yak.nickname or '') print(' NEW:', repr(cur_yak)) session.add(cur_yak) else: if cur_yak.score != yak.number_of_likes: diff = yak.number_of_likes - cur_yak.score cur_yak.score = yak.number_of_likes print('{: =+4}:'.format(diff), repr(cur_yak)) session.commit()
def delete_member(member): """ Delete the member data from database :param member: :return: """ assert member is not None, 'Member not specified' try: session.query(Member)\ .filter(Member.member_id == member.member_id)\ .delete() # Commit session session.commit() except (exc.SQLAlchemyError, exc.DBAPIError) as e: logging.error("Failed to delete member [" + member.to_string() + "]: " + str(e))
def from_xml(node): # if question already has ID assigned, fetch it if 'id' in node.attrib: question = session.query(Question).get(node.attrib['id']) # otherwise, create a new one else: if 'review' in node.attrib and 't' in node.attrib['review'].lower(): question = PeerReview() else: question = Question() session.add(question) session.flush() question.sync_from_node(node) session.commit() return question
def submit_response(self, stuid, responses): from queries import get_last_question_response last_submission = get_last_question_response(self.id, stuid) if not self.check_if_locked(last_submission): item_responses = [ItemResponse(item_id=item.id, response=response) \ for item, response in zip(self.items, responses)] score, comments = self.check(responses) question_response = QuestionResponse( stuid=stuid, time=pdt_now(), question_id=self.id, item_responses=item_responses, score=score, comments=comments ) session.add(question_response) session.commit() return { 'submission': self.delay_feedback(question_response), 'locked': self.check_if_locked(question_response), } else: raise Exception("The deadline for submitting this homework has passed.")
def assign_tasks(hw_id, due_date, send_emails=False): homework = session.query(Homework).get(hw_id) users = session.query(User).filter(User.type == "student").order_by(User.stuid).all() for q in homework.questions: # only if this is a peer graded question if not isinstance(q.items[0], LongAnswerItem): continue random.seed(q.id) # setting a seed will be useful for debugging responsible_kids = list() # did the homework! irresponsible_kids = list() # didn't do the homework! # Figure out who did the homework for user in users: if get_last_question_response(q.id, user.stuid): responsible_kids.append(user.stuid) else: irresponsible_kids.append(user.stuid) # Make the assignments for the responsible kids n = len(responsible_kids) random.shuffle(responsible_kids) for i, stuid in enumerate(responsible_kids): # Make the assignments for this responsible student for offset in [1, 3, 6]: j = (i + offset) % n gt = GradingTask(grader=stuid, student=responsible_kids[j], question_id=q.id) session.add(gt) # Make the assignments for the irresponsible kids: # Do so in round robin order, shuffling the responsible students again # to minimize the number of pairs of students grading together. random.shuffle(responsible_kids) for i, stuid in enumerate(irresponsible_kids): # Make the assignments for this irresponsible student for offset in range(3): j = (i * 3 + offset) % n gt = GradingTask(grader=stuid, student=responsible_kids[j], question_id=q.id) session.add(gt) # Make all self-assignments for stuid in (responsible_kids + irresponsible_kids): gt = GradingTask(grader=stuid, student=stuid, question_id=q.id) session.add(gt) session.commit() if not send_emails: return "Successfully assigned %d students" # Send email notifications to all the students send_all(users, "Peer Assessment for %s is Ready" % homework.name[:-1], r"""Dear %s, We've made the peer-grading assignments for this week. The assessments are due {due_date}, and you can start after lecture today at 11:00AM. You will be able to view your peer's comments on your answers as they are submitted, but your score will not be available until {due_date}. At that time, please log in to view and respond to the comments you received from your peers. Best, STATS 60 Staff""".format(due_date=due_date.strftime("%A, %b %d at %I:%M %p"))) # Send email to the course staff admins = session.query(User).filter_by(type="admin").all() send_all(admins, "Peer Assessment for %s is Ready" % homework.name[:-1], r"""Dear %s (and other members of the STATS 60 Staff), Just letting you know that the peer assessment for this week was just released. It is due at {due_date}. Sincerely, OHMS P.S. This is an automatically generated message ;-) """.format(due_date=due_date.strftime("%A, %b %d at %I:%M %p"))) return r'''Successfully assigned %d students. You should have received an e-mail confirmation.''' % len(users)
def add_grade(student, homework, score, excused=False): grade = Grade(student=student, homework=homework, score=score, excused=excused) session.add(grade) session.commit()
@app.errorhandler(Exception) def handle_exceptions(error): return make_response(error.message, 403) stuid = auth_stuid() if not stuid: raise Exception("You are no longer logged in. Please refresh the page.") try: user = get_user(stuid) except: user = User(stuid=stuid, name=auth_student_name(), type="student") session.add(user) session.commit() @app.route("/") def index(): return render_template("admin/index.html", options=options, user=user) @app.route("/reminder_email/<int:hw_id>", methods=['POST']) def reminder_email(hw_id): homework = session.query(Homework).get(hw_id) # get users who haven't completed peer grading users = set() for question in homework.questions: tasks = session.query(GradingTask).join(QuestionResponse).\