diff --git a/metrics/views.py b/metrics/views.py index 869eecb..b431ccb 100644 --- a/metrics/views.py +++ b/metrics/views.py @@ -11,6 +11,8 @@ from django.conf import settings from django.contrib.auth.decorators import login_required, permission_required from io import StringIO +import re +import urllib.parse register = template.Library() calendar.setfirstweekday(calendar.SUNDAY) @@ -147,7 +149,7 @@ def get_results_divided_by_trimester(buffer, area=None): main_results = get_results_for_timespan(timespan_array, Q(project=Project.objects.get(main_funding=True)), report_query) - poa_wikitext = construct_wikitext(poa_results, header + "!Activity !! Metrics !! Q1 !! Q2 !! Q3 !! Q4 !! Total\n|-\n") + poa_wikitext = construct_wikitext(poa_results, header + "!Activity !! Metrics !! Q1 !! Q2 !! Q3 !! Q4 !! Total !! References\n|-\n") main_wikitext = construct_wikitext(main_results, "") buffer.write(poa_wikitext) @@ -162,9 +164,11 @@ def get_results_for_timespan(timespan_array, metric_query=Q(), report_query=Q()) for time_ini, time_end in timespan_array: supplementary_query = Q(end_date__gte=time_ini) & Q(end_date__lte=time_end) & report_query goal, done = get_goal_and_done_for_metric(metric, supplementary_query=supplementary_query) + refs = build_wiki_ref_for_reports(metric, supplementary_query=supplementary_query) for key, value in goal.items(): if value != 0: done_row.append(done[key]) if done[key] else done_row.append("-") + done_row.append(refs) results.append({"activity": metric.activity.text, "metric": metric.text, "done": done_row}) return results @@ -271,6 +275,33 @@ def get_goal_for_metric(metric): } +def wikifi_link(link): + pattern = r"^https?://([a-z]+\.)?(wikipedia|wiktionary|wikibooks|wikinews|wikiquote|wikisource|wikiversity|wikivoyage|wikimedia|wikidata|commons)\.org/wiki/.*$" + if re.match(pattern, link): + cleaned_link = urllib.parse.unquote(link) + match = re.search(r"/wiki/(.*)$", cleaned_link) + project = re.search(r"(wikipedia|wiktionary|wikibooks|wikinews|wikiquote|wikisource|wikiversity|wikivoyage|wikimedia|wikidata|commons)", link).group(1) + page_title = match.group(1).replace("_", " ") + + return f"[[{project}:{page_title}|{page_title}]]" + else: + return f"[{link}]" + +def build_wiki_ref_for_reports(metric, supplementary_query=Q()): + query = Q(metrics_related__in=[metric]) & supplementary_query + reports = Report.objects.filter(query) + refs_set = [] + for report in reports: + links = report.links.split("\n") + formatted_links = [] + for link in links: + formatted_links.append(wikifi_link(link)) + + ref_content = " ".join(formatted_links) + refs_set.append(f"{ref_content}") + return " ".join(refs_set) + + def get_done_for_report(reports, metric): operation_reports = OperationReport.objects.filter(report__in=reports, metric=metric) return { diff --git a/report/forms.py b/report/forms.py index 33c2826..9c1f2f8 100644 --- a/report/forms.py +++ b/report/forms.py @@ -26,6 +26,7 @@ class Meta: def __init__(self, *args, **kwargs): user = kwargs.pop("user", None) + self.is_update = kwargs.pop("is_update", False) super(NewReportForm, self).__init__(*args, **kwargs) self.fields["activity_associated"].choices = activities_associated_as_choices() self.fields["directions_related"].choices = directions_associated_as_choices() @@ -52,12 +53,12 @@ def clean_editors(self): user_creation_date = get_user_date_of_registration(editor) if user_creation_date: editor_object.account_creation_date = user_creation_date - editor_object.save() # Which means that the user is already on the database and is returning = retained else: - editor_object.retained = 1 - editor_object.save() + if not self.is_update: + editor_object.retained = 1 + editor_object.save() editors.append(editor_object) return editors