Skip to content

Commit

Permalink
Stop using cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
brmzkw committed Nov 21, 2024
1 parent 9ce0c82 commit ade7f5b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
65 changes: 34 additions & 31 deletions mesads/app/views/ads_manager_admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.conf import settings
from django.core.exceptions import SuspiciousOperation
from django.core.mail import send_mail
from django.db import connection
from django.shortcuts import get_object_or_404, redirect
from django.template.loader import render_to_string
from django.urls import reverse
Expand Down Expand Up @@ -204,27 +203,22 @@ def add_sheets(self, workbook):
class ADSManagerAdminUpdatesView(TemplateView):
template_name = "pages/ads_register/ads_manager_admin_updates.html"

def get_updates(self, cursor):
# You might be wondering why we didn't implement pagination and why we
# limit to 100 results.
# Long story short, it's because we are using a raw query and pagination
# needs to be handled manually, and I've spent way too much time on this
# already.
# Alternatively we could use the django ORM instead of a raw query, but
# good luck with that.
cursor.execute(
"""
def get_updates(self):
query = """
SELECT
ads.id AS id,
adsmanager.id,
ads.id,
ads.last_update,
ads.number,
adsmanager.id AS ads_manager_id,
CASE
WHEN COUNT(revision.id) = 0 THEN NULL
ELSE COALESCE(JSON_AGG(JSON_BUILD_OBJECT(
'user_id', revision.user_id,
'user_email', "user".email,
'modification_date', revision.date_created
) ORDER BY revision.date_created DESC), '[]'::json)
END as updates
END as updates,
%(extra_fields)s
FROM app_ads AS ads
LEFT JOIN app_adsmanager AS adsmanager
ON adsmanager.id = ads.ads_manager_id
Expand All @@ -238,34 +232,43 @@ def get_updates(self, cursor):
LEFT JOIN users_user AS "user"
ON "user".id = revision.user_id
WHERE
adsmanageradministrator.id = %s
adsmanageradministrator.id = %%s
GROUP BY ads.id, adsmanager.id
ORDER BY ads.last_update DESC
LIMIT 100
""",
(self.kwargs["ads_manager_administrator"].id,),
""" % {
# The constructor of ADS retrieves the fields specified in
# SMART_VALIDATION_WATCHED_FIELDS. If we didn't explicitly select
# them, the ORM would have to perform a lazy load for each object to
# read them.
"extra_fields": ", ".join(
['"%s"' % key for key in ADS.SMART_VALIDATION_WATCHED_FIELDS.keys()]
)
}
ads_updated = list(
ADS.objects.raw(
query,
(self.kwargs["ads_manager_administrator"].id,),
)
)
updates = cursor.fetchall()

# Load objects
ads_objects = ADS.objects.filter(id__in=[row[0] for row in updates])
ads_dict = {obj.id: obj for obj in ads_objects}
ads_managers = {
obj.id: obj
for obj in ADSManager.objects.filter(
id__in=[row.ads_manager_id for row in ads_updated]
).prefetch_related("content_type", "content_object")
}

ads_managers = ADSManager.objects.filter(
id__in=[row[1] for row in updates]
).prefetch_related("content_type", "content_object")
ads_managers_dict = {obj.id: obj for obj in ads_managers}
return [
{
"ads": ads_dict[update[0]],
"ads_manager": ads_managers_dict[update[1]],
"history_entries": update[2],
"ads": row,
"ads_manager": ads_managers[row.ads_manager_id],
"history_entries": row.updates,
}
for update in updates
for row in ads_updated
]

def get_context_data(self, *args, **kwargs):
ctx = super().get_context_data(**kwargs)
with connection.cursor() as cursor:
ctx["updates"] = self.get_updates(cursor)
ctx["updates"] = self.get_updates()
return ctx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ <h1>Dernières modifications</h1>
{% for update in updates %}
<tr>
<td>
<a href="{% url 'app.ads.detail' manager_id=update.ads.ads_manager_id ads_id=update.ads.id %}">
<a href="{% url 'app.ads.detail' manager_id=update.ads_manager.id ads_id=update.ads.id %}">
{% comment %} Before, dates were stored as DateField instead of DateTimeField. Avoid displaying the time if it's 00:00. {% endcomment %}
{% if update.ads.last_update.hour == 0 and update.ads.last_update.minute == 0 %}
{{ update.ads.last_update|date:"d/m/Y" }}
Expand Down

0 comments on commit ade7f5b

Please sign in to comment.