def check_db_write(): from model import Option try: Option.set('last_check', utcnow().isoformat()) except Exception as e: logbook.warning(e) return str(e)
def setUp(self): self.__votation__ = Votation( \ votation_description = 'Simple Votation with voters for vote test ' + str(random.randint(0,50000)) , \ description_url = "" , \ votation_type = votation_dao.TYPE_SIMPLE_MAJORITY , \ promoter_user_id = 1 , \ begin_date = datetime(2018,1,1) , \ end_date = datetime(2018,1,15) , \ votation_status = 2 , \ list_voters = 1) self.assertTrue(votation_dao.insert_votation_dto(self.__votation__)) o1 = Option(votation_id=self.__votation__.votation_id, \ option_name = 'test.option1') self.assertTrue(option_dao.insert_dto(o1)) o2 = Option(votation_id=self.__votation__.votation_id, \ option_name = 'test.option2') self.assertTrue(option_dao.insert_dto(o2)) o3 = Option(votation_id=self.__votation__.votation_id, \ option_name = 'test.option3') self.assertTrue(option_dao.insert_dto(o3)) self.__option1 = o1 self.__option2 = o2 self.__option3 = o3 self.assertIsNotNone(o1.option_id) self.assertIsNotNone(o2.option_id) self.assertIsNotNone(o3.option_id) # set juds j1 = Judgement(votation_id = self.__votation__.votation_id, \ jud_value = 0, jud_name = "bad") j2 = Judgement(votation_id = self.__votation__.votation_id, \ jud_value = 1, jud_name = "medium") j3 = Judgement(votation_id = self.__votation__.votation_id, \ jud_value = 2, jud_name = "good") judgement_dao.insert_dto(j1) judgement_dao.insert_dto(j2) judgement_dao.insert_dto(j3) db.session.commit() jud_array = judgement_dao.load_judgement_by_votation( self.__votation__.votation_id) self.__jud1 = jud_array[0] self.__jud2 = jud_array[1] self.__jud3 = jud_array[2] voter1 = Voter(user_id=1, votation_id=self.__votation__.votation_id, voted=0) voter_dao.insert_dto(voter1) db.session.commit() return super().setUp()
def getOptions(data): """Get information about sold options Parameters: data (Dict[str, str]) : Dictionary of information about cars and rentals Return: Dict[int, Option] : Dictionary of options orders by option's ID """ listOptions = {} for opt in data["options"]: if (opt['id'] is None or opt['rental_id'] is None or opt['type'] is None): print("Options : Unfound info \n") return else: if (type(opt['id']) != int or type(opt['rental_id']) != int or type(opt['type']) != str): print("Options : Invalide type \n") return else: option = Option(opt['id'], opt['rental_id'], opt['type']) listOptions[option.id] = option return listOptions
def update_offer(self): self.__offer = Offer() for issue in self.issues: if len(issue.options) == 0: self.offer.add_option(Option("")) else: self.offer.add_option(issue.options[0]) self.offer.calculate_rating()
def save_options_from_text(votation_id, text): lines = text.splitlines() lines = list(map(lambda l: l.strip().upper(), lines)) lines = list(set(lines)) # removing duplicates lines.sort() for l in lines: if l.strip(): o = Option(votation_id=votation_id, option_name=l) if not insert_dto(o): return False return True
def onText(self): content=self.msg_in["Content"] user=self.msg_in["FromUserName"] Option.log(content,user) cmds=" ".join(content.lower().strip().split()).split(" ") if cmds[0]=="set" : if len(cmds)==2 and cmds[1] in searchRange: Option.set(user,cmds[1]) if cmds[1]=="stock": return self.responseText("输入代码或者简称可以查行情了哦,\r后面跟上(.hk/sh/sz)还可区分不同市场的股票!") if cmds[1]=="bus": return self.responseText("输入公交线路 可以查询公交到站情况了,例如输入:M203") else: return self.responseText("请输入set stock 或者 set bus 进行查询设置 ") else: cmd=Option.get(user) if cmd == "": return self.responseText("还没进行查询设置哦,\r 请输入set stock 或者 set bus 进行查询设置 ") elif cmd =="stock": return self.responseText(getStkmkt(content)) elif cmd =="bus": return self.responseText(getArrive(content))
def test_get_set(self): Option.set('test_key', 'test_value') val = Option.get('test_key') self.assertEqual(val, 'test_value') Option.set('test_key', 'test_value2') val = Option.get('test_key') self.assertEqual(val, 'test_value2')
def test_insert(self): votation_id = self.__votation__.votation_id u = Option(votation_id=votation_id,option_name = 'test.option') self.assertTrue( option_dao.insert_dto(u)) self.assertGreater(u.option_id, 0) ar = option_dao.load_options_by_votation(votation_id) self.assertEqual(1,len(ar)) u1 = ar[0] self.assertIsNotNone(u1) self.assertEqual(u.votation_id, u1.votation_id) self.assertEqual(u.option_name, u1.option_name) self.assertTrue(option_dao.delete_dto(u1)) ar = option_dao.load_options_by_votation(votation_id) self.assertEqual(0,len(ar))
def parse_file(path: str) -> List[Issue]: issues: List[Issue] = [] tree: et = et.parse(path) root: et = tree.getroot() if root.tag != "Issues": return [] for issue in root: if issue.tag == "Issue": issue_attribs = issue.attrib if "name" not in issue_attribs and "rating" not in issue_attribs: continue _issue = Issue(issue.attrib["name"]) _issue.rating = int(issue_attribs["rating"]) for option in issue: if option.tag == "Option": option_attribs = option.attrib if "name" not in option_attribs and "rating" not in option_attribs: continue _option = Option(option_attribs["name"]) _option.rating = int(option_attribs["rating"]) _issue.add_option(_option) issues.append(_issue) return issues
def add_poll(): if request.method == 'GET': return render_template('addpoll.html') else: opt1 = request.form.get('option1') url1 = request.form.get('pic_url1') opt2 = request.form.get('option2') url2 = request.form.get('pic_url2') new_option1 = Option(option=opt1, pic_url=url1) new_option2 = Option(option=opt2, pic_url=url2) url3 = request.form.get('pic_url3') opt3 = request.form.get('option3') url4 = request.form.get('pic_url4') opt4 = request.form.get('option4') new_options = [new_option1, new_option2] if opt3 != None: new_option3 = Option(option=opt3, pic_url=url3) session.add(new_option3) new_options.append(new_option3) if opt4 != None: new_option4 = Option(option=opt4, pic_url=url4) session.add(new_option4) new_options.append(new_option4) new_category = request.form.get('category') new_title = request.form.get('usertitle') new_description = request.form.get('user_description') new_poll = Post(category=new_category, title=new_title, description=new_description, options=new_options) session.add(new_poll, new_option2) session.add(new_option1) session.commit() return redirect(url_for('my_feed'))
def onText(self): content = self.msg_in["Content"] user = self.msg_in["FromUserName"] Option.log(content, user) cmds = " ".join(content.lower().strip().split()).split(" ") if cmds[0] == "set": if len(cmds) == 2 and cmds[1] in searchRange: Option.set(user, cmds[1]) if cmds[1] == "stock": return self.responseText( "输入代码或者简称可以查行情了哦,\r后面跟上(.hk/sh/sz)还可区分不同市场的股票!") if cmds[1] == "bus": return self.responseText("输入公交线路 可以查询公交到站情况了,例如输入:M203") else: return self.responseText("请输入set stock 或者 set bus 进行查询设置 ") else: cmd = Option.get(user) if cmd == "": return self.responseText( "还没进行查询设置哦,\r 请输入set stock 或者 set bus 进行查询设置 ") elif cmd == "stock": return self.responseText(getStkmkt(content)) elif cmd == "bus": return self.responseText(getArrive(content))
def handle_message(self): message = self.update.message User.populate_by_id(message.from_user.id, first_name=message.from_user.first_name, last_name=message.from_user.last_name, username=message.from_user.username) if not message.text: return text = message.text uid = str(message.chat.id) responding_to = memcache.get(uid) def deliver_poll(poll): backend.send_message(0.5, chat_id=uid, text=poll.render_text(), parse_mode='HTML', reply_markup=poll.build_admin_buttons()) if text.startswith('/start'): backend.send_message(chat_id=uid, text=self.NEW_POLL) memcache.set(uid, value='START', time=3600) return elif text == '/done' and responding_to and responding_to.startswith( 'OPT '): poll = Poll.get_by_id(int(responding_to[4:])) if not poll.options: backend.send_message(chat_id=uid, text=self.ERROR_PREMATURE_DONE) return backend.send_message(chat_id=uid, text=self.DONE) deliver_poll(poll) elif text == '/polls': header = [util.make_html_bold('Your polls')] recent_polls = Poll.query( Poll.admin_uid == uid).order(-Poll.created).fetch(50) body = [ u'{}. {}'.format(i + 1, poll.generate_poll_summary_with_link()) for i, poll in enumerate(recent_polls) ] footer = ['Use /start to create a new poll.'] output = u'\n\n'.join(header + body + footer) backend.send_message(chat_id=uid, text=output, parse_mode='HTML') elif text.startswith('/view_'): try: poll = Poll.get_by_id(int(text[6:])) if not poll or poll.admin_uid != uid: raise ValueError deliver_poll(poll) except ValueError: backend.send_message(chat_id=uid, text=self.HELP) elif responding_to == 'START': new_poll_key = Poll.new(admin_uid=uid, title=text).put() bold_title = util.make_html_bold_first_line(text) backend.send_message(chat_id=uid, text=self.FIRST_OPTION.format(bold_title), parse_mode='HTML') memcache.set(uid, value='OPT {}'.format(new_poll_key.id()), time=3600) return elif responding_to and responding_to.startswith('OPT '): poll = Poll.get_by_id(int(responding_to[4:])) poll.options.append(Option(text)) poll.put() if len(poll.options) < 10: backend.send_message(chat_id=uid, text=self.NEXT_OPTION) return backend.send_message(chat_id=uid, text=self.DONE) deliver_poll(poll) else: backend.send_message(chat_id=uid, text=self.HELP) memcache.delete(uid)
# flask imports from flask import Flask, render_template, request, redirect, url_for app = Flask(__name__) import sys # SQLAlchemy from model import Base, Post, Option from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker # setup app = Flask(__name__) engine = create_engine('sqlite:///project.db') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() post1 = Post(category="lifestyle", title="title", description='nune', votes=20) opts = Option(option="damn", pic_url="urlurlurl", opvote=12) session.add(post1) session.add(opts) session.commit() s = session.query(Post).filter_by(id=1).first() print(s.title)
app = Flask(__name__) app.config.from_object(Config) db.init_app(app) db.app = app db.drop_all() db.create_all() domain_name = 'chen.yunwei.space' admin_name = 'yunwei' admin_nickname = '云炜' admin_email = '*****@*****.**' base_pwd = '123456' admin_pwd = hashlib.sha256(base_pwd.encode(encoding='utf-8')).hexdigest() option_domain_name = Option(option_name='domain_name', option_value=domain_name) option_admin_name = Option(option_name='admin_name', option_value=admin_name) option_admin_nickname = Option(option_name='admin_nickname', option_value=admin_nickname) option_admin_email = Option(option_name='admin_email', option_value=admin_email) option_admin_pwd = Option(option_name='admin_pwd', option_value=admin_pwd) db.session.add(option_domain_name) db.session.add(option_admin_name) db.session.add(option_admin_nickname) db.session.add(option_admin_email) db.session.add(option_admin_pwd) db.session.commit()
def test_create(self): Option.create('test_key', 'test_value') with self.assertRaises(errors.OptionAlreadyExists): Option.create('test_key', 'test_value')
from model import Base, Post, Option from sqlalchemy import create_engine, desc from sqlalchemy.orm import sessionmaker engine = create_engine('sqlite:///project.db') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() option_1 = Option( option="Option 1", pic_url= "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTVIbDxcOrnASQRdLQUeudBCifn7D_nQGUpIeXsnNEaST_rDzv9VIcQPws" ) option_2 = Option( option="Option 2", pic_url= "http://www.prieteni.ro/uploads/albumfoto/396711_353820_wy_s_300x300.gif") new_post = Post(category='Category 1', title="Title 1", description="Description 1", options=[option_1, option_2])