Exemplo n.º 1
0
def home(request):
    context = {}
    if request.user.is_anonymous():
        now = datetime.datetime.now()
        blog_date = blog_title = blog_content = None
        competition = Competition.objects\
                      .filter(start_date__lt=now,
                              close_date__gt=now)\
                      .order_by('start_date')
        if competition:
            competition = competition[0]
            predictions = Prediction.objects\
                          .filter(competition=competition)\
                          .all()[:10]
        else:
            competition = None
            predictions = None
        context['blog_date'] = blog_date
        context['blog_title'] = blog_title
        context['blog_content'] = blog_content
        context['competition'] = competition
        context['predictions'] = predictions
        if request.method == "POST":
            # login attempt
            if request.POST.get('login', ''):
                username = request.POST['email']
                password = request.POST['password']
                user = authenticate(username=username, password=password)
                if user and not user.is_anonymous():
                    login(request, user)
                    return redirect(reverse('home'))
                else:
                    error = "Sorry, your details weren't recognised"
                    context = {'error': error}
                    return redirect(addToQueryString(reverse('home'), context))
            else:
                # signup attempt
                form = PredictionForm(request.POST, request.FILES)
                if form.is_valid():
                    prediction = form.save()
                    request.session['prediction'] = prediction
                    request.session['competition'] = competition
                    return redirect(reverse('signup'))
                else:
                    context['form'] = form
        else:
            # default homepage
            context['form'] = PredictionForm()
        return render_with_context(request, 'home.html', context)
    else:
        return redirect(reverse('logged_in'))
Exemplo n.º 2
0
def make_prediction(request, competition=None):
    if not competition:
        competition = Competition.objects\
                      .get(pk=settings.CURRENT_COMPETITION_ID)
    competition = Competition.objects.get(pk=competition)
    has_password = request.user.has_usable_password()
    current_prediction = Prediction.objects\
                         .filter(competition=competition,
                                 user=request.user)\
                         .order_by('-created_date')
    current_prediction = current_prediction.count()\
                         and current_prediction[0] or None
    now = datetime.datetime.now()
    top_predictions = Prediction.objects\
                      .filter(competition=competition)
    if current_prediction:
        top_predictions = top_predictions\
                          .exclude(pk=current_prediction.pk)
    top_predictions = top_predictions[:3]
    if not competition.is_open():
        error = "This competition is now closed"
    if request.method == "POST":
        if has_password:
            form = PredictionForm(request.POST,
                                  request.FILES,
                                  default_table=current_prediction)
        else:
            form = PredictionPasswordForm(request.POST,
                                          request.FILES,
                                          default_table=current_prediction)
        if form.is_valid():
            this_year = datetime.datetime(settings.CURRENT_SEASON, 1, 1)
            prediction = form.save()
            saving = request.POST.get('save', '')
            prediction_obj = Prediction.objects.get_or_create(
                user=request.user,
                name=request.user.email,
                competition=competition)[0]
            prediction_obj.teams.clear()
            for t_id in prediction:
                prediction_obj.teams.add(Team.objects.get(pk=t_id))
            prediction_obj.edited_date = now
            prediction_obj.save()
            score = prediction_obj.calculateScore(force=True)
            goaldiff = prediction_obj.calculateGoalDiff(force=True)
            position = prediction_obj.calculatePosition()
            prediction_obj.position = position

            if not has_password:
                request.user.set_password(form.cleaned_data['password1'])
                request.user.save()
            if saving:
                transaction.commit()
                return redirect(
                    reverse('logged_in') + '#comp-%s' % competition.pk)
            else:
                transaction.rollback()

    else:
        if has_password:
            form = PredictionForm(default_table=current_prediction)
        else:
            form = PredictionPasswordForm(default_table=current_prediction)
    return locals()