示例#1
0
文件: models.py 项目: kyroskoh/Lab
def edit_poll(**kwargs):
    choices_arr = []
    poll = get_poll(str(kwargs.get('slug')))
    poll_dict = {}
    poll_dict['question'] = kwargs.get('question')
    for k, v in kwargs.items():
        if 'choice' not in k: continue
        this_choice = [
            c for c in poll.get('choices')
            if int(k.strip('choice')) == c.get('id')
        ]
        if not len(this_choice):
            return False
        else:
            this_choice = this_choice[0]
        choice_dict = {
            'id': this_choice.get('id'),
            'text': v,
            'value': this_choice.get('value'),
        }
        choices_arr.append(choice_dict)
    slug = str(kwargs.get('slug'))
    poll_dict['slug'] = slug
    poll_dict['choices'] = choices_arr
    set_value(slug, poll_dict)
    if kwargs.get('publish'):
        publish_poll(slug)
    else:
        unpublish_poll(slug)
    return poll_dict
示例#2
0
def publish_poll(key):
    published_polls = get_value('published_polls')
    if not published_polls:               
        set_value('published_polls',[])   
    if key not in published_polls:
        published_polls.append(key)
        set_value('published_polls',published_polls)
示例#3
0
def edit_poll(**kwargs):
    choices_arr = []
    poll = get_poll(str(kwargs.get('slug')))
    poll_dict = {}
    poll_dict['question'] = kwargs.get('question')
    for k,v in kwargs.items():
        if 'choice' not in k: continue
        this_choice = [c for c in poll.get('choices') if int(k.strip('choice')) == c.get('id')]
        if not len(this_choice):
            return False
        else:
            this_choice = this_choice[0]
        choice_dict = {
            'id': this_choice.get('id'),
            'text': v,
            'value': this_choice.get('value'),
        }
        choices_arr.append(choice_dict)
    slug = str(kwargs.get('slug'))
    poll_dict['slug'] = slug
    poll_dict['choices'] = choices_arr
    set_value(slug,poll_dict)
    if kwargs.get('publish'):
        publish_poll(slug)
    else:
        unpublish_poll(slug)
    return poll_dict
示例#4
0
def unpublish_polls(key):
    published_polls = get_value('published_polls')   
    if not published_polls:               
        set_value('published_polls',[])  
    if key in published_polls:
        published_polls.remove(key)
        set_value('published_polls',published_polls)
示例#5
0
def cast_vote(poll_key,choice):
    poll = get_value(poll_key)
    for c in poll['choices']:
        if c['id'] == int(choice):
            c['value'] += 1
    set_value(poll_key,poll)
    return poll
示例#6
0
文件: views.py 项目: kyroskoh/Lab
def unpublish_polls(key):
    published_polls = get_value('published_polls')
    if not published_polls:
        set_value('published_polls', [])
    if key in published_polls:
        published_polls.remove(key)
        set_value('published_polls', published_polls)
示例#7
0
文件: views.py 项目: kyroskoh/Lab
def publish_poll(key):
    published_polls = get_value('published_polls')
    if not published_polls:
        set_value('published_polls', [])
    if key not in published_polls:
        published_polls.append(key)
        set_value('published_polls', published_polls)
示例#8
0
文件: views.py 项目: kyroskoh/Lab
def cast_vote(poll_key, choice):
    poll = get_value(poll_key)
    for c in poll['choices']:
        if c['id'] == int(choice):
            c['value'] += 1
    set_value(poll_key, poll)
    return poll
示例#9
0
def get_polls():
    poll_list = []
    published_polls = get_value('published_polls')
    if not published_polls:
        set_value('published_polls',[])
    for p in published_polls:
        poll = get_value(p)
        poll_list.append(poll)
    return poll_list
示例#10
0
文件: views.py 项目: kyroskoh/Lab
def get_polls():
    poll_list = []
    published_polls = get_value('published_polls')
    if not published_polls:
        set_value('published_polls', [])
    for p in published_polls:
        poll = get_value(p)
        poll_list.append(poll)
    return poll_list
示例#11
0
def create_poll(**kwargs):
    """ This creates the initial poll question. It expects the following key,value pairs:
        question: the poll question
        answer: a list of strings that are the answer choices """
    poll_key = slugify(kwargs.get('question'))
    poll_value = {
        'question': kwargs.get('question'),
    }
    for a in kwargs.get('answer'):
        poll_value[a] = 0
    set_value(poll_key,poll_value)
    return poll_value
示例#12
0
文件: models.py 项目: kyroskoh/Lab
def add_poll(**kwargs):
    choices_arr = []
    count = 1
    poll_dict = {}
    poll_dict['question'] = kwargs.get('question')
    for k, v in kwargs.items():
        if 'choice' not in k: continue
        if not v: continue
        choice_dict = {'id': count, 'text': v, 'value': 0}
        choices_arr.append(choice_dict)
        count += 1
    slug = slugify(kwargs.get('question'))
    poll_dict['slug'] = slug
    poll_dict['choices'] = choices_arr
    set_value(slug, poll_dict)
    if kwargs.get('publish'):
        publish_poll(slug)
示例#13
0
def add_poll(**kwargs):
    choices_arr = []
    count = 1
    poll_dict = {}
    poll_dict['question'] = kwargs.get('question')
    for k,v in kwargs.items():
        if 'choice' not in k: continue
        choice_dict = {
            'id': count,
            'text': v,
            'value': 0
        }
        choices_arr.append(choice_dict)
        count += 1
    slug = slugify(kwargs.get('question')) 
    poll_dict['slug'] = slug
    poll_dict['choices'] = choices_arr
    set_value(slug,poll_dict)
    if kwargs.get('publish'):
        publish_poll(slug)