Exemplo n.º 1
0
def vote_list(request):
    if not settings.VOTING_ENABLED:
        scores = []
        current_place = 0
        current_votes = sys.maxint
        for ts in TotalScore.objects.order_by('-value'):
            if ts.value < current_votes:
                current_votes = ts.value
                current_place += 1
            scores.append((current_place, current_votes, ts.content_object))
        template = 'app/results.html'
        return locals()
    if 'true' in (request.META.get('HTTP_X_PJAX'), request.GET.get('_pjax')):
        template = 'app/vote_list_pjax.html'
    else:
        template = 'app/vote_list.html'
    voter = request.user.get_profile()
    ct = ContentType.objects.get_for_model(Element)
    voter_votes = {v[1]: v[0] for v in Vote.objects.filter(voter=voter,
            total_score__content_type=ct).values_list(
                    'id', 'total_score__object_id')}
    votes_left = settings.VOTES_PER_USER - len(voter_votes)
    try:
        element = Element.objects.get(pk=int(request.GET['v']))
        if element.speakers.filter(pk=voter.id).count():
            raise ValueError("Cannot vote for yourself.")
        if votes_left <= 0:
            raise ValueError("No more votes left.")
        score_old = TotalScore.get_value(element, voter, ct=ct)
        score = TotalScore.update(element, voter, 1, ct=ct)
        vote = Vote.objects.get(voter=voter, total_score__content_type=ct,
                total_score__object_id=element.id)
        voter_votes[element.id] = vote.id
        if score_old != score:
            votes_left -= 1
    except (KeyError, ValueError, TypeError, Element.DoesNotExist):
        pass
    try:
        vote = Vote.objects.get(pk=int(request.GET['x']))
        if voter != vote.voter:
            raise ValueError("Cannot erase votes by others.")
        element = vote.total_score.content_object
        score_old = TotalScore.get_value(element, voter, ct=ct)
        score = TotalScore.update(element, voter, -vote.value, ct=ct)
        del voter_votes[element.id]
        if score_old != score:
            votes_left += 1
    except (KeyError, ValueError, TypeError, Vote.DoesNotExist):
        pass
    already_used = set()
    profiles = []
    for elem in Element.objects.order_by('speakers__user__last_name'):
        if elem.speakers.filter(pk=voter.id).count() or elem.id in already_used:
            continue
        already_used.add(elem.id)
        profiles.append((voter_votes.get(elem.id), elem,
                         TotalScore.get_value(elem, ct=ct)))
    return locals()
Exemplo n.º 2
0
def update_score(request, content_type, object_id, value, voter=None):
    if not voter:
        voter_model = Vote.voter.field.rel.to
        if voter_model is User:
            voter = request.user
        else:
            voter = request.user.get_profile()
            if voter_model is not voter.__class__:
                raise ImproperlyConfigured("voter not passed to the"
                    "`update_score()` view. Write a `process_view()` "
                    "middleware to pass it. This is not required if the voter "
                    "model is `User` or `user_instance.get_profile()`.")
    ct = get_object_or_404(ContentType, pk=int(content_type))
    obj = get_object_or_404(ct.model_class(), pk=int(object_id))
    score = TotalScore.update(obj, voter, int(value), ct=ct)
    return redirect(request, reverse('lckd-score:show',
        args=[int(content_type), int(object_id)]))
Exemplo n.º 3
0
def update_score(request, content_type, object_id, value, voter=None):
    if not voter:
        voter_model = Vote.voter.field.rel.to
        if voter_model is User:
            voter = request.user
        else:
            voter = request.user.get_profile()
            if voter_model is not voter.__class__:
                raise ImproperlyConfigured(
                    "voter not passed to the"
                    "`update_score()` view. Write a `process_view()` "
                    "middleware to pass it. This is not required if the voter "
                    "model is `User` or `user_instance.get_profile()`.")
    ct = get_object_or_404(ContentType, pk=int(content_type))
    obj = get_object_or_404(ct.model_class(), pk=int(object_id))
    score = TotalScore.update(obj, voter, int(value), ct=ct)
    return redirect(
        request,
        reverse('lckd-score:show', args=[int(content_type),
                                         int(object_id)]))
Exemplo n.º 4
0
def show_score(request, content_type, object_id):
    template = 'score/show.html'
    ct = get_object_or_404(ContentType, pk=content_type)
    obj = get_object_or_404(ct.model_class(), pk=object_id)
    score = TotalScore.get_value(obj, ct=ct)
    return locals()
Exemplo n.º 5
0
def show_score(request, content_type, object_id):
    template = 'score/show.html'
    ct = get_object_or_404(ContentType, pk=content_type)
    obj = get_object_or_404(ct.model_class(), pk=object_id)
    score = TotalScore.get_value(obj, ct=ct)
    return locals()