def get_emoji_html_escape(key, type="category"): key = key.encode('utf-8') if type == "brand": mapping_dict = brand_emoji_dict else: mapping_dict = category_emoji_dict if mapping_dict.has_key(key) is False: return "" emoji_name = mapping_dict[key] if emoji_encoding_dict.has_key(emoji_name) is False: log.error("get_emoji('%s', '%s') fail: emoji_encoding_dict doesn't have key of '%s'" \ % (key, type, emoji_name)) unicode_name = config.DEFAULT_EMOJI else: unicode_name = emoji_encoding_dict[emoji_name] if emoji_name == ":coffee:": ret = "☕" else: unicode_str = "%s" % repr(unicode_name) ret = "&#x%s;" % unicode_str[7:-1] return ret
def get_emoji(key, type="category"): if type == "brand": mapping_dict = brand_emoji_dict else: mapping_dict = category_emoji_dict if mapping_dict.has_key(key) is False: return "" emoji_name = mapping_dict[key] if emoji_encoding_dict.has_key(emoji_name) is False: log.error("get_emoji('%s', '%s') fail: emoji_encoding_dict doesn't have key of '%s'" \ % (key, type, emoji_name)) return "" return emoji_encoding_dict[emoji_name]
def insert_expense(expense): conn = MySQLdb.connect(config.DB_HOST, config.DB_USERNAME, config.DB_PWD, \ config.DB_NAME, charset='utf8mb4', port=config.DB_PORT) cursor = conn.cursor() sql = Expense.generate_insert_sql(expense) log.info("SQL:" + sql) #print sql try: cursor.execute(sql) conn.commit() except MySQLdb.Error: log.error("SQL fail: %s" % sql) conn.rollback() cursor.close() # TODO: defensive check return True
def delete_obj_by_id(userid, expenseid): if expenseid != "": sql = "delete from expenses where userid='" + userid + "' and expenseid=" + expenseid else: sql = "delete from expenses where userid='" + userid + "'" conn = MySQLdb.connect(config.DB_HOST, config.DB_USERNAME, config.DB_PWD, \ config.DB_NAME, charset='utf8mb4', port=config.DB_PORT) log.info(sql) cursor = conn.cursor() # TODO: report error try: cursor.execute(sql) conn.commit() except MySQLdb.Error: log.error("ERROR delete mysql fail") conn.rollback() cursor.close() return True
def delete_obj_by_id(userid, expenseid): if expenseid != "": sql = "delete from expenses where userid='" + userid + "' and expenseid=" + expenseid else: sql = "delete from expenses where userid='" + userid + "'" conn = MySQLdb.connect( config.DB_HOST, config.DB_USERNAME, config.DB_PWD, config.DB_NAME, charset="utf8mb4", port=config.DB_PORT ) log.info(sql) cursor = conn.cursor() # TODO: report error try: cursor.execute(sql) conn.commit() except MySQLdb.Error: log.error("ERROR delete mysql fail") conn.rollback() cursor.close() return True
def audit_log(request): # from direct http request if request.values.has_key("userid"): userid = request.values["userid"] else: userid = "weixin" try: # from weixin data = request.data msg = util._parse_msg(data) userid = msg["FromUserName"] except Exception, e: log.error(sys.exc_info()) log.error("audit_log error: " + str(e)) log.error("raw_msg: " + str(data))
def add_expense(userid, content): wechat_user = user.get_user_by_id(userid) content = content.encode('utf-8', 'ignore').strip() # bypass word split #content = util.wash_sentence(content) (code, date, category, amount, brand, comment, token_list) \ = split_expense_word(" %s " % (content)) expense = Expense() expense.init_with_mysql(userid, amount, category, \ brand, date, content, config.DEFAULT_COMMENT) if code == config.RECORD_STATE_QUESTION: return chat.response_fail(expense, "?") if code == config.RECORD_STATE_MULTIPLE_RECORDS: return chat.response_fail(expense, "multiple_record") # print "====== Before (%s, %s, %d; %s) ====" % (category, brand, amount, expense.memo) # store tokens, if detection fail if category == "" or brand == "" or amount == -1: expense.memo = "" for text, start, end in token_list: expense.memo = "%s; %s" % (expense.memo, text) expense.memo = expense.memo.strip("; ") # print "====== After (%s, %s, %d; %s) ====" % (category, brand, amount, expense.memo) if chat.is_comment_meaningless(comment) is True: return chat.response_fail(expense, "empty_notes") if chat.is_comment_pure_alpha(comment) is True: return chat.response_fail(expense, "pure_alpha") if expense.amount == -1: return chat.reply_chat(expense, wechat_user) if expense.amount != -1: if insert_expense(expense) is False: rep_content = MSG.REPLY_EXPENSE_FAILED % comment log.error("user(%s) add_expense fail, content:%s" % (userid, content)) if (expense.category != config.DEFAULT_CATEGORY) and (expense.branding != config.DEFAULT_BRANDING): tips = "" # TODO to be implemented rep_content = MSG.REPLY_EXPENSE_ADDED % ( category + get_emoji(category, "category"), str(amount)) rep_content = chat.attach_detected_extra_msg(wechat_user, rep_content, expense) else: if expense.category != config.DEFAULT_CATEGORY: tips = "" # TODO to be implemented rep_content = MSG.REPLY_EXPENSE_ADDED_WITH_CATEGORY % (\ category + get_emoji(category, "category"), str(amount)) else: if expense.branding != config.DEFAULT_BRANDING: tips = "" # TODO to be implemented rep_content = MSG.REPLY_EXPENSE_ADDED_WITH_BRANDING % (\ brand + get_emoji(brand, "brand"), str(amount)) else: tips = "" # TODO to be implemented rep_content = MSG.REPLY_EXPENSE_ADDED_SIMPLE % (\ str(date[5:7]).lstrip("0"), str(date[8:10]).lstrip("0"), \ str(amount), comment, tips) rep_content = chat.attach_undetected_extra_msg(wechat_user, expense, rep_content) if expense.category == config.CATEGORY_INCOMING: rep_content = rep_content.replace("消费", "赚了") return rep_content
def add_expense(userid, content): wechat_user = user.get_user_by_id(userid) content = content.encode('utf-8', 'ignore').strip() # bypass word split #content = util.wash_sentence(content) (code, date, category, amount, brand, comment, token_list) \ = split_expense_word(" %s " % (content)) expense = Expense() expense.init_with_mysql(userid, amount, category, \ brand, date, content, config.DEFAULT_COMMENT) if code == config.RECORD_STATE_QUESTION: return chat.response_fail(expense, "?") if code == config.RECORD_STATE_MULTIPLE_RECORDS: return chat.response_fail(expense, "multiple_record") # print "====== Before (%s, %s, %d; %s) ====" % (category, brand, amount, expense.memo) # store tokens, if detection fail if category == "" or brand == "" or amount == -1: expense.memo = "" for text, start, end in token_list: expense.memo = "%s; %s" % (expense.memo, text) expense.memo = expense.memo.strip("; ") # print "====== After (%s, %s, %d; %s) ====" % (category, brand, amount, expense.memo) if chat.is_comment_meaningless(comment) is True: return chat.response_fail(expense, "empty_notes") if chat.is_comment_pure_alpha(comment) is True: return chat.response_fail(expense, "pure_alpha") if expense.amount == -1: return chat.reply_chat(expense, wechat_user) if expense.amount != -1: if insert_expense(expense) is False: rep_content = MSG.REPLY_EXPENSE_FAILED % comment log.error("user(%s) add_expense fail, content:%s" % (userid, content)) if (expense.category != config.DEFAULT_CATEGORY) and ( expense.branding != config.DEFAULT_BRANDING): tips = "" # TODO to be implemented rep_content = MSG.REPLY_EXPENSE_ADDED % ( category + get_emoji(category, "category"), str(amount)) rep_content = chat.attach_detected_extra_msg( wechat_user, rep_content, expense) else: if expense.category != config.DEFAULT_CATEGORY: tips = "" # TODO to be implemented rep_content = MSG.REPLY_EXPENSE_ADDED_WITH_CATEGORY % (\ category + get_emoji(category, "category"), str(amount)) else: if expense.branding != config.DEFAULT_BRANDING: tips = "" # TODO to be implemented rep_content = MSG.REPLY_EXPENSE_ADDED_WITH_BRANDING % (\ brand + get_emoji(brand, "brand"), str(amount)) else: tips = "" # TODO to be implemented rep_content = MSG.REPLY_EXPENSE_ADDED_SIMPLE % (\ str(date[5:7]).lstrip("0"), str(date[8:10]).lstrip("0"), \ str(amount), comment, tips) rep_content = chat.attach_undetected_extra_msg( wechat_user, expense, rep_content) if expense.category == config.CATEGORY_INCOMING: rep_content = rep_content.replace("消费", "赚了") return rep_content