6 from django.utils.html import escape |
6 from django.utils.html import escape |
7 from django.utils.translation import ugettext as _ |
7 from django.utils.translation import ugettext as _ |
8 from ldt.ldt_utils.models import Project |
8 from ldt.ldt_utils.models import Project |
9 from ldt.ldt_utils.projectserializer import ProjectJsonSerializer |
9 from ldt.ldt_utils.projectserializer import ProjectJsonSerializer |
10 from ldt.ldt_utils.searchutils import search_generate_ldt |
10 from ldt.ldt_utils.searchutils import search_generate_ldt |
|
11 from operator import itemgetter |
11 from datetime import datetime |
12 from datetime import datetime |
12 import ldt.auth as ldt_auth |
13 import ldt.auth as ldt_auth |
13 import lxml.etree |
14 import lxml.etree |
|
15 import logging |
14 |
16 |
15 |
17 |
16 def project_json_id(request, id): |
18 def project_json_id(request, id): |
17 |
19 |
18 project = get_object_or_404(Project.safe_objects, ldt_id=id) |
20 project = get_object_or_404(Project.safe_objects, ldt_id=id) |
92 if escape_str: |
94 if escape_str: |
93 escape_bool = {'true': True, 'false': False, "0": False, "1": True}.get(escape_str.lower()) |
95 escape_bool = {'true': True, 'false': False, "0": False, "1": True}.get(escape_str.lower()) |
94 |
96 |
95 # We search |
97 # We search |
96 s = request.REQUEST.get("tag") |
98 s = request.REQUEST.get("tag") |
|
99 sort_type = request.REQUEST.get("sort", "") |
97 if s: |
100 if s: |
98 # We get the projects with all the segments |
101 # We get the projects with all the segments |
99 project_xml, results = search_generate_ldt(request, "tags", s, False) |
102 project_xml, results = search_generate_ldt(request, "tags", s, False) |
100 project = Project() |
103 project = Project() |
101 project.ldt = lxml.etree.tostring(project_xml, pretty_print=True) |
104 project.ldt = lxml.etree.tostring(project_xml, pretty_print=True) |
125 "listtype": "mashup", |
128 "listtype": "mashup", |
126 "dc:description": "" |
129 "dc:description": "" |
127 }, |
130 }, |
128 "id": "generated_mashup_list" |
131 "id": "generated_mashup_list" |
129 } |
132 } |
130 #filtered_results = [] |
133 # If sort_type = weight, we sort the result by the tag's weight |
131 for res in results: |
134 if sort_type.lower() == "weight": |
132 cur_in = float(res["start_ts"]) |
135 # First we turn each string timecode to a float |
133 cur_out = cur_in + float(res["duration"]) |
136 for res in results: |
134 if tc_in<=cur_in and cur_out<=tc_out: |
137 res["start_ts"] = float(res["start_ts"]) |
135 #filtered_results.append(res) |
138 res["duration"] = float(res["duration"]) |
136 mashup_list["items"].append(res["element_id"]) |
139 # We sort to group by start_ts for each media/iri_id |
|
140 sorted_results = sorted(results, key=itemgetter("iri_id", "start_ts", "duration")) |
|
141 highest_weighted = [] |
|
142 current_weight = 1 |
|
143 nb_res = len(sorted_results) |
|
144 for i, res in enumerate(sorted_results): |
|
145 # Explanation : we calculate the weight, which is the number of segments |
|
146 # tagged with the searched tag for the same iri_id at the same start_ts. |
|
147 # Thanks to the previous sort, the last segment is the one with the longest duration and the one we finally keep |
|
148 next_res = None |
|
149 if i<(nb_res-1): |
|
150 next_res = sorted_results[i+1] |
|
151 if next_res and next_res["iri_id"]==res["iri_id"] and next_res["start_ts"]==res["start_ts"]: |
|
152 current_weight += 1 |
|
153 continue |
|
154 res["weight"] = current_weight |
|
155 highest_weighted.append(res) |
|
156 current_weight = 1 |
|
157 |
|
158 # Now that we have the weight for all temporal segments, we just have to sort the array. |
|
159 highest_weighted = sorted(highest_weighted, key=itemgetter("weight"), reverse=True) |
|
160 for res in highest_weighted: |
|
161 cur_in = res["start_ts"] |
|
162 cur_out = cur_in + res["duration"] |
|
163 if tc_in<=cur_in and cur_out<=tc_out: |
|
164 #mashup_list["items"].append(res["iri_id"] + ", " + res["element_id"] + ", " + str(res["start_ts"]) + ", " + str(res["duration"]) + ", " + str(res["weight"])) |
|
165 mashup_list["items"].append(res["element_id"]) |
|
166 else: |
|
167 # no particular sorting |
|
168 for res in results: |
|
169 cur_in = float(res["start_ts"]) |
|
170 cur_out = cur_in + float(res["duration"]) |
|
171 if tc_in<=cur_in and cur_out<=tc_out: |
|
172 mashup_list["items"].append(res["element_id"]) |
137 mashup_dict["lists"].append(mashup_list) |
173 mashup_dict["lists"].append(mashup_list) |
138 |
|
139 #mashup_dict["escape_bool"] = escape_bool |
|
140 #mashup_dict["indent"] = indent |
|
141 |
174 |
142 json_str = simplejson.dumps(mashup_dict, ensure_ascii=False, indent=indent) |
175 json_str = simplejson.dumps(mashup_dict, ensure_ascii=False, indent=indent) |
143 if escape_bool: |
176 if escape_bool: |
144 json_str = escape(json_str) |
177 json_str = escape(json_str) |
145 |
178 |