def create_card(self, call): '''Create a card. Parameters ---------- call : CallbackQuery Response to button press. ''' handler = tools.Handler(self.bot) if handler.error(call.message) or handler.cancel(call.message): return key = re.findall(r'\w-\d+-\d+-\w+', call.data)[0] card_key = f'c-{random.randint(1, 1000000000)}-' \ f'{random.randint(1, 1000000000)}-d' # User status update. update = db.Update(call.message.chat.id) update.user_attribute('action', 3) update.user_attribute('session', card_key) # Allocating space for the card in the database. insert = db.Insert(call.message.chat.id, 'collections', 'card') insert.create_card(key, card_key, datetime.datetime.now()) text = messages.CARDS['CARD_NAME'] self.bot.answer_callback_query(call.id, text, True) self.bot.send_message(call.message.chat.id, text) self.bot.register_next_step_handler(call.message, self._card_name)
def create_collection(self, call): '''Collection creation. Parameters ---------- call : CallbackQuery Response to button press. ''' handler = tools.Handler(self.bot) if handler.error(call.message) or handler.cancel(call.message): return date = datetime.datetime.now() key = f'k-{random.randint(1, 1000000000)}' \ f'-{random.randint(1, 1000000000)}-n' # User status update. update = db.Update(call.message.chat.id) update.user_attribute('action', 1) update.user_attribute('session', key) # Allocating space for a collection in the database. insert = db.Insert(call.message.chat.id, 'collections', 'collection') insert.create_collection(key, date) text = messages.COLLECTIONS['CREATE_COLLECTION'] self.bot.answer_callback_query(call.id, text, True) self.bot.send_message(call.message.chat.id, text) self.bot.register_next_step_handler(call.message, self._save_collection)
def test_FRDB2_2(self): database.Insert("First", "", "", Encrypt.cryptEncode(self.keys[0], "First123"), self.keys[0]) database.Insert("Second", "", "First", Encrypt.cryptEncode(self.keys[1], "Second1"), self.keys[1]) database.Insert("Third", "Type", "Username", Encrypt.cryptEncode(self.keys[2], "Third123"), self.keys[2]) oldQuery = database.GetN("First") database.UpdateU(1, "ChangedFirst") newQuery = database.GetN("First") self.assertEqual(oldQuery.ID, newQuery.ID) self.assertNotEqual(oldQuery.UserName, newQuery.UserName)
def copy_collection(self, message): '''Creating a copy of a user collection. Parameters ---------- message : Message User message. ''' # Getting a copy of the name and number of cards in the collection. fetch = db.Fetch(message.chat.id, 'collections', 'collection') name = fetch.general_collection(message.text, 'name') cards = fetch.general_collection(message.text, 'cards') karma = fetch.general_collection(message.text, 'user_id') # Checking for a duplicate collection in the database. fetch = db.Fetch(message.chat.id, 'collections', 'collection') result = fetch.copy_check('name', f'{name} (Копия)') if result: self.bot.send_message(message.chat.id, messages.ERRORS[9]) self.bot.register_next_step_handler(message, self.copy_collection) return # Getting a unique collection key from the database. fetch = db.Fetch(message.chat.id) key = fetch.user_attribute('session') # Making copies of cards from the original collection. date = datetime.datetime.now() insert = db.Insert(message.chat.id, 'collections', 'card') insert.copy_collection(message.text, key, date) # Inserting the name of the collection # and the number of cards in the database. update = db.Update(message.chat.id, 'collections', 'collection') update.collection_attribute(key, 'name', f'{name} (Копия)') update.collection_attribute(key, 'cards', cards) # User status update. update = db.Update(message.chat.id) update.user_attribute('action', 0) update.user_attribute('session', None) update.change_user_attribute('collections', 1) update.change_user_attribute('cards', cards) # Adding karma to the user whose collection was copied. if karma != message.chat.id: update = db.Update(karma) update.change_user_attribute('karma', 1) text = messages.COLLECTIONS['COLLECTION_COPIED'] self.bot.send_message(message.chat.id, text.format(name)) self.send_menu(message)
def test_FRDB3(self): database.Insert("First", "", "", Encrypt.cryptEncode(self.keys[0], "First123"), self.keys[0]) database.Insert("Second", "", "First", Encrypt.cryptEncode(self.keys[1], "Second1"), self.keys[1]) database.Insert("Third", "Type", "Username", Encrypt.cryptEncode(self.keys[2], "Third123"), self.keys[2]) databaseLengthBefore = len(database.GetAll()) oldQuery = database.GetId(1) database.Delete(1) databaseLengthAfter = len(database.GetAll()) self.assertEqual(oldQuery.AccName, "First") with self.assertRaises(IndexError): database.GetId(1)
def test_FRDB2_1(self): database.Insert("First", "", "", Encrypt.cryptEncode(self.keys[0], "First123"), self.keys[0]) database.Insert("Second", "", "First", Encrypt.cryptEncode(self.keys[1], "Second1"), self.keys[1]) database.Insert("Third", "Type", "Username", Encrypt.cryptEncode(self.keys[2], "Third123"), self.keys[2]) oldQuery = database.GetN("First") database.UpdateP(1, Encrypt.cryptEncode(self.keys[0], "First321")) newQuery = database.GetN("First") self.assertEqual(oldQuery.ID, newQuery.ID) self.assertNotEqual( Encrypt.cryptDecode(self.keys[0], oldQuery.HashVal.encode('utf-8')), Encrypt.cryptDecode(self.keys[0], newQuery.HashVal.encode('utf-8')))
def test_FRDB1(self): database.Insert("First", "", "", Encrypt.cryptEncode(self.keys[0], "First123"), self.keys[0]) First = database.GetN("First") self.assertEqual(First.AccName, "First") # if master password has "First" as Name self.assertNotEqual( First.HashVal, "First123") # if master password is directly in database self.assertEqual(First.ID, 1) # if master password has 1 as ID.
def test_FRDB4(self): database.Insert("First", "Test", "", Encrypt.cryptEncode(self.keys[0], "First123"), self.keys[0]) database.Insert("Second", "Test", "First", Encrypt.cryptEncode(self.keys[1], "Second1"), self.keys[1]) database.Insert("Third", "Test", "Username", Encrypt.cryptEncode(self.keys[2], "Third123"), self.keys[2]) All = database.GetAll() Types = database.GetT("Test") First_id = database.GetId(1) First_Name = database.GetN("First") self.assertEqual(len(All), 3) self.assertEqual(len(Types), 3) self.assertEqual(All[0], First_id) self.assertEqual(All[0], First_Name) self.assertEqual(Types[0], First_id) self.assertEqual(Types[0], First_Name) [self.assertEqual(x.AccType, "Test") for x in Types]
def signup(): if request.method == "GET": return render_template("signup.html") elif request.method == "POST": err_msg = {"result": "NO"} param = json.loads(request.data.decode("utf-8")) print(param) firstname = param.get('firstname') lastname = param.get('lastname') username = param.get('username') GM = param.get('GM') # NGM = param.get('NGM') password = param.get('password') repassword = param.get('repassword') if firstname is None: err_msg["msg"] = "缺少姓" return jsonify(err_msg) if lastname is None: err_msg["msg"] = "缺少名" return jsonify(err_msg) if username is None: err_msg["msg"] = "缺少用户名" return jsonify(err_msg) if GM is None: return jsonify(err_msg) if password is None: err_msg["msg"] = "缺少密码" return jsonify(err_msg) if repassword is None: err_msg["msg"] = "未重复输入密码" return jsonify(err_msg) if password != repassword: err_msg["msg"] = "密码两次不一致" return jsonify(err_msg) user = database.Search(cursor, "mydb.users", "username", username) if len(user) == 0: user_id = len(database.Search(cursor, "mydb.users")) + 1 user_class = User( ((user_id, firstname, lastname, username, GM, password), )) user_class.hash_password(password) user = (user_id, firstname, lastname, username, GM, user_class.password_hash) database.Insert(db, "mydb.users", user) return jsonify({"result": "OK"}) else: err_msg["msg"] = "用户已经注册" return jsonify(err_msg) return jsonify({"result": "OK", "next_url": "/"})
def start(message): '''Getting started with a bot. Parameters ---------- message : Message User message. ''' # Adding a new user to the database. insert = db.Insert(message.chat.id) insert.new_user(username=message.from_user.username, first_name=message.from_user.first_name, last_name=message.from_user.last_name) private_office(message)
def new_customer(): if request.method == 'POST': if request.form.get('Take Photo') == 'Take Photo': photo_name = request.values.get("idphoto") if (len(photo_name) == 0) | (string.find(photo_name, ip_address) != -1): global name name = cam.takephoto() photo_name = ip_address + ":5000/preview.jpg" print("Take Photo") source = "static/image/" + name print(source) return render_template( "new_cus.html", photo=photo_name, preview=u'<strong>Photo Preview</strong>', preview_photo=u'<img src ="' + source + '" width="256" height="256"/><br /><br />') else: photo_name = request.values.get("idphoto") return render_template( "new_cus.html", photo=photo_name, preview=u'<strong>Photo Preview</strong>', preview_photo=u'<img src="' + photo_name + '" width="256" height="256"/><br /><br />') elif request.form.get('Create User') == 'Create User': global id id = request.values.get("id") fn = request.values.get("First Name") ln = request.values.get("Last Name") cp = request.values.get("Company") email = request.values.get("Email") mp = request.values.get("MobileP") wp = request.values.get("WorkP") hp = request.values.get("HomeP") ad = request.values.get("Address") url = request.values.get("Website") fb = request.values.get("facebook") tw = request.values.get("twitter") lk = request.values.get("linkedin") yb = request.values.get("youtube") photo = request.values.get("idphoto") if string.find(photo, ip_address) != -1: print("rename") new_name = "/home/pi/MFRC522-python/static/image/" + id + ".jpg" old_name = "/home/pi/MFRC522-python/static/image/" + name if os.path.exists(old_name): os.rename(old_name, new_name) df.delete('/home/pi/MFRC522-python/static/image/', 'photo') photo = "localhost:5000/" + id + ".jpg" zap = request.values.get("zapcode") zap_name = '/home/pi/MFRC522-python/static/Zapcode/' + id + '.png' cus_info = str(random.randint(0, 9999)) wf.write(id, fn, ln, cp, email, mp, wp, hp, ad, url, fb, tw, lk, yb, photo, zap, cus_info) reader.write(id) GPIO.cleanup() db.Insert("Customer", id, fn, ln, cp, email, mp, wp, hp, ad, url, fb, tw, lk, yb, photo, zap, cus_info) urllib.urlretrieve(zap, zap_name) print("Create") else: zap = request.values.get("zapcode") zap_name = '/home/pi/MFRC522-python/static/Zapcode/' + id + '.png' df.delete('/home/pi/MFRC522-python/static/image/', 'photo') cus_info = str(random.randint(0, 9999)) wf.write(id, fn, ln, cp, email, mp, wp, hp, ad, url, fb, tw, lk, yb, photo, zap, cus_info) reader.write(id) GPIO.cleanup() db.Insert("Customer", id, fn, ln, cp, email, mp, wp, hp, ad, url, fb, tw, lk, yb, photo, zap, cus_info) urllib.urlretrieve(zap, zap_name) print("Create") return redirect(url_for('existed_customer')) elif request.method == 'GET': print("No call") return render_template("new_cus.html")