def user_register(): username = request.form['username'] password = request.form['password'] user = User.getUser({'username': username}) if user is None: user = User(username=username, password=password) user.addUser() session['user'] = username flash('Registration successful', 'info') return redirect(url_for('wh')) else: flash('Registration unsuccessful.Credentials Already Present', 'error') return redirect(url_for('newindex'))
def post(self): url = self.request.uri route = url.split('/') status_int = 200 if 'contact' in route: data = self.request.get('data') json_data = json.loads(data) logging.info(data) Contact.contact_id = '' Contact.names = json_data['names'] Contact.cell = json_data['cell'] Contact.email = json_data['email'] Contact.subject = json_data['subject'] Contact.message = json_data['message'] Contact.put() response_data = Contact.to_dict() elif 'media' in route: uid = route[len(route) - 1] response_data = '' if 'album' in route: album_json = json.loads(self.request.body) album_instance = Album() album = album_instance.addAlbum(album=album_json) response_data = album.to_dict() else: status_int = 500 response_data = {'message' : 'Error request not understood'} elif 'user' in route: json_data = json.loads(self.request.body) logging.info('User Data') logging.info(json_data) this_user = User() this_user = this_user.addUser(json_user=json_data) if this_user != '': response_data = this_user.to_dict() else: status_int = 403 response_data = {'message': 'user not found'} else: status_int = 500 response_data = {'message' : 'Error request not understood'} self.response.headers['Content-Type'] = "application/json" self.response.status_int = status_int json_data = json.dumps(response_data) self.response.write(json_data)
def addUser(): name = request.form.get('name') password = request.form.get('password') tel = request.form.get('tel') data = { "status": "", "data": { "uid": "", "name": "", "password": "", "tel": "" } } u = User.addUser({"name": name, "password": password, "tel": tel}) data['data']['uid'] = u.uid data['data']['name'] = u.name data['data']['password'] = u.password data['data']['tel'] = u.tel data['status'] = True return json.dumps(data)
def getStudentList(username, password): url = "http://jwxt.ecjtu.jx.cn/infoQuery/class_findClassList.action" cookie = login_jwxt(username, password) html = cas.page_by_get(cookie, headers, url) soup = BeautifulSoup(html, "lxml") # 获取学院 departments = soup.find_all( "select", {"name": "depInfo.departMent"})[0].find_all("option") departmentList = [] for department in departments: departmentList.append(department["value"]) # 获取年级 grades = soup.find_all("select", {"name": "gra.grade"})[0].find_all("option") gradeList = [] for grade in grades: if (int(grade["value"]) >= 2015): gradeList.append(grade["value"]) # 获取班级 for dep in departmentList: for gra in gradeList: payload = { "depInfo.departMent": dep, "gra.grade": gra, "classInfo.className": "selectClass" } html = cas.page_by_post( cookie, headers, "http://jwxt.ecjtu.jx.cn/infoQuery/class_findClaByDepGra.action", payload) name_patt = re.compile(r'<option.+?>(.+?)</option>') class_names = name_patt.findall(html)[1:] value_patt = re.compile(r'<option\svalue=\'(.+?)\'>.+?</option>') class_value = value_patt.findall(html)[1:] class_dic = dict(zip(class_names, class_value)) for key in class_dic: payload = { "depInfo.departMent": dep, "gra.grade": gra, "classInfo.classID": class_dic[key] } html = cas.page_by_post( cookie, headers, "http://jwxt.ecjtu.jx.cn/infoQuery/class_findStuNames.action", payload) soup = BeautifulSoup(html, "lxml") tr = soup.find_all("tr", class_="classNameDis") for i in tr: td = i.find_all("td") info = { 'department': dep, 'grade': gra, 'major': key, 'name': td[1].text, 'sex': td[2].text, 'class_id': td[3].text, 'student_id': td[4].text, 'student_status': td[5].text } User.addUser(info) return
def post(self): url = self.request.uri route = url.split('/') status_int = 200 if 'contact' in route: json_data = json.loads(self.request.body) logging.info(json_data) this_contact = Contact() contact_key = this_contact.new_contact(contact=json_data) if contact_key != None: response_data = {'message': 'Thank you ' + str(json_data['names']) + ' for sending us this message we will be responding to you through this email ' + str(json_data['email']) } else: response_data = {'message' : 'Unfortunately we cannot process any more contact messages from you'} elif 'categories' in route: json_data = json.loads(self.request.body) this_category = Categories(); this_category = this_category.addCategory(category=json_data) if this_category == '': status_int = 403 response_data={'message':'error category already exist'} else: response_data = this_category.to_dict() elif 'products' in route: json_data = json.loads(self.request.body) this_products = Products() this_products = this_products.addProduct(product=json_data) logging.info(this_products) if this_products != None: response_data = {'message': 'product successfully added'} else: response_data = {'message': 'duplicate product error'} elif 'services' in route: json_data = json.loads(self.request.body) this_service = Services() this_service = this_service.addService(service=json_data) if this_service != '': response_data = this_service.to_dict() else: status_int = 403 response_data = { 'message':'duplicate service'} elif 'physical-address' in route: json_data = json.loads(self.request.body) physical_address = PhysicalAddress() physical_address = physical_address.addPhysicalAddress(physical=json_data) response_data = physical_address.to_dict() elif 'contact-details' in route: json_data = json.loads(self.request.body) contact_details = ContactDetails() contact_details = contact_details.addContactDetails(contact=json_data) response_data = contact_details.to_dict() elif 'cart' in route: json_data = json.loads(self.request.body) cart_owner_id = json_data['uid'] item = json_data['item'] logging.info(item) cart_request = Cart.query(Cart.uid == cart_owner_id) cart_list = cart_request.fetch() if len(cart_list) > 0 : cart = cart_list[0] else: cart = Cart() cart.uid = cart_owner_id cart.cart_id = cart.create_cart_id() cart.is_active = True today = datetime.now() today = today.strftime("%d-%b-%Y") cart.date_created = today cart.sub_total = str(0) cart.tax = str(0) cart.total = str(0) items = Items() try: product_name = item['product_name'] items.item_type = 'products' items.quantity = str(json_data['quantity']) except: items.item_type = 'services' items.quantity = str(json_data['quantity']) items.item_id = items.create_item_id() items.id_service_product = item['id'] items.cart_id = cart.cart_id items.price = item['price'] items.sub_total = str(float(items.price) * int(items.quantity)) cart.sub_total = str(float(cart.sub_total) + float(items.sub_total)) cart.tax = str(0) cart.total = str(float(cart.sub_total) + float(cart.tax)) cart.total_items = str(int(cart.total_items) + int(items.quantity)) cart.put() items.put() # add product to product requests and then add service to service requests if items.item_type == 'products': this_request = ProductRequests() this_request = this_request.addProduct(product=item,uid=cart_owner_id,total=items.quantity) else: this_request = ServiceRequests() this_request = this_request.addService(service=item,uid=cart_owner_id,total=items.quantity) items_request = Items.query(Items.cart_id == cart.cart_id) items_list = items_request.fetch() ser_items = [] for item in items_list: ser_items.append(item.to_dict()) cart = cart.to_dict() # let results = {status : true, cart_items : [], cart : {}, error: {} }; response_data = { 'status' : 'True' , 'cart_items': ser_items, 'cart' : cart, 'error' : {} } elif 'user' in route: json_data = json.loads(self.request.body) logging.info('User Data') logging.info(json_data) this_user = User() this_user = this_user.addUser(json_user=json_data) if this_user != '': response_data = this_user.to_dict() else: status_int = 403 response_data = {'message': 'user not found'} elif 'store' in route: json_data = json.loads(self.request.body) this_store = Store() this_store = this_store.addStore(store=json_data) if(this_store != ''): response_data = this_store.to_dict() else: status_int = 403 response_data = {'message': 'store not stored'} elif 'transactions' in route: json_data = json.loads(self.request.body) this_transaction = Transactions() result = this_transaction.addTransaction(transaction=json_data) logging.info(this_transaction) response_data = this_transaction.to_dict() elif 'sms' in route: uid = route[len(route) - 1] user = User() this_user = user.getUser(uid) if ('send' in route) and (this_user != ''): sms_message = json.loads(self.request.body) sms = SMSMessage() # once the message is sent results will be returned # through a webhook results = sms.addSMSmessage(sms_message=sms_message) response_data = {} response_data = results.to_dict() elif ('contact-lists' in route) and (this_user != ''): json_contact_lists = json.loads(self.request.body) contacts_lists = ContactLists() results = contacts_lists.addList(contact_list=json_contact_lists) response_data = {} response_data = results.to_dict() elif ('smscontact' in route) and (this_user != ''): json_contact = json.loads(self.request.body) logging.info(json_contact) sms_contact = SMSContacts() results = sms_contact.addContact(contact=json_contact) contact_list = sms_contact.fetchContactsByListID(id=json_contact['list_id']) response_data = [] for contact in contact_list: response_data.append(contact.to_dict()) else: status_int = 303 response_data = {'message':'request not understood'} elif 'dashboard' in route: uid = route[len(route) - 1] admin_user_query = User.query(User.uid == uid) admin_user_list = admin_user_query.fetch() response_data = '' if len(admin_user_list) > 0: admin_user = admin_user_list[0] if ('contact' in route) and admin_user.is_admin : if 'response' in route : response_message = json.loads(self.request.body) this_response = Response() results = this_response.createResponse(response_data=response_message, admin_user=admin_user) response_data = results.to_dict() else: status_int = 400 response_data = {'message': 'request could not be understood'} elif ('banking' in route) and admin_user.is_admin: banking_details = json.loads(self.request.body) this_banking = Banking() results = this_banking.addAdminBank(banking_details=banking_details) if (results != ''): response_data = results.to_dict() else: status_int = 401 response_data = {'message': 'error saving banking details'} else: status_int = 400 response_data = { 'message': 'request could not be understood'} else: status_int = 400 response_data = { 'message': 'request could not be understood'} else: status_int = 400 response_data = {'message': 'the server cannot process this request'} self.response.headers['Content-Type'] = "application/json" self.response.status_int = status_int json_data = json.dumps(response_data) self.response.write(json_data)