def show_variables_for_run(self, instrument_name, run_number=None): """ Look for the applicable variables for the given run number. If none are set, return an empty list (or QuerySet) anyway. """ instrument = InstrumentUtils().get_instrument(instrument_name) variable_run_number = 0 # If we haven't been given a run number, we should try to find it. if not run_number: applicable_variables = session.query(InstrumentVariable).filter_by(instrument=instrument) \ .order_by('-start_run').all() if len(applicable_variables) != 0: variable_run_number = applicable_variables[0].start_run else: variable_run_number = run_number # Now use the InstrumentJoin class (which is a join of the InstrumentVariable and Variable tables) to make sure # we can make a copy of all the relevant variables with all of the right information. variables = (session.query(InstrumentJoin).filter_by( instrument=instrument, start_run=variable_run_number)).all() # If we have found some variables then we want to use them by first making copies of them and sending them back # to be used. This means we don't alter the previous set of variables. If we haven't found any variables, just # return an empty list. if len(variables) != 0: self._update_variables(variables) new_variables = [] for variable in variables: new_variables.append(VariableUtils().copy_variable(variable)) return new_variables else: return []
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 _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 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 showItems(category_id): category = session.query(Category).filter_by(id=category_id).one() items = session.query(Item).filter_by( category_id=category_id).all() # Get creator data creator = getUserInfo(category.user_id) template = 'publicmenu.html' if userIsOwner(category.user_id): template = 'items.html' return render_template(template, items=items, category=category, creator=creator)
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 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 get_last_due_homework(): """Returns the last homework that was due""" now = pdt_now() hws = reversed(session.query(Homework).order_by(Homework.due_date).all()) for hw in hws: if hw.due_date < now: return hw
def get_movie(): result = session.query(Movie.id, Movie.title, Movie.year, Country.name, Genre.name) \ .join(Country, isouter=True) \ .join(movies_genres_association, isouter=True) \ .join(Genre, isouter=True) \ .all() return db.transform_dataset_into_json(result)
def _make_pending_msg(reduction_run): # Deferred import to avoid circular dependencies from reduction_run_utils import ReductionRunUtils """ Creates a dict message from the given run, ready to be sent to ReductionPending. """ script, arguments = ReductionRunUtils().get_script_and_arguments( reduction_run) # Currently only support single location data_location = session.query(DataLocation).filter_by( reduction_run_id=reduction_run.id)[0] if data_location: data_path = data_location.file_path else: raise Exception("No data path found for reduction run") data_dict = { 'run_number': reduction_run.run_number, 'instrument': reduction_run.instrument.name, 'rb_number': str(reduction_run.experiment.reference_number), 'data': data_path, 'reduction_script': script, 'reduction_arguments': arguments, 'run_version': reduction_run.run_version, 'facility': FACILITY, 'message': '', } return data_dict
def delete_publisher(publisher): """ Delete the game publisher data from database :param publisher: the game publisher to be deleted from database :return: """ assert publisher is not None, 'Publisher not specified' try: session.query(Publisher)\ .filter(Publisher.publisher_id == publisher.publisher_id)\ .delete() # Commit session session.commit() except (exc.SQLAlchemyError, exc.DBAPIError) as e: logging.error("Failed to delete game publisher [" + publisher.to_string() + "]: " + str(e))
def delete_location(location): """ Delete the location data from database :param location: the location data to be deleted from database :return: """ assert location is not None, 'Location not specified' try: session.query(Location)\ .filter(Location.loc_id == location.loc_id)\ .delete() # Commit session session.commit() except (exc.SQLAlchemyError, exc.DBAPIError) as e: logging.error("Failed to delete location [" + location.to_string() + "]: " + str(e))
def get_category(category_id): """ Getting single category out of DB by id """ target_category = session.query(Category).get(category_id) return target_category
def delete_designer(designer): """ Delete the game designer data from database :param designer: the game designer data to be deleted from database :return: """ assert designer is not None, 'Designer not specified' try: session.query(Designer)\ .filter(Designer.designer_id == designer.loc_id)\ .delete() # Commit session session.commit() except (exc.SQLAlchemyError, exc.DBAPIError) as e: logging.error("Failed to delete game designer [" + designer.to_string() + "]: " + str(e))
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 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 get_last_designer(): """ Get the last game designer registered in database :return: """ return session.query(Designer) \ .order_by(Designer.designer_id.desc()) \ .first()
def get_last_location(): """ Get the last location registered in database :return: """ return session.query(Location) \ .order_by(Location.loc_id.desc()) \ .first()
def add_movie(): data = loads(request.data) title = data['title'] year = data['year'] country = data['country'] genre = data['genre'] movie_row = Movie( title=title, year=year, country=session.query(Country).filter(Country.name == country).first()) movie_row.genre.append( session.query(Genre).filter(Genre.name == genre).first()) session.add(movie_row) session.commit() message = 'Movie {} was added!'.format(title) dic = {'message': message} return jsonify(dic)
def get_last_publisher(): """ Get the last game publisher registered in database :return: the last game publisher """ return session.query(Publisher) \ .order_by(Publisher.publisher_id.desc()) \ .first()
def get_all_responses_to_question(question_id): users = session.query(User).filter_by(type="student").all() responses = [] for u in users: response = get_last_question_response(question_id, u.stuid) if response: responses.append(response) return responses
def get_last_member(): """ Get the last member registered in database :return: the last member """ return session.query(Member) \ .order_by(Member.member_id.desc()) \ .first()
def verify_password(username, password): hash_password = md5(password.encode('utf-8')).hexdigest() stored_password = session.query( User.password_hash).filter_by(username=username).first()[0] if hash_password == stored_password: g.username = username return True else: return False
def get_designer_by_id(designer_id): """ Get the designer data from database having a given id :param designer_id: the designer identifier :return: the designer data from database """ return session.query(Designer) \ .filter(Designer.designer_id == designer_id) \ .first()
def get_designer_by_name(name): """ Get the game designer data from database having a given name :param name: the name of the game designer :return: the game designer data from database """ return session.query(Designer) \ .filter(Designer.name == name) \ .first()
def get_member_by_id(member_id): """ Get the member data from database having a given id :param member_id: the member identifier :return: the member data from database """ return session.query(Member) \ .filter(Member.member_id == member_id) \ .first()
def get_member_by_number(number): """ Get the member data from database having a given number :param number: the member number :return: the member data from database """ return session.query(Member) \ .filter(Member.number == number) \ .first()
def get_location_by_id(loc_id): """ Get the location data from database having a given id :param loc_id: the location identifier :return: the location data from database """ return session.query(Location) \ .filter(Location.loc_id == loc_id) \ .first()
def get_member_by_name(name): """ Get the member data from database having to a given name :param name: the member name :return: the member data from database """ return session.query(Member) \ .filter(Member.name == name) \ .first()
def get_location_by_shelving(shelving): """ Get the location data from database having a given shelving number :param shelving: the shelving number :return: the location data from database """ return session.query(Location) \ .filter(Location.shelving == shelving) \ .first()
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).\ filter(QuestionResponse.question_id == question.id).all() for task in tasks: grades = session.query(QuestionGrade).filter_by(grading_task_id=task.id).all() if not grades: users.add(task.grader) # send e-mails users = [session.query(User).get(u) for u in users] message = '''Dear %s,\n\n''' + request.form['message'].replace("%", "%%") + ''' Best, Stats 60 Staff ''' send_all(users, request.form['subject'], message) admins = session.query(User).filter_by(type="admin").all() send_all(admins, "%s Peer Assessment Reminder Sent" % homework.name, """Hey %s (and other staff), A reminder e-mail was just sent to the following users to remind them to complete their peer assessments.\n\n""" + "\n".join(u.stuid for u in users) + """ Sincerely, OHMS P.S. This is an automatically generated message ;-)""") return "Sent reminder to %d recipients. You should have received an e-mail." % len(users)
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 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 from_xml(node): """Constructs a Item object from an xml node""" klass_map = {'Multiple Choice': MultipleChoiceItem, 'Long Answer': LongAnswerItem, 'Short Answer': ShortAnswerItem} if node.attrib['type'] in klass_map: klass = klass_map[node.attrib['type']] else: raise ValueError('type of question not recognized, options are %s' % `klass_map.keys()`) # get existing item based on ID, if specified if 'id' in node.attrib: item = session.query(Item).get(node.attrib['id']) # otherwise create new item else: item = klass() session.add(item) item.sync_from_node(node) item.xml = ET.tostring(node, method="xml") session.flush() return item
def get_grade(stuid, hw_id): return session.query(Grade).filter_by(stuid=stuid).\ filter_by(hw_id=hw_id).first()
import db from base import session with open('yaks.txt', 'w') as fd: for yak in session.query(db.Yak).filter(db.Yak.score > 0).all(): fd.write(yak.text + '\n')
def get_users(): return session.query(User).all()
def get_user(stuid): return session.query(User).filter_by(stuid=stuid).one()
def get_homework(hw_id=None): if hw_id is None: return session.query(Homework).order_by(Homework.due_date).all() else: return session.query(Homework).get(hw_id)
def get_last_two_due_homeworks(): now = pdt_now() hws = session.query(Homework).order_by(Homework.due_date).all() due_hws = [hw for hw in hws if hw.due_date < now] return due_hws[-2:]
def get_peer_review_questions(): return session.query(PeerReview).filter(PeerReview.hw_id != None).all()
def get_sample_responses(question_id): return session.query(QuestionResponse).filter_by(sample=1).\ filter_by(question_id=question_id).\ order_by(QuestionResponse.id).all()
def get_question_responses(question_id, stuid): return session.query(QuestionResponse).\ filter_by(stuid=stuid).\ filter_by(question_id=question_id).\ order_by(QuestionResponse.time).all()
def get_question_response(question_response_id): return session.query(QuestionResponse).get(question_response_id)
def get_grading_task(grading_task_id): return session.query(GradingTask).get(grading_task_id)
def get_all_peer_tasks(question_id): return session.query(GradingTask).filter_by(question_id=question_id).\ filter(GradingTask.grader != GradingTask.student).all()
def get_self_tasks_for_student(question_id, stuid): return session.query(GradingTask).\ filter_by(grader=stuid).join(Question).\ filter(Question.id == question_id).\ filter(GradingTask.student == stuid).\ all()
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 get_homeworks_before(due_date=pdt_now()): return session.query(Homework).filter(Homework.due_date <= due_date).all()
def get_question(question_id): return session.query(Question).get(question_id)
def get_peer_tasks_for_student(question_id, stuid): return session.query(GradingTask).\ filter_by(student=stuid).join(Question).\ filter(Question.id == question_id).\ filter(GradingTask.grader != stuid).\ order_by(GradingTask.id).all()
import db import re from base import session from collections import Counter chars = re.compile('[^a-z0-9]') uniq = Counter() yaks = session.query(db.Yak).all() for yak in yaks: words = yak.text.split() for word in words: uniq[chars.sub('', word.lower())] += 1 for word, count in sorted(uniq.items(), key=lambda x: x[1]): print(word, count) print('UNIQ', len(uniq)) print('TOTAL', sum(uniq.values()))
def get_all_regular_questions(): return [q for q in session.query(Question).\ filter(Question.hw_id != None).\ all() if not isinstance(q, PeerReview)]