mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 08:31:08 -05:00
Improve code readability in moderation models using walrus operator
This commit is contained in:
@@ -79,26 +79,23 @@ class EditSubmission(models.Model):
|
||||
|
||||
def __str__(self) -> str:
|
||||
action = "creation" if self.submission_type == "CREATE" else "edit"
|
||||
model_class = self.content_type.model_class()
|
||||
target = self.content_object or (
|
||||
model_class.__name__ if model_class else "Unknown"
|
||||
)
|
||||
if model_class := self.content_type.model_class():
|
||||
target = self.content_object or model_class.__name__
|
||||
else:
|
||||
target = "Unknown"
|
||||
return f"{action} by {self.user.username} on {target}"
|
||||
|
||||
def _resolve_foreign_keys(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Convert foreign key IDs to model instances"""
|
||||
model_class = self.content_type.model_class()
|
||||
if not model_class:
|
||||
if not (model_class := self.content_type.model_class()):
|
||||
raise ValueError("Could not resolve model class")
|
||||
|
||||
resolved_data = data.copy()
|
||||
|
||||
for field_name, value in data.items():
|
||||
try:
|
||||
field = model_class._meta.get_field(field_name)
|
||||
if isinstance(field, models.ForeignKey) and value is not None:
|
||||
related_model = field.related_model
|
||||
if related_model:
|
||||
if (field := model_class._meta.get_field(field_name)) and isinstance(field, models.ForeignKey) and value is not None:
|
||||
if related_model := field.related_model:
|
||||
resolved_data[field_name] = related_model.objects.get(id=value)
|
||||
except (FieldDoesNotExist, ObjectDoesNotExist):
|
||||
continue
|
||||
@@ -111,8 +108,7 @@ class EditSubmission(models.Model):
|
||||
self.handled_by = user # type: ignore
|
||||
self.handled_at = timezone.now()
|
||||
|
||||
model_class = self.content_type.model_class()
|
||||
if not model_class:
|
||||
if not (model_class := self.content_type.model_class()):
|
||||
raise ValueError("Could not resolve model class")
|
||||
|
||||
try:
|
||||
@@ -126,8 +122,7 @@ class EditSubmission(models.Model):
|
||||
self.object_id = getattr(obj, "id", None)
|
||||
else:
|
||||
# Apply changes to existing object
|
||||
obj = self.content_object
|
||||
if not obj:
|
||||
if not (obj := self.content_object):
|
||||
raise ValueError("Content object not found")
|
||||
for field, value in resolved_data.items():
|
||||
setattr(obj, field, value)
|
||||
|
||||
Reference in New Issue
Block a user