views.py 5.03 KB
Newer Older
1
# Create your views here.
2

Jean Michel Rouly's avatar
Jean Michel Rouly committed
3
from stepup.models import *
Jean Michel Rouly's avatar
asdf    
Jean Michel Rouly committed
4
from django.shortcuts import render_to_response, get_object_or_404
Daniel W Bond's avatar
Daniel W Bond committed
5
6
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.core.urlresolvers import reverse
Ben Waters's avatar
Ben Waters committed
7
from django.contrib.auth import authenticate, login
Ben Waters's avatar
Ben Waters committed
8
from django.contrib.auth.decorators import login_required
Daniel W Bond's avatar
Daniel W Bond committed
9
10
from django.template import RequestContext

11
12
# other view functions

Ben Waters's avatar
Ben Waters committed
13
14
#login

Ben Waters's avatar
Ben Waters committed
15
def login_request(request):
Ben Waters's avatar
Ben Waters committed
16
17
18
19
20
21
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user is not None:
            login(request, user)
Ben Waters's avatar
Ben Waters committed
22
            # Redirect to a success page.
Jean Michel Rouly's avatar
Jean Michel Rouly committed
23
	    return render_to_response('/', {
Ben Waters's avatar
Ben Waters committed
24

Ben Waters's avatar
Ben Waters committed
25
26
27
28
        },
        )
        else:
            # Return an 'invalid login' error message.
Jean Michel Rouly's avatar
Jean Michel Rouly committed
29
            return render_to_response('/login', {
Ben Waters's avatar
Ben Waters committed
30
31
        },
        )
Ben Waters's avatar
Ben Waters committed
32
    else:
Jean Michel Rouly's avatar
Jean Michel Rouly committed
33
        return render_to_response('/login', {
Ben Waters's avatar
Ben Waters committed
34
35
        },
        )
Ben Waters's avatar
Ben Waters committed
36

37
# webpage-generating view functions
Ben Waters's avatar
Ben Waters committed
38
@login_required
39
40
def index(request):

41
    # opportunities filtered by user's tags, and in reverse chronological order
42

Daniel W Bond's avatar
Daniel W Bond committed
43
44
45
46
47
48
49
50
51
52
53
54
#    opportunities = Opportunity.objects.all().order_by("-posted")
#    paginator = Paginator(opportunities, 12)

#    try:
#         page = int(request.GET.get("page", '1'))
#    except ValueError:
#         page = 1

#    try: 
#        opportunities = paginator.page(page)
#    except (InvalidPage, EmptyPage):
#        opportunities = paginator.page(paginator.num_pages)
Daniel W Bond's avatar
Daniel W Bond committed
55

Jean Michel Rouly's avatar
fdsa    
Jean Michel Rouly committed
56
    current_user = Person.objects.get(user__username=request.user.username)
Jean Michel Rouly's avatar
asdf    
Jean Michel Rouly committed
57
58

    tag_opportunities = []
Jean Michel Rouly's avatar
fdsa    
Jean Michel Rouly committed
59
    current_tags = current_user.tags.all()
Jean Michel Rouly's avatar
asdf    
Jean Michel Rouly committed
60
61
62
63
64
    for tag in current_tags:
        opportunity = Opportunity.objects.filter(tags=tag).all()
        tag_opportunities.append( opportunity )

    org_opportunities = []
Jean Michel Rouly's avatar
fdsa    
Jean Michel Rouly committed
65
    current_orgs = current_user.organizations.all()
Jean Michel Rouly's avatar
asdf    
Jean Michel Rouly committed
66
67
68
    for org in current_orgs:
        opportunity = Opportunity.objects.filter(organizations=org).all()
        org_opportunities.append( opportunity )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
69
70
71
72
73
74
75
76
77
78
79

    tags_and_orgs = []
    global_tags = Tag.objects.all()
    for tag in global_tags:
        tags_and_orgs.append( tag )
    global_orgs = Organization.objects.all()
    for org in global_orgs:
        tags_and_orgs.append( org )

    global_opps = Opportunity.objects.all()

80
    return render_to_response('index.html', {
Jean Michel Rouly's avatar
fdsa    
Jean Michel Rouly committed
81
        "current_user" : current_user,
Jean Michel Rouly's avatar
asdf    
Jean Michel Rouly committed
82
83
        "tag_feed" : tag_opportunities,
        "org_feed" : org_opportunities,
Jean Michel Rouly's avatar
Jean Michel Rouly committed
84
        "tags_and_orgs_feed" : tags_and_orgs,
85
        "global_opp_feed" : global_opps,
86
    },
Daniel W Bond's avatar
Daniel W Bond committed
87
    context_instance = RequestContext(request),
88
    )
Daniel W Bond's avatar
Daniel W Bond committed
89

Ben Waters's avatar
Ben Waters committed
90
@login_required
Daniel W Bond's avatar
Daniel W Bond committed
91
def opportunity(request, slug):
92
    return render_to_response('opportunity.html', {
93
        # 'name' : 
94
    },
Daniel W Bond's avatar
Daniel W Bond committed
95
    context_instance = RequestContext(request),
96
    )
Daniel W Bond's avatar
Daniel W Bond committed
97

Ben Waters's avatar
Ben Waters committed
98
@login_required
Daniel W Bond's avatar
Daniel W Bond committed
99
def person(request, slug):
100
    return render_to_response('person.html', {
Jean Michel Rouly's avatar
asdf    
Jean Michel Rouly committed
101
102
103
104
105
106
107
108
109
110
111
        "requested_user" : Person.objects.get(user__username=slug),
        "requested_tags" : Person.objects.get(user__username=slug).tags.all(),
        #"global_tags" : Tag.objects.all(),
        "global_events" : Opportunity.objects.all(),
        "sunday" : len(Person.objects.get(user__username=slug).sunday),
        "monday" : len(Person.objects.get(user__username=slug).monday),
        "tuesday" : len(Person.objects.get(user__username=slug).tuesday),
        "wednesday" : len(Person.objects.get(user__username=slug).wednesday),
        "thursday" : len(Person.objects.get(user__username=slug).thursday),
        "friday" : len(Person.objects.get(user__username=slug).friday),
        "saturday" : len(Person.objects.get(user__username=slug).saturday),
112
    },
Daniel W Bond's avatar
Daniel W Bond committed
113
    context_instance = RequestContext(request),
114
    )
Daniel W Bond's avatar
Daniel W Bond committed
115

Ben Waters's avatar
Ben Waters committed
116
@login_required
Daniel W Bond's avatar
Daniel W Bond committed
117
def organization(request, slug):
118
    return render_to_response('organization.html', {
119
120
121
122
123
124
125
126
127
128
129
        "requested_org" : Organization.objects.get(user__username=slug),
        "requested_tags" : Organization.objects.get(user__username=slug).tags.all(),
        #"global_tags" : Tag.objects.all(),
        "global_events" : Opportunity.objects.all(),
        "sunday" : len(Organization.objects.get(user__username=slug).sunday),
        "monday" : len(Organization.objects.get(user__username=slug).monday),
        "tuesday" : len(Organization.objects.get(user__username=slug).tuesday),
        "wednesday" : len(Organization.objects.get(user__username=slug).wednesday),
        "thursday" : len(Organization.objects.get(user__username=slug).thursday),
        "friday" : len(Organization.objects.get(user__username=slug).friday),
        "saturday" : len(Organization.objects.get(user__username=slug).saturday),
130
    },
Daniel W Bond's avatar
Daniel W Bond committed
131
    context_instance = RequestContext(request),
132
    )
Jean Michel Rouly's avatar
Jean Michel Rouly committed
133

Daniel W Bond's avatar
michel    
Daniel W Bond committed
134
135
136
137
138
139
140
@login_required
def search(request):
    return render_to_response('search.html', {
    },
    context_instance = RequestContext(request),
    )

Abdullah Abdurrah's avatar
Abdullah Abdurrah committed
141
def about(request):
Jean Michel Rouly's avatar
Jean Michel Rouly committed
142
143
144
145
146
147
    return render_to_response('about.html', {
    # put the variables you need here
    },
    )

def logout_page(request, slug):
148
149
    logout(request)
    return render_to_response('login.html', {
Jean Michel Rouly's avatar
Jean Michel Rouly committed
150
151
152
    # put the variables you need here
    },
    )