def create_post(date, thread, message, user, forum, is_approved, is_highlighted, is_spam, is_deleted, is_edited, parent): db = connect() cursor = db.cursor() try: if parent is None: is_root = 0 path = ' ' else: is_root = 1 cursor.execute("""SELECT path FROM Post WHERE id = %s""", (parent,)) path = cursor.fetchone()[0] cursor.execute("""INSERT INTO Post (date, thread, message, user, forum, isApproved, isHighlighted, isSpam, isDeleted, isEdited,parent,isRoot) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) """, (date, thread, message, user, forum, is_approved, is_highlighted, is_spam, is_deleted, is_edited, parent, is_root)) cursor.execute(""" SELECT * FROM Post WHERE forum=%s AND user=%s AND message=%s AND thread=%s """, (forum, user, message, thread)) db_id = cursor.fetchone() results = { "code": 0, "response": { "date": date, "forum": forum, "id": db_id[0], "isApproved": is_approved, "isDeleted": is_deleted, "isEdited": is_edited, "isHighlighted": is_highlighted, "isSpam": is_spam, "message": message, "parent": db_id[5], "thread": thread, "user": user } } post_id = cursor.lastrowid base36 = int2str(int(post_id), radix=36) path += str(len(base36)) + base36 cursor.execute("""UPDATE Post SET path = %s WHERE id = %s""", (path, post_id)) cursor.execute(""" SELECT count(*) FROM Post WHERE thread=%s and isDeleted=0""", (thread,)) posts_count = cursor.fetchone() cursor.execute(""" UPDATE Thread SET posts=%s WHERE id=%s""", (str(posts_count[0]), thread)) print posts_count[0] cursor.close() db.commit() db.close() return results except MySQLdb.IntegrityError as e: if e[0] == 1062: return response_dict[5] elif e[0] == 1452: return response_dict[1] else: return response_dict[4]
def create_post(date, thread, message, user, forum, is_approved, is_highlighted, is_spam, is_deleted, is_edited, parent): db = connect() cursor = db.cursor() try: path = '' if parent is None: is_root = 0 else: is_root = 1 cursor.execute("""SELECT path FROM posts WHERE id = %s""", (parent,)) path = cursor.fetchone() cursor.execute("""INSERT INTO posts (date, thread, message, user, forum, isApproved, isHighlighted, isSpam, isDeleted, isEdited,parent,isRoot) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) """, (date, thread, message, user, forum, is_approved, is_highlighted, is_spam, is_deleted, is_edited, parent, is_root)) cursor.execute(""" SELECT * FROM posts WHERE forum=%s AND user=%s AND message=%s AND thread=%s """, (forum, user, message, thread)) db_id = cursor.fetchone()[0] results = { "code": 0, "response": { "date": date, "forum": forum, "id": db_id, "isApproved": is_approved, "isDeleted": is_deleted, "isEdited": is_edited, "isHighlighted": is_highlighted, "isSpam": is_spam, "message": message, "parent": parent, "thread": thread, "user": user } } post_id = cursor.lastrowid base36 = int2str(int(post_id), radix=36) path += str(len(base36)) + base36 cursor.execute("""UPDATE posts SET path = %s WHERE id = %s""", (path, post_id)) cursor.execute(""" SELECT count(*) FROM posts WHERE thread = %s and isDeleted = FALSE""", (thread,)) posts_count = cursor.fetchone()[0] cursor.execute(""" UPDATE threads SET posts = %s WHERE id = %s""", (str(posts_count), thread)) print posts_count cursor.close() db.commit() db.close() return results except MySQLdb.IntegrityError as e: if e[0] == 1062: return response_dict[5] elif e[0] == 1452: return response_dict[1] else: return response_dict[4]
def create_post(): try: content_json = request.json except BadRequest: return jsonify({'code': 2, 'response': "Invalid request(syntax)"}) if 'isApproved' not in content_json or 'user' not in content_json or 'date' not in content_json \ or 'message' not in content_json or 'isSpam' not in content_json or 'isHighlighted' not in content_json \ or 'thread' not in content_json or 'forum' not in content_json or 'isDeleted' not in content_json\ or 'isEdited' not in content_json: return jsonify({ 'code': 3, 'response': "Incorrect request: some data missing" }) db = mysql.get_db() cursor = db.cursor() if content_json['parent'] is None: isRoot = True path = '' else: isRoot = False cursor.execute("""SELECT `path` FROM `posts` WHERE `id` = %s""", (content_json['parent'], )) path = cursor.fetchone()[0] try: cursor.execute( """INSERT INTO `posts` (`isApproved`, `user`, `date`, `message`, `isSpam`,`isHighlighted`, `thread`, `forum`,`isDeleted`, `isEdited`,`parent`,`isRoot`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);""", (content_json['isApproved'], content_json['user'], content_json['date'], content_json['message'], content_json['isSpam'], content_json['isHighlighted'], content_json['thread'], content_json['forum'], content_json['isDeleted'], content_json['isEdited'], content_json['parent'], isRoot)) post_id = cursor.lastrowid base36 = int2str(int(post_id), radix=36) path += str(len(base36)) + base36 cursor.execute("""UPDATE `posts` SET path = %s WHERE `id` = %s""", (path, post_id)) cursor.execute( """UPDATE `threads` SET `posts` = `posts` + 1 WHERE `id` = %s;""", (content_json['thread'], )) except MySQLdb.Error: return jsonify({ 'code': 3, 'response': "Incorrect request: post is already exist" }) db.commit() content_json.update({'id': post_id}) return jsonify({'code': 0, 'response': content_json})
def create_post(): try: content_json = request.json except BadRequest: return jsonify({'code': 2, 'response': "Invalid request(syntax)"}) if 'isApproved' not in content_json or 'user' not in content_json or 'date' not in content_json \ or 'message' not in content_json or 'isSpam' not in content_json or 'isHighlighted' not in content_json \ or 'thread' not in content_json or 'forum' not in content_json or 'isDeleted' not in content_json\ or 'isEdited' not in content_json: return jsonify({'code': 3, 'response': "Incorrect request: some data missing"}) db = mysql.get_db() cursor = db.cursor() if content_json['parent'] is None: isRoot = True path = '' else: isRoot = False cursor.execute("""SELECT `path` FROM `posts` WHERE `id` = %s""", (content_json['parent'],)) path = cursor.fetchone()[0] try: cursor.execute( """INSERT INTO `posts` (`isApproved`, `user`, `date`, `message`, `isSpam`,`isHighlighted`, `thread`, `forum`,`isDeleted`, `isEdited`,`parent`,`isRoot`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);""", ( content_json['isApproved'], content_json['user'], content_json['date'], content_json['message'], content_json['isSpam'], content_json['isHighlighted'], content_json['thread'], content_json['forum'], content_json['isDeleted'], content_json['isEdited'], content_json['parent'], isRoot ) ) post_id = cursor.lastrowid base36 = int2str(int(post_id), radix=36) path += str(len(base36)) + base36 cursor.execute("""UPDATE `posts` SET path = %s WHERE `id` = %s""", (path, post_id)) cursor.execute("""UPDATE `threads` SET `posts` = `posts` + 1 WHERE `id` = %s;""", (content_json['thread'],)) except MySQLdb.Error: return jsonify({'code': 3, 'response': "Incorrect request: post is already exist"}) db.commit() content_json.update({'id': post_id}) return jsonify({'code': 0, 'response': content_json})
def _inc_path(cls, path): """ :returns: The path of the next sibling of a given node path. """ base = len(cls.alphabet) newpos = numconv.str2int(path[-cls.steplen:], base, cls.alphabet) + 1 key = numconv.int2str(newpos, base, cls.alphabet) if len(key) > cls.steplen: raise PathOverflow("Path Overflow from: '%s'" % (path, )) return '%s%s%s' % (path[:-cls.steplen], '0'*(cls.steplen-len(key)), key)
def _get_path(cls, path, depth, newstep): """ Builds a path given some values :param path: the base path :param depth: the depth of the node :param newstep: the value (integer) of the new step """ parentpath = cls._get_basepath(path, depth-1) key = numconv.int2str(newstep, len(cls.alphabet), cls.alphabet) return '%s%s%s' % (parentpath, '0'*(cls.steplen-len(key)), key)
def is_happy_num(num, base): cur_number = numconv.int2str(num, radix=base) passed_numbers = [cur_number] while True: cur_number = squared_sum(cur_number, base) if cur_number == "1": return True elif cur_number == "0": break elif cur_number in passed_numbers: break passed_numbers.append(cur_number) return False
def test_int2str_with_alphabet(): assert int2str(100, alphabet='abcdefghijklm') == 'baa'
def test_int2str_with_radix_and_alphabet(): assert int2str(37, 4, 'rofl') == 'foo'
def test_int2str(num, radix, expected): assert int2str(num, radix) == expected
def hash(self, text): ident = hashlib.md5() ident.update(text) digest = ident.hexdigest() number = int(digest, 16) % 0xffffffff return numconv.int2str(number, 64)
def post_create(): json_dict = parse_json(request) parent = json_dict.get('parent', None) thread = json_dict.get('thread', None) is_deleted = json_dict.get('isDeleted', False) is_spam = json_dict.get('isSpam', False) is_edited = json_dict.get('isEdited', False) is_post_approved = json_dict.get('isApproved', False) is_highlighted = json_dict.get('isHighlighted', False) forum = json_dict.get('forum', None) user = json_dict.get('user', None) date = json_dict.get('date', None) message = json_dict.get('message', None) db = get_connection() cursor = db.cursor() post_id = 0 if parent is None: isRoot = True matPath = '' else: isRoot = False cursor.execute("""SELECT `matPath` FROM `posts` WHERE `id` = %s""", (parent,)) matPath = cursor.fetchone()[0] try: cursor.execute( """INSERT INTO `posts` (`thread`, `isDeleted`, `isSpam`, `isEdited`, `isApproved`, `isHighlighted`, `forum`, `user`, `date`, `message`, `parent`, `isRoot`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);""", (thread, is_deleted, is_spam, is_edited, is_post_approved, is_highlighted, forum, user, date, message, parent, isRoot)) post_id = cursor.lastrowid base36 = int2str(int(post_id), radix=36) matPath += str(len(base36)) + base36 cursor.execute("""UPDATE `posts` SET matPath = %s WHERE `id` = %s""", (matPath, post_id)) cursor.execute("""UPDATE `threads` SET `posts` = `posts` + 1 WHERE `id` = %s;""", (thread,)) db.commit() except MySQLdb.Error: db.rollback() cursor.close() db.close() post = { "date": date, "forum": forum, "id": post_id, "isApproved": is_post_approved, "isDeleted": is_deleted, "isEdited": is_edited, "isHighlighted": is_highlighted, "isSpam": is_spam, "message": message, "parent": parent, "thread": thread, "user": user } return jsonify({'code': 0, 'response': post})
def squared_sum(num, base): sum = 0 for i in num: sum = sum + int(i) * int(i) return numconv.int2str(sum, radix=base)