| author | cavaliet |
| Fri, 08 Feb 2013 16:50:23 +0100 | |
| changeset 30 | a96a795aa08f |
| parent 29 | 333d128cc1ae |
| child 36 | daa526d27044 |
| permissions | -rw-r--r-- |
| 9 | 1 |
from django.conf import settings |
| 19 | 2 |
from django.contrib.auth.decorators import login_required |
|
24
d65e05154aec
All pictures view with intelligent pagination. Css need to be improved.
cavaliet
parents:
23
diff
changeset
|
3 |
from django.core.paginator import Paginator, InvalidPage, EmptyPage |
|
23
bb7819c8d7c2
first step user login/logout. Create fragment works. Views and templates adapted with real data model.
cavaliet
parents:
20
diff
changeset
|
4 |
from django.shortcuts import get_object_or_404, render_to_response, redirect |
| 9 | 5 |
from django.template import RequestContext |
|
23
bb7819c8d7c2
first step user login/logout. Create fragment works. Views and templates adapted with real data model.
cavaliet
parents:
20
diff
changeset
|
6 |
from models import Image, Fragment |
|
25
48614929b87a
Real sort on home. Real pict's fragment on other pages. Real links on intelligent pagination.
cavaliet
parents:
24
diff
changeset
|
7 |
from django.db.models.aggregates import Max |
| 29 | 8 |
import logging |
| 9 | 9 |
|
| 29 | 10 |
logger = logging.getLogger(__name__) |
| 9 | 11 |
|
12 |
||
13 |
def home(request): |
|
14 |
||
| 20 | 15 |
# if the request has a "search" get parameter, we search this term |
16 |
search = None |
|
17 |
nb_results = 1 |
|
18 |
if "search" in request.GET: |
|
19 |
search = request.GET["search"] |
|
20 |
field = "all" |
|
21 |
if "field" in request.GET: |
|
22 |
field = request.GET["field"] |
|
|
25
48614929b87a
Real sort on home. Real pict's fragment on other pages. Real links on intelligent pagination.
cavaliet
parents:
24
diff
changeset
|
23 |
|
| 29 | 24 |
# We force list() because if not, img_id_list is a valuelistqueryset and not a list of values. |
25 |
# We separate image requests and make requests with select_related to optimize database hits. |
|
26 |
img_id_list = list(Image.objects.values_list('id', flat=True).annotate(date_modif=Max('fragment__date_saved')).exclude(date_modif=None).order_by('-date_modif')[:12]) |
|
27 |
img_list = Image.objects.filter(id__in=img_id_list).select_related('info', 'metadata') |
|
28 |
frg_list = Fragment.objects.all().order_by('-date_saved').select_related('image', 'image__info', 'image__metadata','author')[:12] |
|
| 17 | 29 |
|
| 9 | 30 |
return render_to_response("egonomy_home.html", |
| 20 | 31 |
{'img_list':img_list, 'fragment_list':frg_list, "search":search, "nb_results":nb_results}, |
| 9 | 32 |
context_instance=RequestContext(request)) |
33 |
||
| 15 | 34 |
|
|
23
bb7819c8d7c2
first step user login/logout. Create fragment works. Views and templates adapted with real data model.
cavaliet
parents:
20
diff
changeset
|
35 |
def annotate_picture(request, image_id): |
|
18
ffd106d9b8e1
Prepare template for real datas. Some fake datas temporary generated in views.
cavaliet
parents:
17
diff
changeset
|
36 |
|
| 30 | 37 |
img = get_object_or_404(Image.objects.select_related('info', 'metadata'), id=image_id) |
38 |
||
39 |
# We force list() to get only one database hit instead of 3 (first request, count, [0]) |
|
40 |
frg_list = list(Fragment.objects.filter(image=img).order_by('-date_saved').select_related('image', 'image__info', 'image__metadata','author')) |
|
|
27
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
41 |
last_frg = None |
| 30 | 42 |
if len(frg_list)>0: |
|
27
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
43 |
last_frg = frg_list[0] |
| 9 | 44 |
|
45 |
return render_to_response("egonomy_annotate_picture.html", |
|
|
27
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
46 |
{'img': img, 'fragment_list': frg_list, 'last_frg':last_frg}, |
| 9 | 47 |
context_instance=RequestContext(request)) |
48 |
||
49 |
||
|
23
bb7819c8d7c2
first step user login/logout. Create fragment works. Views and templates adapted with real data model.
cavaliet
parents:
20
diff
changeset
|
50 |
def view_fragment(request, fragment_pk): |
|
bb7819c8d7c2
first step user login/logout. Create fragment works. Views and templates adapted with real data model.
cavaliet
parents:
20
diff
changeset
|
51 |
|
| 30 | 52 |
frg = get_object_or_404(Fragment.objects.select_related('image', 'image__info', 'image__metadata','author'), pk=fragment_pk) |
53 |
frg_list = Fragment.objects.filter(image=frg.image).select_related('image', 'image__info', 'image__metadata','author') |
|
|
18
ffd106d9b8e1
Prepare template for real datas. Some fake datas temporary generated in views.
cavaliet
parents:
17
diff
changeset
|
54 |
|
| 15 | 55 |
return render_to_response("egonomy_view_fragment.html", |
|
18
ffd106d9b8e1
Prepare template for real datas. Some fake datas temporary generated in views.
cavaliet
parents:
17
diff
changeset
|
56 |
{'fragment': frg, 'fragment_list': frg_list}, |
| 15 | 57 |
context_instance=RequestContext(request)) |
58 |
||
|
24
d65e05154aec
All pictures view with intelligent pagination. Css need to be improved.
cavaliet
parents:
23
diff
changeset
|
59 |
@login_required |
|
23
bb7819c8d7c2
first step user login/logout. Create fragment works. Views and templates adapted with real data model.
cavaliet
parents:
20
diff
changeset
|
60 |
def create_fragment(request, image_id): |
|
bb7819c8d7c2
first step user login/logout. Create fragment works. Views and templates adapted with real data model.
cavaliet
parents:
20
diff
changeset
|
61 |
|
| 30 | 62 |
img = get_object_or_404(Image.objects.select_related('info', 'metadata'), id=image_id) |
63 |
frg_list = Fragment.objects.filter(image=img).order_by('-date_saved').select_related('image', 'image__info', 'image__metadata','author') |
|
|
18
ffd106d9b8e1
Prepare template for real datas. Some fake datas temporary generated in views.
cavaliet
parents:
17
diff
changeset
|
64 |
|
| 15 | 65 |
return render_to_response("egonomy_create_fragment.html", |
|
23
bb7819c8d7c2
first step user login/logout. Create fragment works. Views and templates adapted with real data model.
cavaliet
parents:
20
diff
changeset
|
66 |
{'img': img, 'fragment_list': frg_list}, |
| 15 | 67 |
context_instance=RequestContext(request)) |
68 |
||
|
24
d65e05154aec
All pictures view with intelligent pagination. Css need to be improved.
cavaliet
parents:
23
diff
changeset
|
69 |
@login_required |
| 19 | 70 |
def save_fragment(request): |
71 |
||
72 |
frg_title = request.POST["fragment_title"] |
|
73 |
frg_desc = request.POST["fragment_description"] |
|
74 |
frg_kw = request.POST["users_keywords"] |
|
75 |
frg_path = request.POST["fragment_path"] |
|
|
23
bb7819c8d7c2
first step user login/logout. Create fragment works. Views and templates adapted with real data model.
cavaliet
parents:
20
diff
changeset
|
76 |
frg_image_id = request.POST["image_id"] |
|
bb7819c8d7c2
first step user login/logout. Create fragment works. Views and templates adapted with real data model.
cavaliet
parents:
20
diff
changeset
|
77 |
img = get_object_or_404(Image, id=frg_image_id) |
| 19 | 78 |
|
|
23
bb7819c8d7c2
first step user login/logout. Create fragment works. Views and templates adapted with real data model.
cavaliet
parents:
20
diff
changeset
|
79 |
frg = Fragment() |
|
bb7819c8d7c2
first step user login/logout. Create fragment works. Views and templates adapted with real data model.
cavaliet
parents:
20
diff
changeset
|
80 |
frg.image = img |
|
bb7819c8d7c2
first step user login/logout. Create fragment works. Views and templates adapted with real data model.
cavaliet
parents:
20
diff
changeset
|
81 |
frg.coordinates = frg_path |
|
bb7819c8d7c2
first step user login/logout. Create fragment works. Views and templates adapted with real data model.
cavaliet
parents:
20
diff
changeset
|
82 |
frg.title = frg_title |
|
bb7819c8d7c2
first step user login/logout. Create fragment works. Views and templates adapted with real data model.
cavaliet
parents:
20
diff
changeset
|
83 |
frg.description = frg_desc |
|
bb7819c8d7c2
first step user login/logout. Create fragment works. Views and templates adapted with real data model.
cavaliet
parents:
20
diff
changeset
|
84 |
frg.tags = frg_kw |
|
bb7819c8d7c2
first step user login/logout. Create fragment works. Views and templates adapted with real data model.
cavaliet
parents:
20
diff
changeset
|
85 |
frg.author = request.user |
|
bb7819c8d7c2
first step user login/logout. Create fragment works. Views and templates adapted with real data model.
cavaliet
parents:
20
diff
changeset
|
86 |
|
|
bb7819c8d7c2
first step user login/logout. Create fragment works. Views and templates adapted with real data model.
cavaliet
parents:
20
diff
changeset
|
87 |
frg.save() |
|
bb7819c8d7c2
first step user login/logout. Create fragment works. Views and templates adapted with real data model.
cavaliet
parents:
20
diff
changeset
|
88 |
|
|
bb7819c8d7c2
first step user login/logout. Create fragment works. Views and templates adapted with real data model.
cavaliet
parents:
20
diff
changeset
|
89 |
return redirect("view_fragment", fragment_pk=frg.pk) |
| 15 | 90 |
|
91 |
||
|
24
d65e05154aec
All pictures view with intelligent pagination. Css need to be improved.
cavaliet
parents:
23
diff
changeset
|
92 |
def all_pictures(request): |
|
d65e05154aec
All pictures view with intelligent pagination. Css need to be improved.
cavaliet
parents:
23
diff
changeset
|
93 |
|
|
27
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
94 |
# Get the cur_page_nb number parameter if possible |
|
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
95 |
cur_page_nb = request.GET.get("page") or 1 |
|
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
96 |
cur_page_nb = int(cur_page_nb) |
|
24
d65e05154aec
All pictures view with intelligent pagination. Css need to be improved.
cavaliet
parents:
23
diff
changeset
|
97 |
|
| 30 | 98 |
img_list = Image.objects.all().order_by('pk').select_related('info', 'metadata') |
|
24
d65e05154aec
All pictures view with intelligent pagination. Css need to be improved.
cavaliet
parents:
23
diff
changeset
|
99 |
nb = 32 |
|
d65e05154aec
All pictures view with intelligent pagination. Css need to be improved.
cavaliet
parents:
23
diff
changeset
|
100 |
paginator = Paginator(img_list, nb) |
|
d65e05154aec
All pictures view with intelligent pagination. Css need to be improved.
cavaliet
parents:
23
diff
changeset
|
101 |
try: |
|
27
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
102 |
results = paginator.page(cur_page_nb) |
|
24
d65e05154aec
All pictures view with intelligent pagination. Css need to be improved.
cavaliet
parents:
23
diff
changeset
|
103 |
except (EmptyPage, InvalidPage): |
|
d65e05154aec
All pictures view with intelligent pagination. Css need to be improved.
cavaliet
parents:
23
diff
changeset
|
104 |
results = paginator.page(paginator.num_pages) |
| 19 | 105 |
|
|
24
d65e05154aec
All pictures view with intelligent pagination. Css need to be improved.
cavaliet
parents:
23
diff
changeset
|
106 |
return render_to_response("egonomy_all_pictures.html", |
|
27
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
107 |
{'results':results, 'nb_pages':paginator.num_pages, 'cur_page_nb':cur_page_nb}, |
|
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
108 |
context_instance=RequestContext(request)) |
|
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
109 |
|
|
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
110 |
|
|
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
111 |
def all_fragments(request): |
|
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
112 |
|
|
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
113 |
# Get the cur_page_nb number parameter if possible |
|
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
114 |
cur_page_nb = request.GET.get("page") or 1 |
|
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
115 |
cur_page_nb = int(cur_page_nb) |
|
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
116 |
|
| 30 | 117 |
frg_list = Fragment.objects.all().order_by('pk').select_related('image', 'image__info', 'image__metadata','author') |
|
27
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
118 |
nb = 32 |
|
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
119 |
paginator = Paginator(frg_list, nb) |
|
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
120 |
try: |
|
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
121 |
results = paginator.page(cur_page_nb) |
|
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
122 |
except (EmptyPage, InvalidPage): |
|
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
123 |
results = paginator.page(paginator.num_pages) |
|
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
124 |
|
|
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
125 |
return render_to_response("egonomy_all_fragments.html", |
|
daaafc916dc4
Debug in templates. All fragments page added. Language files updated.
cavaliet
parents:
26
diff
changeset
|
126 |
{'results':results, 'nb_pages':paginator.num_pages, 'cur_page_nb':cur_page_nb}, |
|
24
d65e05154aec
All pictures view with intelligent pagination. Css need to be improved.
cavaliet
parents:
23
diff
changeset
|
127 |
context_instance=RequestContext(request)) |
|
d65e05154aec
All pictures view with intelligent pagination. Css need to be improved.
cavaliet
parents:
23
diff
changeset
|
128 |
|
|
d65e05154aec
All pictures view with intelligent pagination. Css need to be improved.
cavaliet
parents:
23
diff
changeset
|
129 |
|
|
d65e05154aec
All pictures view with intelligent pagination. Css need to be improved.
cavaliet
parents:
23
diff
changeset
|
130 |