def __save_content(args, message_type, user_id, conversation_parties): with database.transaction(): if(message_type.name == 'directive_quotation_mt'): mc = MessageContent() id_quotation = args.get('id_quotation', None) if(id_quotation is not None): quotation = Quotation.select().where(Quotation.id == id_quotation).first() else: creator = User.select().where(User.id == user_id).first() receiver = conversation_parties.select().where(ConversationParty.user != creator).first().user quotation = save_quotation(args, creator, receiver) mc.quotation = quotation mc.save() return mc, config['QUOTATION_DIRECTIVE_MT_DISPLAY_TEXT'] elif(message_type.name == 'common_text'): mc = MessageContent() tc = save_text_content(args) mc.text_content = tc mc.save() text = tc.text return mc, (text[:config['COMMON_TEXT_MAX_LEN']] + '...') if len(text) > config['COMMON_TEXT_MAX_LEN'] else text
def test_create(self): """ Test that a Quotation.create works when supported language and quotation character """ tests = [ {"lc": constants.LC_FRENCH, "char": u"«"}, {"lc": constants.LC_ENGLISH, "char": u"\"", "close": True}, {"lc": constants.LC_JAPANESE, "char": u"「"} ] for test in tests: q = Quotation.create(test["lc"], test["char"], force_close=("close" in test)) self.assertTrue(q) self.assertEqual(str(q), test["char"].encode('utf-8'))
def test_mirror(self): """ Test that a QuotationNotFound is raised when unsupported languages or quotations requested """ from errors import QuotationNotFound tests = [ {"opening": u"„", "closing": u"“", "lc": constants.LC_GERMAN}, {"opening": u"『", "closing": u"』", "lc": constants.LC_CHINESE} ] for test in tests: opening = Quotation.create(test["lc"], test["opening"]) closing = opening.mirror() self.assertTrue(str(closing), test["closing"]) self.assertTrue(opening ^ closing)
def extract(self): """ Yields Quotation instance extractable from text """ closed = True for index, char in enumerate(self.text): if char in QUOTATION_MAP.get(self.lc, []): if char in LONE_RANGERS and index > 0: prev_char = self.text[index-1] if ALPHANUMERIC_PATTERN.match(prev_char): # prev char is a alphabet; highly likely to be a lone ranger, not a true quotation # ignore lone ranger continue closed = not closed # toggle to open from start prev = Quotation.create(self.lc, char, force_close=closed) yield prev
def get_company(cls, company_symbol): """ Query a company by its symbol """ company = Company.query.filter_by(symbol=company_symbol).one() fetch_full = len(company.quotations) == 0 try: ts = TimeSeries(key=ALPHAVANTAGE_API_KEY) data, meta_data = ts.get_daily_adjusted(symbol=company_symbol, outputsize=fetch_full) for date, quotation_data in data.items(): mapped_qd = map_quotation_key(quotation_data) data[date] = mapped_qd quotation = cls.get(company_id=company.id, date=date) if quotation: quotation.data = mapped_qd quotation.save() else: Quotation(company_id=company.id, date=date, data=mapped_qd).save() return data except ValueError: return Quotation.query.filter_by(company_id=company.id).all()
from models import BaseModel, TextContent, MessageContent, Photo, Role, User, UserRoles, MessageType, ConversationType, Conversation, Message, ConversationParty, Quotation, Company, database try: database.drop_tables(BaseModel.__subclasses__(), safe = True, cascade = True) # database.create_tables(BaseModel.__subclasses__()) Photo.create_table() Role.create_table() User.create_table() UserRoles.create_table() MessageType.create_table() ConversationType.create_table() Quotation.create_table() Company.create_table() TextContent.create_table() MessageContent.create_table() Conversation.create_table() Message.create_table() ConversationParty.create_table() database.create_foreign_key(Conversation, Conversation.last_message) except Exception as e: print('Error while creating schema %s' % e)