Fakultas Ilmu Komputer UI

views.py 2.1 KB
Newer Older
1
from django.http import HttpResponse
Rayza Arasj Mahardhika's avatar
Rayza Arasj Mahardhika committed
2
from django.shortcuts import redirect, render
Rayza Arasj Mahardhika's avatar
Rayza Arasj Mahardhika committed
3
from django.core.mail import send_mail
4
from django.core.exceptions import ValidationError
5
from lists.models import Item, List
6

7
8
9
10
11
12
13
14
15
16
17
18
COMMENTARY_ZERO = 'yey, waktunya berlibur'
COMMENTARY_LESS_THAN_FIVE = 'sibuk tapi santai'
COMMENTARY_MORE_THAN_EQUAL_FIVE = 'oh tidak'

def get_commentary(item_count):
    if (item_count == 0):
        return COMMENTARY_ZERO
    elif (item_count < 5):
        return COMMENTARY_LESS_THAN_FIVE
    elif (item_count >= 5):
        return COMMENTARY_MORE_THAN_EQUAL_FIVE

19
def home_page(request):
20
    return render(request, 'home.html')
Rayza Arasj Mahardhika's avatar
Rayza Arasj Mahardhika committed
21
22
23
24
25

def about_page(request):
    return HttpResponse(
        '<html>' +
            '<title>Rayza\'s About Page</title>' +
26
            '<h1>Here\'s a few facts about me</h1>' +
Rayza Arasj Mahardhika's avatar
Rayza Arasj Mahardhika committed
27
            '<p>I\'m 21 years old</p>' +
28
29
            '<p>I\'m from Bogor</p>' +
            '<p>I\'m currently studying Computer Science at Faculty of Computer Science, Universitas Indonesia</p>' +
Rayza Arasj Mahardhika's avatar
Rayza Arasj Mahardhika committed
30
31
        '</html>'
    )
32

Rayza Arasj Mahardhika's avatar
Rayza Arasj Mahardhika committed
33
def view_list(request, list_id, category=None):
34
    list_ = List.objects.get(id=list_id)
Rayza Arasj Mahardhika's avatar
Rayza Arasj Mahardhika committed
35
    items = Item.objects.filter(list=list_)
36
37
    error = None

Rayza Arasj Mahardhika's avatar
Rayza Arasj Mahardhika committed
38
39
40
    if category in ['work', 'study', 'life']:
        items = Item.objects.filter(list=list_, category=category.capitalize())

41
42
    if request.method == 'POST':
        try:
Rayza Arasj Mahardhika's avatar
Rayza Arasj Mahardhika committed
43
            item = Item(text=request.POST['item_text'], category=request.POST['item_category'], list=list_)
44
45
46
47
48
49
            item.full_clean()
            item.save()
            return redirect(list_)
        except ValidationError:
            error = "You can't have an empty list item"

Rayza Arasj Mahardhika's avatar
Rayza Arasj Mahardhika committed
50
    return render(request, 'list.html', {'list': list_, 'items': items, 'error': error})
51
52
53

def new_list(request):
    list_ = List.objects.create()
Rayza Arasj Mahardhika's avatar
Rayza Arasj Mahardhika committed
54
    item = Item(text=request.POST['item_text'], category=request.POST['item_category'], list=list_)
55
56
57
58
59
60
61
62
    try:
        item.full_clean()
        item.save()
    except ValidationError:
        list_.delete()
        error = "You can't have an empty list item"
        return render(request, 'home.html', {'error': error})
    return redirect(list_)