def saveTopic(): name = request.args.get('name') image = request.args.get('image') t = Topic(name, image) db_session.add(t) db_session.commit() return jsonify({"success" : "true"}), 200, {'Access-Control-Allow-Origin':'*'}
def create_users(self): for u in range(self.num_students): first_name = random.choice(first_names) last_name = random.choice(last_names) username = '******'.format(first_name[0], last_name[1:], random.randint(1, 1000)).lower() db_user = User(first_name=first_name.capitalize(), last_name=last_name.capitalize(), username=username, password_hash=hashlib.sha256(username.encode()).hexdigest(), role='student') db_session.add(db_user) db_session.flush() self.student_ids.append(db_user.id) for u in range(self.num_instructors): first_name = random.choice(first_names) last_name = random.choice(last_names) username = '******'.format(first_name[0], last_name[1:], random.randint(1, 1000)).lower() db_user = User(first_name=first_name.capitalize(), last_name=last_name.capitalize(), username=username, password_hash=hashlib.sha256(username.encode()).hexdigest(), role='TA') db_session.add(db_user) db_session.flush() self.instructor_ids.append(db_user.id) db_session.commit()
def create_student_demand(self): student_schedule = {} for student_id in self.student_ids: student_schedule = [] for semester_id in self.semesters.keys(): if len(student_schedule) >= 11: continue for _ in range(random.choice([1, 1, 1, 2])): course_id = None while(True): proposed_course = random.choice(list(self.courses.keys())) if proposed_course in student_schedule: continue if proposed_course in self.prereq_reversed.keys() and self.prereq_reversed[proposed_course] not in student_schedule: continue course_id = proposed_course break db_demand = StudentDemand(student_id=student_id, course_id=course_id, semester_id=semester_id, is_current=True) db_session.add(db_demand) db_session.flush() student_schedule.append(course_id) db_session.commit()
def create_student_records(self): for s in self.student_ids: db_record = StudentRecord(user_id=s, seniority=random.randint(1, 7), current_gpa=float(random.randint(1, 8)/8)) db_session.add(db_record) db_session.commit()
def saveUserTopic(userId=None, topicId=None): user = User.query.filter_by(id=userId).first() topic = Topic.query.filter_by(id=userId).first() userTopic = UserTopic(userId, topicId); db_session.add(userTopic) db_session.commit() return jsonify({"success" : "true"}), 200, {'Access-Control-Allow-Origin':'*'}
def create_admin_user(self): admin_user = User(first_name='Awesome', last_name='Administrator', username='******', password_hash=hashlib.sha256('aadmin3'.lower().encode()).hexdigest(), role='administrator') db_session.add(admin_user) db_session.commit()
def create_instructor_pool(self): for _ in range(self.num_instructors * 2): db_ipool = InstructorPool(user_id=random.choice(self.instructor_ids), course_id=random.choice(list(self.courses.keys())), semester_id=random.choice(list(self.semesters.keys())), instructor_role='TA') db_session.add(db_ipool) db_session.commit()
def create_demand(): j = request.get_json() if not StudentDemand.query.filter_by(student_id=g.user_id, semester_id=j['semester_id'], course_id=j['course_id']).first(): db_session.add(StudentDemand(student_id=g.user_id, semester_id=j['semester_id'], course_id=j['course_id'])) db_session.commit() return get_user_demand()
def saveFeedTopic(feedId=None, topicId=None): feedTopic = FeedTopic(feedId, topicId); topic = Topic.query.filter_by(id=topicId).first() feed = Feed.query.filter_by(id=feedId).first() topic.image = feed.image db_session.add(topic) db_session.commit() db_session.add(feedTopic) db_session.commit() return jsonify({"success" : "true"}), 200, {'Access-Control-Allow-Origin':'*'}
def create_student_records(self): with open('optimizer/resources/small/student_records_10.csv', 'rU') as record_file: record_reader = csv.DictReader(record_file) for r in record_reader: db_record = StudentRecord(user_id=int(r['user_ID']), seniority=int(r['seniority']), current_gpa=float(r['GPA'])) db_session.add(db_record) db_session.commit()
def create_demand(): j = request.get_json() if not InstructorPool.query.filter_by(user_id=j['instructor_id'], semester_id=j['semester_id'], course_id=j['course_id']).first(): db_session.add(InstructorPool(user_id=j['instructor_id'], semester_id=j['semester_id'], course_id=j['course_id'])) db_session.commit() return get_instructor_pool()
def create_instructor_pool(self): with open('optimizer/resources/small/instructor_pool_10.csv', 'rU') as ipool_file: ipool_reader = csv.DictReader(ipool_file) for p in ipool_reader: db_ipool = InstructorPool(user_id=p['faculty_ID'], course_id=p['course_ID'], semester_id=p['semester_ID'], instructor_role=user_roles[p['role_ID']]) db_session.add(db_ipool) db_session.commit()
def create_prerequisites(self): with open('optimizer/resources/static/course_dependencies.csv', 'rU') as prereq_file: prereq_reader = csv.DictReader(prereq_file) for p in prereq_reader: db_prereq = CourseDependency(first_course=p['prereq_course_ID'], second_course=p['dependent_course_ID']) db_session.add(db_prereq) db_session.flush() self.prerequisites_reversed[db_prereq.second_course] = db_prereq.first_course db_session.commit()
def create_student_demand(self): with open('optimizer/resources/small/student_demand_10.csv', 'rU') as demand_file: demand_reader = csv.DictReader(demand_file) for d in demand_reader: db_demand = StudentDemand(student_id=int(d['student_ID']), course_id=int(d['course_ID']), semester_id=int(d['semester_ID']), is_current=True) db_session.add(db_demand) db_session.commit()
def create_users(self): with open('optimizer/resources/small/users_10.csv', 'rU') as user_file: user_reader = csv.DictReader(user_file) for u in user_reader: db_user = User(first_name=u['first_name'].capitalize(), last_name=u['last_name'].capitalize(), username=u['username'].lower(), password_hash=hashlib.sha256(u['username'].lower().encode()).hexdigest(), role=user_roles[u['role_ID']]) db_session.add(db_user) db_session.commit()
def create_semesters(self): with open('optimizer/resources/static/semesters.csv', 'rU') as semester_file: semester_reader = csv.DictReader(semester_file) for s in semester_reader: db_semester = Semester(name=s['semester_name'], start_date=s['start_date'], end_date=s['end_date']) db_session.add(db_semester) db_session.flush() self.semesters[db_semester.id] = db_semester.name db_session.commit()
def create_demand(): j = request.get_json() if not InstructorPool.query.filter_by(user_id=j['instructor_id'], semester_id=j['semester_id'], course_id=j['course_id']).first(): db_session.add( InstructorPool(user_id=j['instructor_id'], semester_id=j['semester_id'], course_id=j['course_id'])) db_session.commit() return get_instructor_pool()
def create_demand(): j = request.get_json() if not StudentDemand.query.filter_by(student_id=g.user_id, semester_id=j['semester_id'], course_id=j['course_id']).first(): db_session.add( StudentDemand(student_id=g.user_id, semester_id=j['semester_id'], course_id=j['course_id'])) db_session.commit() return get_user_demand()
def create_demand(): j = request.get_json() StudentDemand.query.filter(StudentDemand.student_id == g.user_id).update({StudentDemand.is_current: False}) for s in j['courses']: db_session.add(StudentDemand(student_id=g.user_id, semester_id=s['semester_id'], course_id=s['course_id'], is_current=True)) db_session.commit() return get_user_demand()
def index(): # We can use "current_app" to have access to our "app" object testFeed = Feed("testing") db_session.add(testFeed) db_session.commit() testEntry = Entry("test title", "image", "body", "link", None, "description", testFeed.id) db_session.add(testEntry) db_session.commit() #testFeedEntry = FeedEntry(testFeed.id, testEntry.id) #db_session.add(testFeedEntry) testTopic = Topic("test topic", "image") db_session.add(testTopic) testUser = User(name="test User", email="*****@*****.**") db_session.add(testUser) db_session.commit() feeds = Feed.query.all() users = User.query.all() topics = Topic.query.all() return jsonify( feeds = [i.serialize for i in feeds], users = [i.serialize for i in users], topics = [i.serialize for i in topics] ), 200, {'Access-Control-Allow-Origin':'*'}
def create_user(): j = request.get_json() u = User(first_name=j['first_name'], last_name=j['last_name'], username=j['username'], role=j['role']) # Hash password # This is not a secure way at all to do this... u.password_hash = hashlib.sha256(j['password'].encode()).hexdigest() db_session.add(u) db_session.commit() return jsonify(UserSchema().dump(u).data)
def saveFeed(): url = request.args.get('url') topic = request.args.get('topic') feed = parseXml(url) #x = feedparser.parse(url) entries = Entry.query.filter_by(feed_id=feed.id) if feed.image is None: feed.image = entries[0].image db_session.add(feed) db_session.commit() return jsonify( feed = [feed.serialize], entries = [i.serialize for i in entries] ), 200, {'Access-Control-Allow-Origin':'*'}
def create_courses(self): with open('optimizer/resources/static/courses.csv', 'rU') as course_file: course_reader = csv.DictReader(course_file) for c in course_reader: db_course = Course(course_name=c['course_name'], course_number=c['course_number'], is_fall=(c['fall_term'] is '1'), is_spring=(c['spring_term'] is '1'), is_summer=(c['summer_term'] is '1'), availability_string=c['availability']) db_session.add(db_course) db_session.flush() self.courses[db_course.id] = dict(is_fall=db_course.is_fall, is_spring=db_course.is_spring, is_summer=db_course.is_summer) db_session.commit()
def solve(): r = requests.post('http://ec2-54-86-66-135.compute-1.amazonaws.com:8080/solve', json=get_solver_context()) if r.status_code != 200 or 'error' in r.json(): return jsonify(error=True) # Create this run ID o = OptimizationRun(created_by=g.user_id) db_session.add(o) db_session.flush() # Configure everything data = r.json() for a in data['assignments']: s = StudentAssignment(student_id=a['student'], semester_id=a['semester'], course_id=a['course'], run_id=o.id) db_session.add(s) for a in data['instructors']: s = InstructorAssignment(instructor_id=a['instructor_id'], semester_id=a['semester_id'], course_id=a['course_id'], run_id=o.id) db_session.add(s) db_session.commit() return jsonify(updated=True, run_id=o.id)
def solve(): r = requests.post( 'http://ec2-54-86-66-135.compute-1.amazonaws.com:8080/solve', json=get_solver_context()) if r.status_code != 200 or 'error' in r.json(): return jsonify(error=True) # Create this run ID o = OptimizationRun(created_by=g.user_id) db_session.add(o) db_session.flush() # Configure everything data = r.json() for a in data['assignments']: s = StudentAssignment(student_id=a['student'], semester_id=a['semester'], course_id=a['course'], run_id=o.id) db_session.add(s) for a in data['instructors']: s = InstructorAssignment(instructor_id=a['instructor_id'], semester_id=a['semester_id'], course_id=a['course_id'], run_id=o.id) db_session.add(s) db_session.commit() return jsonify(updated=True, run_id=o.id)
def parseXml(url): x = feedparser.parse(str(url)) length = len(x['entries']) try: title = x.feed.title except AttributeError: title = None category = None try: image = x.feed.image.url except AttributeError: image = None try: link = x.feed.link except AttributeError: link = None try: description = x.feed.description except AttributeError: description = None f = Feed(title, category, image, link, description) db_session.add(f) db_session.commit() for entry in x.entries: try: etitle = entry.title except AttributeError: etitle = None eimage = None try: eimage = entry.image except AttributeError: try: if len(entry.enclosures) != 0: eimage = entry.enclosures[0].url except AttributeError: try: if len(entry.media_content) != 0: eimage = entry.media_content[0]['url'] except AttributeError: if "<img" in entry.description: d = feedparser.parse(entry.description) eimage = d.feed.img['src'] ebody = None try: elink = entry.link except AttributeError: elink = None try: #edate = DateTime.fromtimestamp(mktime(entry.published)) edate = entry.published except AttributeError: edate = None try: edescription = entry.description except AttributeError: edescription = None efid = f.id e = Entry(etitle, eimage, ebody, elink, edate, edescription, efid) db_session.add(e) db_session.commit() return f