Skip to content

Commit

Permalink
fixing update bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Ederporto committed Mar 14, 2024
1 parent 54ee227 commit 6c2d07d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
33 changes: 32 additions & 1 deletion metrics/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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 name=\"sara-{report.id}\">{ref_content}</ref>")
return " ".join(refs_set)


def get_done_for_report(reports, metric):
operation_reports = OperationReport.objects.filter(report__in=reports, metric=metric)
return {
Expand Down
7 changes: 4 additions & 3 deletions report/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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

Expand Down

0 comments on commit 6c2d07d

Please sign in to comment.