Skip to content

Commit

Permalink
deps, models, views: django upgrade to 4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
m4ra committed Nov 27, 2023
1 parent a434d2a commit ee0ef87
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2 on 2023-11-27 12:54

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("a4actions", "0008_set_action_obj_comment_creator"),
]

operations = [
migrations.RenameIndex(
model_name="action",
new_name="a4actions_a_obj_con_ee317e_idx",
old_fields=("obj_content_type", "obj_object_id"),
),
]
2 changes: 1 addition & 1 deletion adhocracy4/actions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Action(models.Model):

class Meta:
ordering = ("-timestamp",)
index_together = [("obj_content_type", "obj_object_id")]
indexes = [models.Index(fields=["obj_content_type", "obj_object_id"])]

def __str__(self):
ctx = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2 on 2023-11-27 12:54

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("a4comments", "0013_set_project"),
]

operations = [
migrations.RenameIndex(
model_name="comment",
new_name="a4comments__content_ff606b_idx",
old_fields=("content_type", "object_pk"),
),
]
13 changes: 10 additions & 3 deletions adhocracy4/comments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Meta:
verbose_name = pgettext_lazy("noun", "Comment")
verbose_name_plural = _("Comments")
ordering = ("created",)
index_together = [("content_type", "object_pk")]
indexes = [models.Index(fields=["content_type", "object_pk"])]

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand All @@ -60,17 +60,24 @@ def __str__(self):
else:
return "{}".format(self.comment)

def save(self, *args, **kwargs):
def save(self, update_fields=None, *args, **kwargs):
"""Change comment.comment if comment was marked removed or censored."""
self.comment = transforms.clean_html_all(self.comment)
update_project = False
if not self.project:
self.project = self.content_object.module.project
update_project = True

if self.is_removed or self.is_censored:
self.comment = self._former_comment = ""
self.comment_categories = ""

super(Comment, self).save(*args, **kwargs)
if update_fields:
update_fields = {"comment"}.union(update_fields)
if update_project:
update_fields = {"project"}.union(update_fields)

super().save(update_fields=update_fields, *args, **kwargs)

def get_absolute_url(self):
if hasattr(self.content_object, "get_absolute_url"):
Expand Down
2 changes: 1 addition & 1 deletion adhocracy4/dashboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def post(self, request, *args, **kwargs):
def get_next(self):
if "referrer" in self.request.POST:
return self.request.POST["referrer"]
elif "HTTP_REFERER" in self.request.META:
elif "referer" in self.request.headers:
return self.request.headers["Referer"]

return reverse(
Expand Down
6 changes: 4 additions & 2 deletions adhocracy4/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ class TimeStampedModel(models.Model):
class Meta:
abstract = True

def save(self, ignore_modified=False, *args, **kwargs):
def save(self, ignore_modified=False, update_fields=None, *args, **kwargs):
if self.pk is not None and not ignore_modified:
self.modified = timezone.now()
super(TimeStampedModel, self).save(*args, **kwargs)
if update_fields:
update_fields = {"modified"}.union(update_fields)
super().save(update_fields=update_fields, *args, **kwargs)


class UserGeneratedContentModel(TimeStampedModel):
Expand Down
10 changes: 8 additions & 2 deletions adhocracy4/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ def __str__(self):
def get_absolute_url(self):
return reverse("project-detail", kwargs=dict(slug=self.slug))

def save(self, *args, **kwargs):
def save(self, update_fields=None, *args, **kwargs):
self.information = html_transforms.clean_html_field(
self.information, "collapsible-image-editor"
)
Expand All @@ -289,7 +289,13 @@ def save(self, *args, **kwargs):
if self.pk is None:
project_type = "{}.{}".format(self._meta.app_label, self.__class__.__name__)
self.project_type = project_type
super(Project, self).save(*args, **kwargs)

if update_fields:
update_fields = {"information", "result"}.union(update_fields)
if self.pk is None:
update_fields = {"project_type"}.union(update_fields)

super().save(update_fields=update_fields, *args, **kwargs)

# functions needed to determine permissions
def has_member(self, user):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2 on 2023-11-27 12:54

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("a4ratings", "0004_verbose_name_created_modified"),
]

operations = [
migrations.RenameIndex(
model_name="rating",
new_name="a4ratings_r_content_8fb00e_idx",
old_fields=("content_type", "object_pk"),
),
]
4 changes: 1 addition & 3 deletions adhocracy4/ratings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@


class Rating(UserGeneratedContentModel):

POSITIVE = 1
NEGATIVE = -1

Expand All @@ -18,7 +17,7 @@ class Rating(UserGeneratedContentModel):

class Meta:
unique_together = ("content_type", "object_pk", "creator")
index_together = [("content_type", "object_pk")]
indexes = [models.Index(fields=["content_type", "object_pk"])]

def __str__(self):
return str(self.value)
Expand Down Expand Up @@ -46,7 +45,6 @@ def _get_value(self, number):
return number

def get_meta_info(self, user):

ratings = Rating.objects.filter(
content_type=self.content_type, object_pk=self.object_pk
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2 on 2023-11-27 12:54

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("a4reports", "0003_verbose_name_created_modified"),
]

operations = [
migrations.RenameIndex(
model_name="report",
new_name="a4reports_r_content_75c7cc_idx",
old_fields=("content_type", "object_pk"),
),
]
3 changes: 1 addition & 2 deletions adhocracy4/reports/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


class Report(base.UserGeneratedContentModel):

content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_pk = models.PositiveIntegerField()
content_object = GenericForeignKey(ct_field="content_type", fk_field="object_pk")
Expand All @@ -16,7 +15,7 @@ def __str__(self):
return "{}_{}".format(str(self.content_type), str(self.object_pk))

class Meta:
index_together = [("content_type", "object_pk")]
indexes = [models.Index(fields=["content_type", "object_pk"])]

@property
def project(self):
Expand Down
4 changes: 4 additions & 0 deletions changelog/7776.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@

- Django from 4.0 to 4.1
Reverse relations need to be saved before being called from the object they relate

- Django from 4.1 to 4.2
any fields modified in the custom save() methods should be added to the update_fields keyword argument before calling super()
retrieve referer in requests from headers instead of META
2 changes: 1 addition & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
beautifulsoup4==4.12.2
bleach[css]==6.0.0
Django==4.1
Django==4.2
django-allauth==0.55.0
git+https://github.com/liqd/django-autoslug.git@liqd2212#egg=django-autoslug
https://github.com/liqd/django-ckeditor-5/releases/download/v0.2.11-liqd/django_ckeditor_5-0.2.11-py3-none-any.whl
Expand Down
2 changes: 0 additions & 2 deletions tests/project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@

USE_I18N = True

USE_L10N = True

USE_TZ = True


Expand Down

0 comments on commit ee0ef87

Please sign in to comment.