def upload(aid): file = request.files['inputFile'] file_id = int( str(aid) + current_user.id) # Use assignment id and user id to specify a file dbs = DatabaseOperations() already_submit = dbs.query_already_upload(int(current_user.id), aid) # check if document already been submitted if already_submit: exist_id = int(already_submit[0]) dbs.delete_file(exist_id) # old_submission = db.session.query(FileContents).filter_by(file_id=exist_id) # db.session.delete(old_submission) # db.session.commit() new_file = FileContents(file_id=exist_id, doc_name=file.filename, data=file.read()) db.session.add(new_file) db.session.commit() assignment_num = str(aid) return redirect('/assignment_detail/' + assignment_num) else: new_file = FileContents(file_id=file_id, doc_name=file.filename, data=file.read()) db.session.add(new_file) # 是不是要放一个界面更好一些 new_submit = Submit(file_id=file_id, id=current_user.id, file_grade=0) db.session.add(new_submit) new_submission = Submission(file_id=file_id, a_id=aid) db.session.add(new_submission) db.session.commit() assignment_num = str(aid) return redirect('/assignment_detail/' + assignment_num)
def login(): form = LoginForm(request.form) db = DatabaseOperations() if request.method == 'POST' and form.validate(): try: user_id = int(form.username.data) password = db.query_password(user_id)[0] except Exception: message = "Invalid input" # 如果输入不合规范 return render_template('login/login.html', message=message) if form.password.data == password: # 判断不同职位进入不同网页 authority1 = int(db.query_authority(user_id)[0]) if authority1 == 1: # 管理员 admin_user = User(user_id) login_user(admin_user) return redirect('/homepage_a') if authority1 == 0: # 普通用户 student_user = User(user_id) login_user(student_user) return redirect('/homepage') else: message = "Wrong password" # 真正的密码是啥 return render_template('login/login.html', message=message) else: message = "Welcome" # 乱来的 return render_template('login/login.html', message=message)
def attendance_new_one_year(): data = DatabaseOperations() members = data.query_attendance_one_year() curr_status = "primary" status_in_str = "Unchecked" return render_template('./attendance/attendance_new.html', members=members, status_colour=curr_status, current_status=status_in_str)
def grading_detail(aid): dbs = DatabaseOperations() assignment = dbs.query_assignment_information(aid) number = dbs.query_assignment_number(aid) homeworks = dbs.query_homework_information(aid) aid = aid * 10000000000 return render_template('./grading/grading_detail.html', a_id=aid, assignment=assignment, number=number, homeworks=homeworks)
def assignment_detail(aid): dbs = DatabaseOperations() detail = dbs.query_assignment_detail(int(current_user.id), aid) submission = dbs.query_assignment_submission(aid, int(current_user.id)) grading = dbs.query_assignment_grading(aid, int(current_user.id)) # 需要assignment_title detail submission status grading status duedate time remaining return render_template('.' '/assignment/assignment_detail.html', assignment=detail, aid=aid, submission=submission, grading=grading)
def detail_records_admin(): db_sql = DatabaseOperations() userid = int(current_user.id) name = db_sql.query_name(int(userid))[0] page = request.values.get('page', 1, type=int) winning_rate = 0 mvp_rate = 0 # Query match record from database and do pagination records = db.session.query(Amatch, HasMatch, MatchRecord, UserTB).join \ (HasMatch, and_(Amatch.match_id == HasMatch.match_id)).join(MatchRecord, and_ \ (HasMatch.r_id == MatchRecord.r_id)).join(UserTB, and_(HasMatch.id == UserTB.id)). \ filter(UserTB.id.like('%' + str(userid) + '%')).order_by(Amatch.date.desc()) win_num = db.session.query(Amatch, HasMatch, MatchRecord, UserTB).join(HasMatch, and_ \ (Amatch.match_id == HasMatch.match_id)).join(MatchRecord, and_(HasMatch.r_id == MatchRecord.r_id)). \ join(UserTB, and_(HasMatch.id == UserTB.id)).filter(UserTB.id.like('%' + str(userid) + '%'), Amatch.win == 1).count() mvp_num = db.session.query(Amatch, HasMatch, MatchRecord, UserTB).join(HasMatch, and_ \ (Amatch.match_id == HasMatch.match_id)).join(MatchRecord, and_(HasMatch.r_id == MatchRecord.r_id)). \ join(UserTB, and_(HasMatch.id == UserTB.id)).filter(UserTB.id.like('%' + str(userid) + '%'), MatchRecord.is_mvp == 1).count() record_num = records.count() if winning_rate != 0: winning_rate = request.values.get('winning_rate') else: winning_rate = format(win_num / record_num, '.2%') if mvp_rate != 0: mvp_rate = request.values.get('mvp_rate') else: mvp_rate = format(mvp_num / record_num, '.2%') pagination = records.paginate(page=page, per_page=5, error_out=False) results = pagination.items return render_template('homepage/detail_records_admin.html', pagination=pagination, results=results, member_id=userid, name=name, mvp_rate=mvp_rate, winning_rate=winning_rate, record_num=record_num)
def assignment_new(): # 数据库判断权限 form = AssignmentForm(request.form) # message = form.username.data if request.method == 'POST': # 将form内的各种信息写到数据库内 并返回至list界面 title = request.values.get("assName") detail = request.values.get("assReq") dueDate = request.values.get("assDueDate") dueTime = request.values.get("assDueTime") dbs = DatabaseOperations() dbs.insert_new_assignment(int(current_user.id), title, detail, dueDate, dueTime) return redirect("/grading") # return render_template('assignment/assignment_create.html',title=title,detail=detail,dueDate=dueDate,dueTime=dueTime) else: return render_template('assignment/assignment_create.html')
def mk_dir(self, make_dir): mkdir_path = os.path.abspath('.') + '/' + make_dir print(mkdir_path) if not os.path.exists(mkdir_path): os.mkdir(mkdir_path) DatabaseOperations().mk_table(make_dir) else: print('path already scraped!!!')
def change_pwd_admin(): # form = ChangepwdForm(request.form) dbs = DatabaseOperations() userid = int(current_user.id) name = dbs.query_name(int(userid))[0] if not int(dbs.query_authority(int(current_user.id))[0]): return redirect("/authority") if request.method == 'POST': try: password = dbs.query_password(int(current_user.id))[0] except Exception: message = "Invalid format" # 如果输入不合规范 return render_template('homepage/change_pwd_admin.html', message=message, name=name) # 白给成功允许修改 if request.values.get("origin_pwd") == password: dbs.update_password(int(current_user.id), request.values.get("new_pwd")) return redirect('/') else: message = "Wrong password" return render_template('homepage/change_pwd_admin.html', message=request.values.get("new_pwd"), name=name) else: message = "" return render_template('homepage/change_pwd_admin.html', message=request.values.get("new_pwd"), name=name)
def initial_setup(self, dns='', path='base'): self.make = path + '_' + dns '''CREATE DIRECTORY FOR IMAGES.''' FileOperations().mk_dir(self.make) '''CREATE TABLE TO STORE LINKS.''' DatabaseOperations().mk_table(self.make) '''Insert ROOT link.''' if path == 'base': href = self.base_url1 child = Utility().hash(self.base_url1) elif path == 'relative': href = self.url child = Utility().hash(self.url) parent, scraped_status = 1, False DatabaseOperations().insert_into(table_name=self.make, to_insert=[(child, href, parent, scraped_status)])
def attendance_search(): # 数据库判断权限 dbs = DatabaseOperations() # message = form.username.data if request.method == 'POST': year = request.values.get("year") month = request.values.get("month") day = request.values.get("day") # if it post date = year + "-" + month + "-" + day persons = dbs.query_attendance_date(date) # form.year form.month.form.day 确认时间并查询语句 # 判断不同职位进入不同 # test_persons = [(1, "吴飞机","今天白给"), (2, "白给大侠","鬼知道去哪里了"), (3,"真的好累阿","约会去了"), # (4,"要命命","一个人的旅行")] return render_template('attendance/attendance_search.html', people=persons) else: return render_template('attendance/attendance_search.html')
def change_notice(): dbs = DatabaseOperations() userid = int(current_user.id) name = dbs.query_name(int(userid))[0] if request.method == 'POST': list1 = request.values.get("board").split("\r\n") dbs.drop_notice() dbs.creat_notice() for i in list1: dbs.insert_notice(i) # return render_template('homepage/homepage_admin.html', message=list1) return redirect('/homepage_a') else: # events = dbs.query_notice() # string = "" # for i in events: # string = string +str(i) +"\r\n" return render_template('homepage/change_notice.html', name=name)
def spider(self): '''--GET HREFs FROM TABLE WHICH HAS SCRAPED_STATUS AS ZERO, AND SCRAPED THEM!!!--''' unscraped_links = DatabaseOperations().select_from(self.make) do = DatabaseOperations() for href in unscraped_links: links_list = [] raw_data = requests.get(href[1]) souped_data = BeautifulSoup(raw_data.text, features="html.parser") anchors = souped_data.findAll('a') for i in range(len(anchors)): try: links_list.append(anchors[i]['href']) except: pass links_list = Utility(url=self.url, url_parts=self.url_parts, base_url1=self.base_url1, base_url2=self.base_url2).reformat_urls(list_=links_list, parent=href[0]) import pdb; pdb.set_trace() do.insert_into(table_name=' '+self.make+' ', to_insert=links_list, update=True, set_clause=' set scraped_status=true ', where=' where child= ' +str(Utility().hash(href[1]))+' ') if (do.select_count(table_name=self.make, where=' where scraped_status=false ')) and (do.select_count(table_name=self.make) < 7800): self.spider()
def information_a(user_id): dbs = DatabaseOperations() name = dbs.query_name(int(user_id))[0] # 检索数据库取得公告板信息赋值给event, # 从数据库中调record # 数据库拿到其他信息 records = dbs.query_match_info(int(user_id)) try: firwin = int(dbs.query_position_times(user_id, 1)) secwin = int(dbs.query_position_times(user_id, 2)) thiwin = int(dbs.query_position_times(user_id, 3)) forwin = int(dbs.query_position_times(user_id, 3)) firrate = str(firwin * 100 / (firwin + secwin + thiwin + forwin)) secrate = str(secwin * 100 / (firwin + secwin + thiwin + forwin)) thirate = str(thiwin * 100 / (firwin + secwin + thiwin + forwin)) forrate = str(forwin * 100 / (firwin + secwin + thiwin + forwin)) except Exception: firrate = 0 secrate = 0 thirate = 0 forrate = 0 skilled = "none" temp = max(firwin, secwin, thiwin, forwin) if firwin == temp: skilled = "First" if secwin == temp: skilled = "Second" if thiwin == temp: skilled = "third" if forwin == temp: skilled = "forth" return render_template('search/information_admin.html', name=name, firrate=firrate, secrate=secrate, thirate=thirate, forrate=forrate, skilled=skilled, records=records)
def initialize_database(): database = DatabaseOperations() database.create_tables() database.init_db() return "Database initialized!"
def homepage_a(): userid = int(current_user.id) db_sql = DatabaseOperations() if not int(db_sql.query_authority(int(current_user.id))[0]): return redirect("/authority") name = db_sql.query_name(int(userid))[0] page = request.values.get('page', 1, type=int) # 检索数据库取得公告板信息赋值给event, # events = ("明天秃头不要迟到", "ballball you 要了老命了", "画饼一时爽一直画饼一直爽") # 四个count数不同的数字 pres excu late abse events = db_sql.query_notice() try: pre = int(db_sql.query_attendance(userid, 0)[0]) exc = int(db_sql.query_attendance(userid, 1)[0]) lat = int(db_sql.query_attendance(userid, 2)[0]) abs = int(db_sql.query_attendance(userid, 3)[0]) prerate = str(pre * 100 / (pre + exc + lat + abs)) excrate = str(exc * 100 / (pre + exc + lat + abs)) latrate = str(lat * 100 / (pre + exc + lat + abs)) absrate = str(abs * 100 / (pre + exc + lat + abs)) except Exception: pre = 0 exc = 0 lat = 0 abs = 0 prerate = 0 excrate = 0 latrate = 0 absrate = 0 try: firwin = int(db_sql.query_position_times(userid, 1)) secwin = int(db_sql.query_position_times(userid, 2)) thiwin = int(db_sql.query_position_times(userid, 3)) forwin = int(db_sql.query_position_times(userid, 4)) firrate = str(firwin * 100 / (firwin + secwin + thiwin + forwin)) secrate = str(secwin * 100 / (firwin + secwin + thiwin + forwin)) thirate = str(thiwin * 100 / (firwin + secwin + thiwin + forwin)) forrate = str(forwin * 100 / (firwin + secwin + thiwin + forwin)) except Exception: firrate = 0 secrate = 0 thirate = 0 forrate = 0 skilled = "none" temp = max(firwin, secwin, thiwin, forwin) if firwin == temp: skilled = "First" elif secwin == temp: skilled = "Second" elif thiwin == temp: skilled = "third" elif forwin == temp: skilled = "forth" else: skilled = "" # Query match record from database and do pagination records = db.session.query(Amatch, HasMatch, MatchRecord, UserTB).join\ (HasMatch, and_(Amatch.match_id == HasMatch.match_id)).join(MatchRecord, and_\ (HasMatch.r_id == MatchRecord.r_id)).join(UserTB, and_(HasMatch.id == UserTB.id)).\ filter(UserTB.id.like('%' + str(userid) + '%')).order_by(Amatch.date.desc()) pagination = records.paginate(page=page, per_page=5, error_out=False) results = pagination.items return render_template('homepage/homepage_admin.html', name=name, events=events, results=results, pre=pre, exc=exc, lat=lat, abs=abs, prerate=prerate, pagination=pagination, excrate=excrate, latrate=latrate, absrate=absrate, records=records, member_id=userid, firrate=firrate, secrate=secrate, thirate=thirate, forrate=forrate, skilled=skilled)
def match_create(): if request.method == 'POST': matchName = request.values.get("matchName") matchDate = request.values.get("matchDate") matchOppo = request.values.get("matchOppo") firstPosition = int(request.values.get("firstPosition")) secondPosition = int(request.values.get("secondPosition")) thirdPosition = int(request.values.get("thirdPosition")) fourthPosition = int(request.values.get("fourthPosition")) side = request.values.get("side") mvp = int(request.values.get("mvp")) result = request.values.get("result") dbs = DatabaseOperations() dbs.insert_match_record(matchName, matchDate, matchOppo, int(side), int(result)) matchID = int( dbs.query_match_id(matchName, matchDate, matchOppo, int(side), int(result))[0]) #insert to take_part and record and has #insert into record return r_id r_id1 = int(dbs.insert_record(1, mvp == 1)[0]) r_id2 = int(dbs.insert_record(2, mvp == 2)[0]) r_id3 = int(dbs.insert_record(3, mvp == 3)[0]) r_id4 = int(dbs.insert_record(4, mvp == 4)[0]) dbs.insert_take_part(matchID, r_id1) dbs.insert_take_part(matchID, r_id2) dbs.insert_take_part(matchID, r_id3) dbs.insert_take_part(matchID, r_id4) dbs.insert_has(firstPosition, r_id1) dbs.insert_has(secondPosition, r_id2) dbs.insert_has(thirdPosition, r_id3) dbs.insert_has(fourthPosition, r_id4) return render_template('match/match_record.html', matchName=matchName, matchDate=matchDate, side=side, result=result, matchOppo=matchOppo) else: return render_template('match/match_record.html')
def match_del(mid): dbs = DatabaseOperations() if mid != 0: r_id = dbs.query_r_id(mid) # return render_template('match/match_del.html',matchs = r_id,r_id = r_id) for i in r_id: dbs.delete_record(i[0]) dbs.delete_has(i[0]) dbs.delete_take_part(i[0]) dbs.delete_match(mid) return redirect('/match_del/0') if request.method == 'POST': dbs = DatabaseOperations() matchDate = request.values.get("date") matchs = dbs.query_match_del(matchDate) return render_template('match/match_del.html', matchs=matchs) return render_template('match/match_del.html')
def grade_action(): db_sql = DatabaseOperations() grade = request.form['grade'] file_id = request.form['file_id'] db_sql.update_grade(file_id, grade) return jsonify({'grade': grade})
def attendance_all(): data = DatabaseOperations() members = data.query_attendance_all() return render_template('./attendance/attendance_new.html', members=members)