views.py 1.2 KB
Newer Older
Andreas Berger's avatar
Andreas Berger committed
1
2
3
4
from django.shortcuts import render, redirect

from projects.models import Project

Ola Hermann Opheim's avatar
autopep    
Ola Hermann Opheim committed
5

Andreas Berger's avatar
Andreas Berger committed
6
def home(request):
Ola Hermann Opheim's avatar
Ola Hermann Opheim committed
7
    if request.user.is_authenticated:
Andreas Berger's avatar
Andreas Berger committed
8
        user = request.user
Ola Hermann Opheim's avatar
autopep    
Ola Hermann Opheim committed
9
10
11
        user_projects = Project.objects.filter(user=user.profile)
        customer_projects = list(Project.objects.filter(
            participants__id=user.id).order_by().distinct())
Andreas Berger's avatar
Andreas Berger committed
12
13
14
15
16
17
18
        for team in user.profile.teams.all():
            customer_projects.append(team.task.project)
        cd = {}
        for customer_project in customer_projects:
            cd[customer_project.id] = customer_project

        customer_projects = cd.values()
Ola Hermann Opheim's avatar
autopep    
Ola Hermann Opheim committed
19
20
        given_offers_projects = Project.objects.filter(
            pk__in=get_given_offer_projects(user)).distinct()
Andreas Berger's avatar
Andreas Berger committed
21
        return render(
Ola Hermann Opheim's avatar
autopep    
Ola Hermann Opheim committed
22
23
24
25
26
27
28
            request,
            'index.html',
            {
                'user_projects': user_projects,
                'customer_projects': customer_projects,
                'given_offers_projects': given_offers_projects,
            })
Ola Hermann Opheim's avatar
Ola Hermann Opheim committed
29
    return redirect('projects')
Andreas Berger's avatar
Andreas Berger committed
30

Ola Hermann Opheim's avatar
autopep    
Ola Hermann Opheim committed
31

Andreas Berger's avatar
Andreas Berger committed
32
33
34
35
36
37
38
def get_given_offer_projects(user):
    project_ids = set()

    for taskoffer in user.profile.taskoffer_set.all():
        project_ids.add(taskoffer.task.project.id)

    return project_ids