def data_edit(request): """Returns view on current data ordered by count""" user = user_handler.get_user(request.user) edit_form = EditForm() filter_form = FilterForm() subjects = query_to_dicts("""SELECT log_subjects.subject, acquisition, processed, note, category_id, subjectcategory_id, count, date FROM log_subjects LEFT JOIN log_category on log_category.categoryid = log_subjects.category_id LEFT JOIN log_subjectcategory on log_subjectcategory.subjectcategoryid = log_subjects.subjectcategory_id RIGHT JOIN log_subjectcount on log_subjectcount.subject_id = log_subjects.subjectid WHERE log_subjectcount.user_id = '%s'"""%(user.id)) subjects = list(subjects) for i in xrange(len(subjects)): subjects[i]["subject"] = subjects[i]["subject"] subjects[i]["note"] = subjects[i]["note"] subjects.sort(key=lambda x: (x["subject"], x["date"]), reverse=True) page_interval = 150 subjects, filters = filter_data(subjects, dict(request.GET), page_interval) subjects = merge_subjects(subjects) next, previous, page_count = get_pagination(len(subjects), page_interval, filters["page"]) graph_data = False if "graph" in filters.keys(): graph_data = get_graph_data(subjects, filters) if next == "inactive": subjects = subjects[page_interval*filters["page"]:] else: subjects = subjects[page_interval*filters["page"]:page_interval*filters["page"]+page_interval] return render_to_response("data_edit.html", {'subjects': subjects, "edit_form": edit_form, "filter_form": filter_form, "filters":filters, "next": next, "previous":previous, "page_count":page_count, "graph_data": graph_data}, context_instance=RequestContext(request))
def get_graph_data(subjects, filters): graph_data = {} graph_id = filters["graph"] subjects = [t for t in subjects if t["processed"]] if graph_id == 1: key = "category_id" keys = set([t[key] for t in subjects]) categories = list(query_to_dicts("""SELECT * FROM log_category""")) keys2label = {} for c in categories: keys2label[c["categoryid"]] = c["category"] elif graph_id == 2: subjects = [t for t in subjects if t["category_id"] == 4] key = "subjectcategory_id" keys = set([t[key] for t in subjects]) subject_categories = list(query_to_dicts("""SELECT * FROM log_subjectcategory""")) keys2label = {} for c in subject_categories: keys2label[c["subjectcategoryid"]] = c["subjectcategory"] elif graph_id == 3: subjects = [t for t in subjects if t["category_id"] == 1] key = "acquisition" keys = set([t[key] for t in subjects]) keys2label = {0:"fond", 1:"akvizice"} else: return False data = {} for k in keys: if k != None: data[keys2label[k]] = 0 for t in subjects: if t[key] != None: data[keys2label[t[key]]] += 1 data = [[d, data[d]] for d in data.keys()] data.insert(0, ["", 0]) graph_data["data"] = data date = list(query_to_dicts("""SELECT date FROM log_subjectcount GROUP BY date ORDER BY date""")) if not filters["date_to"]: graph_data["date_to"] = "/".join([str(date[-1]["date"].month), str(date[-1]["date"].year)]) else: graph_data["date_to"] = filters["date_to"] if not filters["date_from"]: graph_data["date_from"] = "/".join([str(date[0]["date"].month), str(date[0]["date"].year)]) else: graph_data["date_from"] = filters["date_from"] graph_data["count"] = len(subjects) return graph_data