def login(): set_session() if request.method == 'POST': account = request.form.get('id', None) password = request.form.get('password', None) if not account or account == '': return skinned('/login.html', error=error.login('empty_id'), i=None, p=password) if not password or password == '': return skinned('/login.html', error=error.login('empty_pw'), i=account, p=None) res = func.login(account, password) if not res: return skinned('/login.html', error=error.login('failed'), i=account, p=password) else: session['logged'] = True session['account'] = account usr = func.get_user(account) session['username'] = usr['username'] session['skin'] = usr['skin'] session['admin'] = usr['admin'] logger.info("{} logged in site on {}".format( session['username'], ip())) return redirect(url_for('main')) return skinned('/login.html', error=None)
def test_change_role(client): # log out if need be logout(client) # change the role of an account (without priviledge) r = add_role(client, 'admintest', 0) # ensure failure due to requiring admin permission assert r.status_code == status.HTTP_401_UNAUTHORIZED notadmin = 'notadminaccount' # register a new account r = register(client, notadmin, "nonadmin", "*****@*****.**") assert r.status_code == status.HTTP_201_CREATED # account also fails for duplicate emails # log in admin account r = login(client, 'admintest', 'admintest') # extract the token and build new header with admin jwt headers = auth_header(r) # try to make admin account to be admin (should fail) r = add_role(client, 'admintest', UserAccountType.admin, headers) assert r.status_code == status.HTTP_417_EXPECTATION_FAILED # elevate new account to employee r = add_role(client, notadmin, UserAccountType.employee, headers) assert r.status_code == status.HTTP_200_OK # delete new account (should prob write handler) account = UserAccount.query.filter_by(username=notadmin).first() db.session.delete(account) db.session.commit()
def test_admin_addproduct(client): # log out if need be logout(client) # log in admin account r = login(client, 'admintest', 'admintest') # extract the token and build new header with admin jwt headers = auth_header(r) # now add the product r = add_product(client, "shampoo", "kiwi scent shampoo", 5, 9.99, headers) # product created successfully or already exist assert r.status_code == status.HTTP_201_CREATED or r.status_code == status.HTTP_409_CONFLICT
def test_admin_delproduct(client): # log out if need be logout(client) # log in admin account r = login(client, 'admintest', 'admintest') # extract the token and build new header with admin jwt headers = auth_header(r) # add a product add_product(client, "shampoo", "kiwi scent shampoo", 5, 9.99, headers) # delete the product r = del_product(client, "shampoo", headers) # should be able to assert r.status_code == status.HTTP_200_OK
def pass_login(self): self.status.setText('正在登录') operators = { '校园网不行': '', '中国移不动': '%40cmcc', '中国联不通': '%40unicom', '中国电不信': '%40telecom' } user = self.user.text() password = self.password.text() operator = operators[self.operator.currentText()] response = func.login(user, password, operator) self.status.setText(response) self.mk_conf(user, password, operator)
# Dependencies import config import func import requests # Session Setup s = requests.Session() # Login func.login(config.USERNAME, config.PASSWORD) # Get link wodlink = func.getLink() # Check if wodlink returns None if wodlink is None: print('Geen WOD link beschikbaar') raise SystemExit(0) # Check if already registered if func.isRegistered(wodlink): print('Je bent al aangemeld voor deze les.') raise SystemExit(0) # Check if too soon to register if func.tooSoon(wodlink): print('Te vroeg om aan te melden voor deze les.') raise SystemExit(0) # Register for desired class while not func.isRegistered(wodlink) or func.tooSoon(wodlink):
def curriculum(cookies: str = None): print(cookies) mainSession, userID, semesterName, semesterStartTime = login(cookies) req = mainSession.post(url=api.course, json={ "oddOrDouble": 0, "semester": semesterName, "startWeek": "1", "stopWeek": "22", "studentId": userID, "weeks": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22] }).json() print(mainSession.cookies.items()) print(req) tmpData = req['data'] reObject = re.compile(r'(\d*)-(\d*)( [单双])?') allCourseData = [] for timeSection in tmpData: for timeSectionCourse in timeSection['courseList']: allCourseData.append(timeSectionCourse) purgeAllCourseData = {} for day in range(1, 8): for time in range(1, 12): timeSign = (day, time) purgeAllCourseData[timeSign] = {} for courseData in allCourseData: day = fixDay(int(courseData['dayOfWeek'])) time = int(courseData['time']) timeSign = (day, time) purgeCourseDict = { 'courseName': courseData['courseName'], 'weeks': courseData['weeks'], 'teacher': courseData.get('teacherName', ''), 'studentNum': courseData['numberOfStudent'], 'classroomName': courseData.get('classroomName', ''), 'className': courseData.get('teachingClassName', '') } purgeAllCourseData.setdefault(timeSign, {}) purgeAllCourseData[timeSign][dictHash(purgeCourseDict)] = purgeCourseDict parsedCourseData = [] for timeSign in purgeAllCourseData.keys(): day = timeSign[0] time = timeSign[1] while True: if purgeAllCourseData[timeSign]: currentOptCourse = purgeAllCourseData[timeSign].popitem() courseHash = currentOptCourse[0] courseData = currentOptCourse[1] courseName = courseData['courseName'] teacher = courseData['teacher'] studentNum = courseData['studentNum'] className = courseData['className'] classroomName = courseData['classroomName'] rawWeeks = courseData['weeks'] try: parsedWeeks = re.match(reObject, rawWeeks).groups() interval = 2 if parsedWeeks[2] else 1 startWeek = parsedWeeks[0] endWeek = parsedWeeks[1] except AttributeError: parsedWeeks = int(rawWeeks) interval = 1 startWeek = parsedWeeks endWeek = parsedWeeks startTimePointer = copy.copy(time) endTimePointer = copy.copy(time) # print(courseName, str(parsedWeeks), rawWeeks) while True: if endTimePointer != 4 and endTimePointer != 11: if courseHash in purgeAllCourseData[(day, endTimePointer + 1)].keys(): purgeAllCourseData[(day, endTimePointer + 1)].pop(courseHash) endTimePointer += 1 else: break else: break parsedOneCourse = { 'day': day, 'courseName': courseName, 'className': className, 'classroomName': classroomName, 'startTimeID': startTimePointer, 'endTimeID': endTimePointer, 'teacherName': teacher, 'startWeek': startWeek, 'endWeek': endWeek, 'interval': interval, 'studentNumber': studentNum } oneCourseHash = dictHash(parsedOneCourse) print(oneCourseHash, parsedOneCourse) if oneCourseHash in fix_dict.keys(): fix_object = fix_dict[oneCourseHash] for key, value in fix_object.items(): parsedOneCourse[key] = value print("fixed:", parsedOneCourse, file=sys.stderr) parsedOneCourse['startTime'] = courseTimeDict[parsedOneCourse['startTimeID']] parsedOneCourse['endTime'] = courseTimeDict[parsedOneCourse['endTimeID']] + COURSE_TIME parsedCourseData.append(copy.deepcopy(parsedOneCourse)) parsedOneCourse.clear() else: break calt = getIcal(f'{semesterName} 课表') for oneEvent in parsedCourseData: count = int((int(oneEvent['endWeek']) - int(oneEvent['startWeek'])) / int(oneEvent['interval']) + 1) dtstart_datetime = semesterStartTime + (int(oneEvent['startWeek']) - 1) * ONE_WEEK + ( int(oneEvent['day']) - 1) * ONE_DAY + oneEvent['startTime'] dtend_datetime = semesterStartTime + (int(oneEvent['startWeek']) - 1) * ONE_WEEK + ( int(oneEvent['day']) - 1) * ONE_DAY + oneEvent['endTime'] event = icalendar.Event() event.add('summary', oneEvent['courseName']) # 标题 event.add('uid', str(uuid1()) + '@JGSU') # UUID event.add('dtstamp', datetime.now()) # 创建时间 if oneEvent['classroomName']: event.add('location', oneEvent['classroomName']) # 地点 event.add('description', '{}教师: {}\r\n' '{}时间: {} - {} \r\n' '周期: {}\r\n' '班级: {}\r\n' '学生数: {}'.format( f"第 {oneEvent['startTimeID']} - {oneEvent['endTimeID']} 节\r\n" if oneEvent['startTimeID'] != oneEvent['endTimeID'] else f"第 {oneEvent['startTimeID']} 节\r\n", oneEvent['teacherName'], f'教室: {oneEvent["classroomName"]}\r\n' if oneEvent["classroomName"] else '', str(oneEvent['startTime']), str(oneEvent['endTime']), f'{oneEvent["startWeek"]} - {oneEvent["endWeek"]}' if oneEvent["startWeek"] != oneEvent["endWeek"] else f'{oneEvent["startWeek"]}', oneEvent['className'], oneEvent['studentNumber'] ) ) event.add('dtstart', dtstart_datetime) event.add('dtend', dtend_datetime) if count != 1: event.add('rrule', {'freq': 'weekly', 'interval': oneEvent['interval'], 'count': count}) calt.add_component(event) with open(f'data/{userID}.{semesterName}.curriculum.ics', 'wb') as f: f.write(calt.to_ical()) return f'https://ical.learningman.top/{userID}/{semesterName}/curriculum.ics'