--- a/web/hdabo/views.py Wed Jul 06 18:34:03 2011 +0200
+++ b/web/hdabo/views.py Thu Jul 07 16:10:14 2011 +0200
@@ -30,23 +30,18 @@
all_ds_mapping = dict([(res['organisation'],res['nb_all']) for res in Datasheet.objects.values("organisation").annotate(nb_all=Count("organisation"))])
validated_ds_mapping = dict([(res['organisation'],[res['nb_val'],res['first_id_val']]) for res in Datasheet.objects.filter(validated=True).values("organisation").annotate(nb_val=Count("organisation")).annotate(first_id_val=Min("id"))])
unvalidated_ds_mapping = dict([(res['organisation'],[res['nb_unval'],res['first_id_unval']]) for res in Datasheet.objects.filter(validated=False).values("organisation").annotate(nb_unval=Count("organisation")).annotate(first_id_unval=Min("id"))])
-
+ # We get the hda_id from the id
+ val_hda_ids = dict([(res.id,res.hda_id) for res in Datasheet.objects.filter(id__in=[v[1] for v in validated_ds_mapping.values()]).only("id","hda_id")])
+ unval_hda_ids = dict([(res.id,res.hda_id) for res in Datasheet.objects.filter(id__in=[v[1] for v in unvalidated_ds_mapping.values()]).only("id","hda_id")])
+
for orga in orgas :
nb_all = all_ds_mapping.get(orga.id,0)
- duo_val = validated_ds_mapping.get(orga.id,0)
- if duo_val != 0:
- nb_val = duo_val[0]
- first_id_val = duo_val[1]
- else :
- nb_val = 0
- first_id_val = None
- duo_unval = unvalidated_ds_mapping.get(orga.id,0)
- if duo_unval != 0:
- nb_unval = duo_unval[0]
- first_id_unval = duo_unval[1]
- else :
- nb_unval = 0
- first_id_unval = None
+ duo_val = validated_ds_mapping.get(orga.id,[0,None])
+ nb_val = duo_val[0]
+ first_id_val = val_hda_ids.get(duo_val[1], None)
+ duo_unval = unvalidated_ds_mapping.get(orga.id,[0,None])
+ nb_unval = duo_unval[0]
+ first_id_unval = unval_hda_ids.get(duo_unval[1], None)
org_list.append({'organisation':orga, 'nb_all':nb_all, 'nb_val':nb_val, 'first_id_val':first_id_val, 'nb_unval':nb_unval, 'first_id_unval':first_id_unval})
return render_to_response("organisation_list.html",
@@ -58,50 +53,83 @@
def display_datasheet(request, ds_id=None):
if ds_id :
- ds = Datasheet.objects.get(id=ds_id)
- start_index = None
+ ds = Datasheet.objects.get(hda_id=ds_id)
# In this function the context is given by the get parameters.
- # If there is searched_tag parameter, it means that we display all the ds tagged by one particular tag
- if "searched_tag" in request.GET :
- param_str = "?searched_tag=" + request.GET["searched_tag"]
- datasheets_qs = Datasheet.objects.filter(tags__in=[Tag.objects.get(id=int(request.GET["searched_tag"]))]).order_by("organisation__name","original_creation_date").select_related("format")
- # If searched_tag is set and if ds_id is None, it means that we have to display the first ds
+ if "index" in request.GET :
+ try:
+ index = int(request.GET["index"])
+ except :
+ index = 0
+ else :
+ index = 0
+ if "nb_sheets" in request.GET :
+ try:
+ nb_sheets = int(request.GET["nb_sheets"])
+ except :
+ nb_sheets = None
+ else :
+ nb_sheets = None
+ # If there is tag parameter, it means that we display all the ds tagged by one particular tag
+ param_str = None
+ if "tag" in request.GET :
+ param_str = "?tag=" + request.GET["tag"]
+ datasheets_qs = Datasheet.objects.filter(tags__in=[Tag.objects.get(id=int(request.GET["tag"]))]).order_by("organisation__name","original_creation_date").select_related("format")
+ # If tag is set and if ds_id is None, it means that we have to display the first ds
if not ds_id :
- start_index = 0
+ index = 0
ds = datasheets_qs[0]
else :
- param_str = ""
# The current list depends on the ds's validation state
datasheets_qs = Datasheet.objects.filter(organisation=ds.organisation).filter(validated=ds.validated).order_by("id").select_related("format")
+ # We calculate the number of sheets if necessary
+ if not nb_sheets :
+ nb_sheets = datasheets_qs.count()
- # Since a QuerySet is not a list, we cannot find easily the current ds's index
- if not start_index :
- i = 0
- for searched_ds in datasheets_qs :
- if searched_ds==ds :
- start_index = i
- break
- i += 1
-
- nb_sheets = datasheets_qs.count()
+ # We get only the prev/current/next sheets
+ if nb_sheets > 1 :
+ if index == 0 :
+ select_qs = datasheets_qs[:2]
+ prev_index = 0
+ prev_id = select_qs[0].hda_id
+ next_index = 1
+ next_id = select_qs[1].hda_id
+ elif index == (nb_sheets - 1) :
+ select_qs = datasheets_qs[nb_sheets-2:]
+ prev_index = nb_sheets - 2
+ prev_id = select_qs[0].hda_id
+ next_index = nb_sheets - 1
+ next_id = select_qs[1].hda_id
+ else :
+ select_qs = datasheets_qs[index-1:index+2]
+ prev_index = index - 1
+ prev_id = select_qs[0].hda_id
+ next_index = index + 1
+ next_id = select_qs[2].hda_id
+ else :
+ prev_index = next_index = 0
+ prev_id = next_id = datasheets_qs[0].hda_id
+
+ if param_str :
+ param_str += "&"
+ else :
+ param_str = "?"
+ param_str += "nb_sheets=" + str(nb_sheets)
# We get the ORDERED tags if we display one sheet (case valid = 0 and 1)
ordered_tags = TaggedSheet.objects.filter(datasheet=ds).select_related("tag").order_by('order')
- displayed_index = start_index + 1;
- zero_id = datasheets_qs[0].id
- prev_index = max(start_index - 1, 0);
- prev_id = datasheets_qs[prev_index].id
- next_index = min(nb_sheets - 1, start_index + 1);
- next_id = datasheets_qs[next_index].id
- last_index = max(nb_sheets - 1, 0);
- last_id = datasheets_qs[last_index].id
+ displayed_index = index + 1;
+ zero_id = datasheets_qs[0].hda_id
+ last_index = max(nb_sheets - 1, 0)
+ last_id = datasheets_qs[last_index].hda_id
return render_to_response("generic_sheet.html",
{'ds':ds, 'orga_name':ds.organisation.name,
'nb_sheets':nb_sheets, 'ordered_tags':ordered_tags,
- 'zero_id':zero_id, 'prev_id':prev_id, 'next_id':next_id, 'last_id':last_id,
+ 'zero_id':zero_id, 'prev_index':prev_index, 'prev_id':prev_id,
+ 'next_index':next_index, 'next_id':next_id,
+ 'last_index':last_index, 'last_id':last_id,
'displayed_index':displayed_index, 'valid':ds.validated,
'param_str':param_str, 'categories':json.dumps(get_categories())},
context_instance=RequestContext(request))
@@ -209,7 +237,7 @@
new_order = int(request.POST["new_order"]) - 1
old_order = int(request.POST["old_order"]) - 1
# First we get the datasheet's TaggedSheets (list to force evaluation)
- ordered_tags = list(TaggedSheet.objects.filter(datasheet=Datasheet.objects.get(id=ds_id)).order_by('order'))
+ ordered_tags = list(TaggedSheet.objects.filter(datasheet=Datasheet.objects.get(hda_id=ds_id)).order_by('order'))
# We change the moved TaggedSheets's order
new_ts_order = ordered_tags[new_order].order
moved_ts = ordered_tags[old_order]
@@ -228,7 +256,7 @@
ts = ordered_tags[i]
ts.order = ts.order + 1
ts.save()
- ds = Datasheet.objects.get(id=ds_id)
+ ds = Datasheet.objects.get(hda_id=ds_id)
ds.manual_order = True
ds.save()
@@ -238,7 +266,7 @@
@login_required
def get_tag_table(request=None, ds_id=None, valid=None):
- ordered_tags = TaggedSheet.objects.filter(datasheet=Datasheet.objects.filter(id=ds_id)[0]).order_by('order')
+ ordered_tags = TaggedSheet.objects.filter(datasheet=Datasheet.objects.filter(hda_id=ds_id)[0]).order_by('order')
return render_to_response("partial/tag_table.html",
{'ordered_tags':ordered_tags, 'valid':valid},
@@ -297,12 +325,12 @@
ds_id = request.POST["datasheet_id"]
tag_id = request.POST["tag_id"]
# First we get the datasheet's TaggedSheets
- ds_tags = TaggedSheet.objects.filter(datasheet=Datasheet.objects.filter(id=ds_id)[0])
+ ds_tags = TaggedSheet.objects.filter(datasheet=Datasheet.objects.filter(hda_id=ds_id)[0])
# We get the current TaggedSheet and we delete it
ts = ds_tags.filter(tag=Tag.objects.filter(id=tag_id))[0]
ts.delete()
- ds = Datasheet.objects.get(id=ds_id)
+ ds = Datasheet.objects.get(hda_id=ds_id)
ds.manual_order = True
ds.save()
@@ -346,7 +374,7 @@
tag = Tag.objects.get(id=tag_id)
- ds = Datasheet.objects.get(id=ds_id)
+ ds = Datasheet.objects.get(hda_id=ds_id)
if tag.label != tag_label:
@@ -401,7 +429,7 @@
ds_id = request.POST["datasheet_id"]
tag_label = request.POST["value"]
- ds = Datasheet.objects.get(id=ds_id)
+ ds = Datasheet.objects.get(hda_id=ds_id)
if tag_label.lower() in [t.label.lower() for t in ds.tags.all()]:
return HttpResponseBadRequest(json.dumps({'error': 'duplicate_tag', 'message': u"Le tag %s existe déjà pour cette fiche." % (tag_label)}), mimetype="application/json")
@@ -453,7 +481,7 @@
else:
user = None
- ds = Datasheet.objects.get(id=ds_id)
+ ds = Datasheet.objects.get(hda_id=ds_id)
if valid :
ds.validate(user)
else :
@@ -463,7 +491,7 @@
# If there are still some unvalidated/validated ds for the ds's orga, we display the first one
same_organisation_ds = Datasheet.objects.filter(organisation=ds.organisation).filter(validated=not valid)
if len(same_organisation_ds) > 0 :
- return redirect('display_datasheet', ds_id=same_organisation_ds[0].id)
+ return redirect('display_datasheet', ds_id=same_organisation_ds[0].hda_id)
else :
return redirect('home')