async def select_topics(self, ctx, length): def check(msg): if (msg.author == ctx.author) and (msg.channel == ctx.channel): return True else: return False available_topics = Topics(deepcopy(Topics.all_topics)) result_list = [] while len(result_list) < length: await ctx.send( 'Type the number corresponding to the topic to select it:\n{0}' .format(available_topics.pprint())) msg = await self.wait_for('message', check=check) index = util.str_to_int(msg.content) if index > len(available_topics) - 1: raise ValueError( "Your selection must be between 1 and {0}".format( len(available_topics) - 1)) result_list.append(available_topics[index]) await ctx.send( f'**Topic {len(result_list)}: {available_topics[index]}**') available_topics.remove(index) await ctx.send(f'**Your selections are: {", ".join(result_list)}**') return result_list
def add_topic(): """"Adds a topic""" error = None if request.method == 'POST': content = request.form['content'] if not content: return "content cannot be Empty" topic = Topics() topic.content = content db.session.add(topic) db.session.commit() return jsonify(data=content)
def api_polls(): if request.method == 'POST': # get the poll and save it in the database poll = request.get_json() # simple validation to check if all values are properly secret for key, value in poll.items(): if not value: return jsonify({'error': 'value for {} is empty'.format(key)}) title = poll['title'] options_query = lambda option: \ Options.query.filter(Options.name.like(option)) options = [ Polls(option=Options( name=option)) if options_query(option).count() == 0 else Polls( option=options_query(option).first()) for option in poll['options'] ] new_topic = Topics(title=title, options=options) db.session.add(new_topic) db.session.commit() return jsonify({'message': 'Poll was created succesfully'}) else: # it's a GET request, return dict representations of the API polls = Topics.query.join(Polls).all() all_polls = {'Polls': [poll.to_json() for poll in polls]} return jsonify(all_polls)
def api_polls(): if request.method == 'POST': #Get poll and save in the database poll = request.get_json() for key, value in poll.items(): if not value: return jsonify( {'message': 'value for {} is empty'.format(key)}) title = poll['title'] options_query = lambda option: Options.query.filter( Options.name.like(option)) options = [ Polls(option=Options( name=option)) if options_query(option).count() == 0 else Polls( option=options_query(option).first()) for option in poll['options'] ] new_topic = Topics(title=title, options=options) db.session.add(new_topic) db.session.commit() return jsonify({'message': 'Poll was successfully created!'}) else: #Query the database and return all polls as JSON objects polls = Topics.query.join(Polls).order_by(Topics.id.desc()).all() all_polls = {'Polls': [poll.to_json() for poll in polls]} return jsonify(all_polls)
def create(): if request.method == "GET": topic_id = ''.join(random.choices(string.ascii_uppercase + string.digits, k=5)) topic_temp = Topics(topic_id) db.session.add(topic_temp) db.session.commit() session['topic_id'] = topic_id else: topic_id = session.get('topic_id') return render_template("create.html", topic_id=topic_id, options={})
def insert_action(id): data = request.form['topic'] #gets topic name data2 = request.form['message'] #gets message user = User.query.filter_by(id=id).first_or_404() #print(user.id) username = user.username newTopic = Topics(topicName=data, topicMessage=data2, topicCreator=username) try: db.session.add(newTopic) db.session.commit() except IntegrityError: db.session.rollback() return render_template('notcreated.html') return render_template('created.html')
def api_polls(): if request.method == 'POST': # get the poll and save it in the database poll = request.get_json() # simple validation to check if all values are properly set for key, value in poll.items(): if not value: return jsonify( {'message': 'value for {} is empty'.format(key)}) title = poll['title'] # return option that matches a given name def options_query(option): return Options.query.filter(Options.name.like(option)) options = [ Polls(option=Options( name=option)) if options_query(option).count() == 0 else Polls( option=options_query(option).first()) for option in poll['options'] ] eta = datetime.utcfromtimestamp(poll['close_date']) new_topic = Topics(title=title, options=options, close_date=eta) db.session.add(new_topic) db.session.commit() # run the task from tasks import close_poll close_poll.apply_async((new_topic.id, env['SQLALCHEMY_DATABASE_URI']), eta=eta) return jsonify({'message': 'Poll was created succesfully'}) else: # it's a GET request, return dict representations of the API polls = Topics.query.filter_by(status=True).join(Polls)\ .order_by(Topics.id.desc()).all() all_polls = {'Polls': [poll.to_json() for poll in polls]} return jsonify(all_polls)
def api_polls(): if request.method == 'POST': poll = request.get_json() for key, value in poll.items(): if not value: return jsonify({"message": "ERROR: value for {} is empty".format(key)}) title = poll['title'] options_query = lambda option : Options.query.filter(Options.name.like(option)) options = [Polls(option=Options(name=option)) if options_query(option)==0 else Polls(option=options_query(option).first()) for option in poll['options']] new_topic = Topics(title=title, options=options) db.session.add(new_topic) db.session.commit() return jsonify({'message':'Poll was created successfuly'}) else: polls = Topics.query.join(Polls).all() all_polls = {'Polls': [poll.to_json() for poll in polls]} return jsonify(all_polls)
def api_polls(): if request.method == "POST": # get the poll and save it in the database poll = request.get_json() # simple validation to check if all values are properly secret for key, value in poll.items(): if not value: return jsonify( {"message": "value for {} is empty".format(key)}) title = poll["title"] options_query = lambda option: Options.query.filter( Options.name.like(option)) options = [ Polls(option=Options( name=option)) if options_query(option).count() == 0 else Polls( option=options_query(option).first()) for option in poll["options"] ] new_topic = Topics(title=title, options=options) db.session.add(new_topic) db.session.commit() # run the task from tasks import close_poll eta = datetime.utcfromtimestamp(poll['close_date']) close_poll.apply_async((new_topic.id), eta=eta) return jsonify({"message": "Poll was created succesfully"}) else: # it's a GET request, return dict representations of the API polls = (Topics.query.filter_by(status=1).join(Polls).order_by( Topics.id.desc()).all()) all_polls = {"Polls": [poll.to_json() for poll in polls]} return jsonify(all_polls)
def api_polls(): if request.method == 'POST': # get the poll and save it in the database poll = request.get_json() # simple validation to check if all values are properly secret for key, value in poll.items(): if not value: return jsonify( {'message': 'value for {} is empty'.format(key)}) title = poll['title'] options_query = lambda option: Options.query.filter( Options.name.like(option)) """The list comprehension reads as follows Make a Poll object out of an option if that option doesn’t exist in the database (count == 0), if exists in the database (else) then get that option and make a Poll object from it. """ options = [ Polls(option=Options( name=option)) if options_query(option).count() == 0 else Polls( option=options_query(option).first()) for option in poll['options'] ] eta = datetime.utcfromtimestamp(poll['close_date']) new_topic = Topics(title=title, options=options, close_date=eta) db.session.add(new_topic) db.session.commit() # run the task from tasks import close_poll close_poll.apply_async((new_topic.id, ), eta=eta) return jsonify({'message': 'Poll was created successfully'}) else: # it's a GET request, return dict representations of the API polls = Topics.query.filter_by(status=True).join(Polls).order_by( Topics.id.desc()).all() all_polls = {'Polls': [poll.to_json() for poll in polls]} return jsonify(all_polls)
def api_polls(): if request.method == 'POST': poll = request.get_json() for key, value in poll.items(): if not value: return jsonify({'message': 'vacia'.format(key)}) title = poll['title'] options_query = lambda option: Options.query.filter( Options.name.like(option)) options = [ Polls(option=Options( name=option)) if options_query(option).count() == 0 else Polls( option=options_query(option).first()) for option in poll['txtOptions'] ] try: new_topic = Topics(title=title, options=options) db.session.add(new_topic) db.session.commit() except exc.SQLAlchemyError: db.session.rollback() return jsonify({ 'message': 'Existe un problema con nuestra base de datos, favor de intentarlo mas tarde' }) raise finally: return jsonify({'message': 'Encuesta creada correctamente'}) else: polls = Topics.query.join(Polls).all() all_polls = {'Polls': [poll.to_json() for poll in polls]} return jsonify(all_polls)
if request.method == 'POST': # get the poll and save it in the database poll = request.get_json() # simple validation to check if all values are properly secret for key, value in poll.items(): if not value: return jsonify({'message': 'value for {} is empty'.format(key)}) title = poll['title'] options_query = lambda option:Options.query.filter(Options.name.like(option)) options = [Polls(option=Options(name=option)) if options_query(option).count() == 0 else Polls(option=options_query(option).first()) for option in poll['options'] ] new_topic = Topics(title=title, options=options) db.session.add(new_topic) db.session.commit() return jsonify({'message': 'Poll was created succesfully'}) else: # it's a GET request, return dict representations of the API polls = Topics.query.filter_by(status=1).join(Polls).order_by(Topics.id.desc()).all() all_polls = {'Polls': [poll.to_json() for poll in polls]} return jsonify(all_polls) @api.route('/polls/options') def api_polls_options(): all_options = [option.to_json() for option in Options.query.all()]
from app import db, app db.app = app db.init_app(app) db.create_all() from models import Options, Topics, Polls topic = Topics(title='Which side is going to win the EPL this season') arsenal = Options(name='Arsenal') spurs = Options(name='Spurs') poll = Polls(topic=topic, option=arsenal) poll1 = Polls(topic=topic, option=spurs) db.session.add(topic) db.session.commit() city = Options(name='Manchester city') liverpool = Options(name='Liverpool FC') liverpool = Polls(option=liverpool) city = Polls(option=city) new_topic = Topics(title='Whos better liverpool or city', options=[liverpool, city]) db.session.add(new_topic) db.session.commit()
def api_polls(): if request.method == 'POST': # get the poll and save it in the database poll = request.get_json() # simple validation to check if all values are properly set for key, value in poll.items(): if not value: return jsonify( {'message': 'value for {} is empty'.format(key)}) title = poll['title'] batch = poll['batch'] options_query = lambda option: Options.query.filter( Options.name.like(option)) options = [ Polls(option=Options( name=option)) if options_query(option).count() == 0 else Polls( option=options_query(option).first()) for option in poll['options'] ] eta = datetime.utcfromtimestamp(poll['close_date']) new_topic = Topics(title=title, poll_group=batch, options=options, close_date=eta) #poll_group = Polls() #db.session.add(poll_group) db.session.add(new_topic) db.session.commit() # run the task from tasks import close_poll close_poll.apply_async((new_topic.id, SQLALCHEMY_DATABASE_URI), eta=eta) return jsonify({'message': 'Poll was created succesfully'}) else: # it's a GET request, return dict representations of the API user = session['user'] # print('User:'******'NFKD', user).encode('ascii','ignore') # print('UpdatedUser:'******'User Info: ', user_info) # user_type = unicodedata.normalize('NFKD', user_info.user_group).encode('ascii','ignore') user_type = user_info.user_group polls = Topics.query.filter_by( status=True, poll_group=user_type).join(Polls).order_by(Topics.id.desc()).all() all_polls = Topics.query.filter_by( status=True, poll_group='all').join(Polls).order_by(Topics.id.desc()).all() for x in all_polls: polls.append(x) print(type(polls)) all_polls = {'Polls': [poll.to_json() for poll in polls]} return jsonify(all_polls)