Skip to content

Commit

Permalink
Change __unicode__ to __str__ globally
Browse files Browse the repository at this point in the history
  • Loading branch information
inducer committed Sep 11, 2024
1 parent ec387c2 commit d5e5bef
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 69 deletions.
4 changes: 1 addition & 3 deletions course/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,12 @@ class EventAdmin(admin.ModelAdmin):
"kind",
)

def __unicode__(self): # pragma: no cover # not used
def __str__(self): # pragma: no cover # not used
return "{}{} in {}".format(
self.kind,
f" ({self.ordinal!s})" if self.ordinal is not None else "",
self.course)

__str__ = __unicode__

list_editable = ("ordinal", "time", "end_time", "shown_in_calendar")

# {{{ permissions
Expand Down
89 changes: 23 additions & 66 deletions course/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,9 @@ class Meta:
verbose_name = _("Course")
verbose_name_plural = _("Courses")

def __unicode__(self):
def __str__(self) -> str:
return self.identifier

__str__ = __unicode__

def clean(self):
if self.force_lang:
self.force_lang = self.force_lang.strip()
Expand Down Expand Up @@ -333,7 +331,7 @@ class Meta:
ordering = ("course", "time")
unique_together = (("course", "kind", "ordinal"))

def __unicode__(self):
def __str__(self) -> str:
if self.ordinal is not None:
return f"{self.kind} {self.ordinal}"
else:
Expand Down Expand Up @@ -374,8 +372,6 @@ def save(self, *args, **kwargs):
self.full_clean()
return super().save(*args, **kwargs)

__str__ = __unicode__

# }}}


Expand All @@ -401,11 +397,9 @@ def clean(self):
{field_name:
_("'%s' contains invalid characters.") % field_name})

def __unicode__(self):
def __str__(self) -> str:
return f"{self.name} ({self.course})"

__str__ = __unicode__

class Meta:
verbose_name = _("Participation tag")
verbose_name_plural = _("Participation tags")
Expand Down Expand Up @@ -441,7 +435,7 @@ def clean(self):
{field_name:
_("'%s' contains invalid characters.") % field_name})

def __unicode__(self):
def __str__(self) -> str:
return _("%(identifier)s in %(course)s") % {
"identifier": self.identifier,
"course": self.course}
Expand Down Expand Up @@ -470,7 +464,6 @@ def has_permission(self, perm: str, argument: str | None = None) -> bool:
return (perm, argument) in self.permission_tuples()

# }}}
__str__ = __unicode__

class Meta:
verbose_name = _("Participation role")
Expand All @@ -490,28 +483,24 @@ class ParticipationPermissionBase(models.Model):
class Meta:
abstract = True

def __unicode__(self):
def __str__(self) -> str:
if self.argument:
return f"{self.permission} {self.argument}"
else:
return self.permission

__str__ = __unicode__


class ParticipationRolePermission(ParticipationPermissionBase):
role = models.ForeignKey(ParticipationRole,
verbose_name=_("Role"), on_delete=models.CASCADE,
related_name="permissions")

def __unicode__(self):
def __str__(self) -> str:
# Translators: permissions for roles
return _("%(permission)s for %(role)s") % {
"permission": super().__unicode__(),
"permission": super().__str__(),
"role": self.role}

__str__ = __unicode__

class Meta:
verbose_name = _("Participation role permission")
verbose_name_plural = _("Participation role permissions")
Expand Down Expand Up @@ -552,7 +541,7 @@ class Participation(models.Model):
notes = models.TextField(blank=True, null=True,
verbose_name=_("Notes"))

def __unicode__(self):
def __str__(self) -> str:
# Translators: displayed format of Participation: some user in some
# course as some role
return _("%(user)s in %(course)s as %(role)s") % {
Expand All @@ -562,8 +551,6 @@ def __unicode__(self):
for role in self.roles.all())
}

__str__ = __unicode__

class Meta:
verbose_name = _("Participation")
verbose_name_plural = _("Participations")
Expand Down Expand Up @@ -634,7 +621,7 @@ class ParticipationPreapproval(models.Model):
creation_time = models.DateTimeField(default=now, db_index=True,
verbose_name=_("Creation time"))

def __unicode__(self):
def __str__(self) -> str:
if self.email:
# Translators: somebody's email in some course in Participation
# Preapproval
Expand All @@ -649,8 +636,6 @@ def __unicode__(self):
return _("Preapproval with pk %(pk)s in %(course)s") % {
"pk": self.pk, "course": self.course}

__str__ = __unicode__

class Meta:
verbose_name = _("Participation preapproval")
verbose_name_plural = _("Participation preapprovals")
Expand Down Expand Up @@ -820,14 +805,12 @@ class AuthenticationToken(models.Model):
null=True, blank=True, unique=True,
verbose_name=_("Hash of git authentication token"))

def __unicode__(self):
def __str__(self) -> str:
return _("Token %(id)d for %(participation)s: %(description)s") % {
"id": self.id,
"participation": self.participation,
"description": self.description}

__str__ = __unicode__

class Meta:
verbose_name = _("Authentication token")
verbose_name_plural = _("Authentication tokens")
Expand All @@ -854,7 +837,7 @@ class Meta:
verbose_name = _("Instant flow request")
verbose_name_plural = _("Instant flow requests")

def __unicode__(self):
def __str__(self) -> str:
return _("Instant flow request for "
"%(flow_id)s in %(course)s at %(start_time)s") \
% {
Expand All @@ -863,8 +846,6 @@ def __unicode__(self):
"start_time": self.start_time,
}

__str__ = __unicode__

# }}}


Expand Down Expand Up @@ -937,7 +918,7 @@ class Meta:
verbose_name_plural = _("Flow sessions")
ordering = ("course", "-start_time")

def __unicode__(self):
def __str__(self) -> str:
if self.participation is None:
return _("anonymous session %(session_id)d on '%(flow_id)s'") % {
"session_id": self.id,
Expand All @@ -948,8 +929,6 @@ def __unicode__(self):
"session_id": self.id,
"flow_id": self.flow_id}

__str__ = __unicode__

def append_comment(self, s: str | None) -> None:
if s is None:
return
Expand Down Expand Up @@ -1029,7 +1008,7 @@ class Meta:
verbose_name = _("Flow page data")
verbose_name_plural = _("Flow page data")

def __unicode__(self):
def __str__(self) -> str:
# flow page data
return (_("Data for page '%(group_id)s/%(page_id)s' "
"(page ordinal %(page_ordinal)s) in %(flow_session)s") % {
Expand All @@ -1038,8 +1017,6 @@ def __unicode__(self):
"page_ordinal": self.page_ordinal,
"flow_session": self.flow_session})

__str__ = __unicode__

# Django's templates are a little daft. No arithmetic--really?
def previous_ordinal(self):
return self.page_ordinal - 1
Expand Down Expand Up @@ -1103,7 +1080,7 @@ class FlowPageVisit(models.Model):
verbose_name=_("Is submitted answer"),
null=True)

def __unicode__(self):
def __str__(self) -> str:
result = (
# Translators: flow page visit
_("'%(group_id)s/%(page_id)s' in '%(session)s' "
Expand All @@ -1120,8 +1097,6 @@ def __unicode__(self):

return result

__str__ = __unicode__

class Meta:
verbose_name = _("Flow page visit")
verbose_name_plural = _("Flow page visits")
Expand Down Expand Up @@ -1221,15 +1196,13 @@ class Meta:

ordering = ("visit", "grade_time")

def __unicode__(self):
def __str__(self) -> str:
# information on FlowPageVisitGrade class
# Translators: return the information of the grade of a user
# by percentage.
return _("grade of %(visit)s: %(percentage)s") % {
"visit": self.visit, "percentage": self.percentage()}

__str__ = __unicode__

# }}}


Expand Down Expand Up @@ -1405,7 +1378,7 @@ class FlowAccessException(models.Model): # pragma: no cover (deprecated and not
comment = models.TextField(blank=True, null=True,
verbose_name=_("Comment"))

def __unicode__(self):
def __str__(self) -> str:
return (
# Translators: flow access exception in admin (deprecated)
_("Access exception for '%(user)s' to '%(flow_id)s' "
Expand All @@ -1416,8 +1389,6 @@ def __unicode__(self):
"course": self.participation.course
})

__str__ = __unicode__


class FlowAccessExceptionEntry(models.Model): # pragma: no cover (deprecated and not tested) # noqa
# deprecated
Expand All @@ -1433,11 +1404,9 @@ class Meta:
# Translators: FlowAccessExceptionEntry (deprecated)
verbose_name_plural = _("Flow access exception entries")

def __unicode__(self):
def __str__(self) -> str:
return self.permission

__str__ = __unicode__

# }}}


Expand Down Expand Up @@ -1468,7 +1437,7 @@ class FlowRuleException(models.Model):
verbose_name=pgettext_lazy(
"Is the flow rule exception activated?", "Active"))

def __unicode__(self):
def __str__(self) -> str:
return (
# Translators: For FlowRuleException
_("%(kind)s exception %(exception_id)s for '%(user)s' to "
Expand All @@ -1481,8 +1450,6 @@ def __unicode__(self):
"exception_id":
" id %d" % self.id if self.id is not None else ""})

__str__ = __unicode__

def clean(self) -> None:
super().clean()

Expand Down Expand Up @@ -1617,7 +1584,7 @@ class Meta:
ordering = ("course", "due_time", "identifier")
unique_together = (("course", "identifier"),)

def __unicode__(self):
def __str__(self) -> str:
return (
# Translators: For GradingOpportunity
_("%(opportunity_name)s (%(opportunity_id)s) in %(course)s")
Expand All @@ -1626,8 +1593,6 @@ def __unicode__(self):
"opportunity_id": self.identifier,
"course": self.course})

__str__ = __unicode__

def get_aggregation_strategy_descr(self):
return dict(GRADE_AGGREGATION_STRATEGY_CHOICES).get(
self.aggregation_strategy)
Expand Down Expand Up @@ -1686,15 +1651,13 @@ class Meta:
verbose_name_plural = _("Grade changes")
ordering = ("opportunity", "participation", "grade_time")

def __unicode__(self):
def str(self):
# Translators: information for GradeChange
return _("%(participation)s %(state)s on %(opportunityname)s") % {
"participation": self.participation,
"state": self.state,
"opportunityname": self.opportunity.name}

__str__ = __unicode__

def clean(self):
super().clean()

Expand Down Expand Up @@ -1941,11 +1904,9 @@ class Meta:
verbose_name_plural = _("Instant messages")
ordering = ("participation__course", "time")

def __unicode__(self):
def __str__(self) -> str:
return f"{self.participation}: {self.text}"

__str__ = __unicode__

# }}}


Expand Down Expand Up @@ -1980,14 +1941,12 @@ class Meta:
verbose_name_plural = _("Exams")
ordering = ("course", "no_exams_before",)

def __unicode__(self):
def __str__(self) -> str:
return _("Exam %(description)s in %(course)s") % {
"description": self.description,
"course": self.course,
}

__str__ = __unicode__


class ExamTicket(models.Model):
exam = models.ForeignKey(Exam,
Expand Down Expand Up @@ -2038,14 +1997,12 @@ class Meta:
("can_issue_exam_tickets", _("Can issue exam tickets to student")),
)

def __unicode__(self):
def __str__(self) -> str:
return _("Exam ticket for %(participation)s in %(exam)s") % {
"participation": self.participation,
"exam": self.exam,
}

__str__ = __unicode__

def clean(self):
super().clean()

Expand Down

0 comments on commit d5e5bef

Please sign in to comment.