Exemple #1
0
def get_cart(sender):
    user = get_user(sender)
    if not user:
        return None

    try:
        cart = VoteCart.objects.get(user=user.id)
    except:
        cart = VoteCart(user=user, status=STATUS.ACTIVE)
        cart.save()
    return cart
def get_cart(sender):
    user = get_user(sender)
    if not user:
        return None

    try:
        cart = VoteCart.objects.get(user=user.id)
    except:
        cart = VoteCart(user=user, status=STATUS.ACTIVE)
        cart.save()
    return cart
Exemple #3
0
def api(request):
    categories = Category.objects.filter(type=TYPE.VOTED).order_by('id')
    teams = Team.objects.filter(status=TEAM_STATUS.ACTIVE).order_by('id')
    users = User.objects.filter(is_active=True).order_by('id')
    judged = Category.objects.filter(type=TYPE.JUDGED).order_by('id')

    saved = False

    try:
        online = VoteStatus.objects.all()[0].online
    except:
        online = False

    try:
        cart = VoteCart.objects.get(user=request.user.id)
    except:
        cart = VoteCart(user=request.user, status=STATUS.ACTIVE)
        cart.save()

    if request.method == 'POST':
        form = VoteForm(request.POST)
        if form.is_valid() and online:

            for key, team in form.cleaned_data.items():
                category_id = key.replace('cat_', '')
                category = Category.objects.get(id=category_id)
                _insert_or_update_vote(cart, category, team)
            saved = True

    vote_dict = {}
    votes = Vote.objects.filter(cart=cart.id)
    for vote in votes:
        vote_dict[vote.category.id] = vote.team.id

    env = {
        'categories': categories,
        'judged_categories': judged,
        'teams': teams,
        'users': users,
        'online': online,
        'votes': vote_dict,
        'saved': saved,
    }

    return HttpResponse(json.dumps(env, cls=DjangoJSONEncoder),
                        mimetype="application/json")
Exemple #4
0
def ipad_vote(request):
    logout(request)
    env = {}

    try:
        online = VoteStatus.objects.all()[0].online
    except:
        online = False

    if not online:
        return render(request, 'voting/vote_offline.html')

    error_message = None
    user = None
    votes = []

    if request.method == 'POST':
        form = VoteAuthorizeForm(request.POST)
        if form.is_valid():
            user = authenticate(username=form.cleaned_data['user_name'],
                                password=form.cleaned_data['password'])
            if user:
                try:
                    cart = VoteCart.objects.get(user=user.id)
                except:
                    cart = VoteCart(user=user, status=STATUS.ACTIVE)
                    cart.save()

                for key, team in form.cleaned_data.items():
                    if key.startswith('cat_'):
                        category_id = key.replace('cat_', '')
                        category = Category.objects.get(id=category_id)
                        _insert_or_update_vote(cart, category, team)
                        votes.append({'category': category, 'team': team})
                env = {'user': user}
                env['votes'] = votes
                return render(request, 'voting/ipad_vote_complete.html', env)
            else:
                error_message = "Bad username or password"
    else:
        form = VoteAuthorizeForm()

    env['form'] = form
    env['error_message'] = error_message
    return render(request, 'voting/ipad_vote.html', env)
Exemple #5
0
def ipad_vote(request):
    logout(request)
    env = {}

    try:
        online = VoteStatus.objects.all()[0].online
    except:
        online = False

    if not online:
        return render(request, 'voting/vote_offline.html');

    error_message = None
    user = None
    votes = []

    if request.method == 'POST':
        form = VoteAuthorizeForm(request.POST)
        if form.is_valid():
            user = authenticate(username=form.cleaned_data['user_name'],
                                password=form.cleaned_data['password'])
            if user:
                try:
                    cart = VoteCart.objects.get(user=user.id)
                except:
                    cart = VoteCart(user=user, status=STATUS.ACTIVE)
                    cart.save()

                for key,team in form.cleaned_data.items():
                    if key.startswith('cat_'):
                        category_id = key.replace('cat_', '')
                        category = Category.objects.get(id=category_id)
                        _insert_or_update_vote(cart, category, team)
                        votes.append({'category': category, 'team': team})
                env = {'user': user}
                env['votes'] = votes
                return render(request, 'voting/ipad_vote_complete.html', env);
            else:
                error_message = "Bad username or password"
    else:
        form = VoteAuthorizeForm()

    env['form'] = form
    env['error_message'] = error_message
    return render(request, 'voting/ipad_vote.html', env)
Exemple #6
0
def api(request):
    categories = Category.objects.filter(type=TYPE.VOTED).order_by('id')
    teams = Team.objects.filter(status=TEAM_STATUS.ACTIVE).order_by('id')
    users = User.objects.filter(is_active=True).order_by('id')
    judged = Category.objects.filter(type=TYPE.JUDGED).order_by('id')

    saved = False

    try:
        online = VoteStatus.objects.all()[0].online
    except:
        online = False

    try:
        cart = VoteCart.objects.get(user=request.user.id)
    except:
        cart = VoteCart(user=request.user, status=STATUS.ACTIVE)
        cart.save()

    if request.method == 'POST':
        form = VoteForm(request.POST)
        if form.is_valid() and online:

            for key,team in form.cleaned_data.items():
                category_id = key.replace('cat_', '')
                category = Category.objects.get(id=category_id)
                _insert_or_update_vote(cart, category, team)
            saved = True

    vote_dict = {}
    votes = Vote.objects.filter(cart=cart.id)
    for vote in votes:
        vote_dict[vote.category.id] = vote.team.id

    env = {'categories': categories,
           'judged_categories': judged,
           'teams': teams,
           'users': users,
           'online': online,
           'votes': vote_dict,
           'saved': saved,
           }

    return HttpResponse(json.dumps(env, cls=DjangoJSONEncoder),
                        mimetype="application/json")
Exemple #7
0
def vote(request):
    env = {}

    try:
        online = VoteStatus.objects.all()[0].online
    except:
        online = False

    if not online:
        return render(request, 'voting/vote_offline.html')

    try:
        cart = VoteCart.objects.get(user=request.user.id)
    except:
        cart = VoteCart(user=request.user, status=STATUS.ACTIVE)
        cart.save()

    # voting is complete don't allow more votes
    if cart.status == STATUS.COMPLETED:
        return render(request, 'voting/vote_complete.html')

    if request.method == 'POST':
        form = VoteForm(request.POST)
        if form.is_valid():

            for key, team in form.cleaned_data.items():
                category_id = key.replace('cat_', '')
                category = Category.objects.get(id=category_id)
                _insert_or_update_vote(cart, category, team)
            return render(request, 'voting/vote_complete.html')
    else:
        votes = Vote.objects.filter(cart=cart.id)
        form_init = {}
        for vote in votes:
            form_init['cat_%s' % vote.category.id] = vote.team.id
        form = VoteForm(form_init)

    env['form'] = form
    return render(request, 'voting/vote.html', env)
Exemple #8
0
def vote(request):
    env = {}

    try:
        online = VoteStatus.objects.all()[0].online
    except:
        online = False

    if not online:
        return render(request, 'voting/vote_offline.html');

    try:
        cart = VoteCart.objects.get(user=request.user.id)
    except:
        cart = VoteCart(user=request.user, status=STATUS.ACTIVE)
        cart.save()

    # voting is complete don't allow more votes
    if cart.status == STATUS.COMPLETED:
        return render(request, 'voting/vote_complete.html');

    if request.method == 'POST':
        form = VoteForm(request.POST)
        if form.is_valid():

            for key,team in form.cleaned_data.items():
                category_id = key.replace('cat_', '')
                category = Category.objects.get(id=category_id)
                _insert_or_update_vote(cart, category, team)
            return render(request, 'voting/vote_complete.html');
    else:
        votes = Vote.objects.filter(cart=cart.id)
        form_init = {}
        for vote in votes:
            form_init['cat_%s' % vote.category.id] = vote.team.id
        form = VoteForm(form_init)

    env['form'] = form
    return render(request, 'voting/vote.html', env)