Skip to content

Commit d04757f

Browse files
committed
back to 💯
1 parent 5b582d0 commit d04757f

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

democrasite/webiscite/models.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,9 @@ def create_from_github(
155155
self._bill.full_clean()
156156
self._bill.save()
157157
logger.info("PR %s: Bill %s created", pull_request.number, self._bill.id)
158+
bill = self._bill
158159

159-
return self._bill
160+
return bill # noqa: RET504
160161

161162
@contextmanager
162163
def _create_submit_task(self) -> Iterator[PeriodicTask]:
@@ -165,7 +166,7 @@ def _create_submit_task(self) -> Iterator[PeriodicTask]:
165166
Returns:
166167
The task that was scheduled
167168
"""
168-
# This can be extracted to a signal if we want to support other creation methods
169+
# This might be better as a signal but I want to keep it localized
169170
voting_ends, __ = IntervalSchedule.objects.get_or_create(
170171
every=settings.WEBISCITE_VOTING_PERIOD, period=IntervalSchedule.DAYS
171172
)
@@ -182,9 +183,15 @@ def _create_submit_task(self) -> Iterator[PeriodicTask]:
182183
try:
183184
yield submit_task
184185
finally:
185-
# This might be better as a signal but I want to keep it localized
186-
if not isinstance(self._bill.id, int):
187-
raise TypeError("self._bill was not saved in the submit task context")
186+
if not (
187+
hasattr(self, "_bill")
188+
and hasattr(self._bill, "id")
189+
and isinstance(self._bill.id, int)
190+
):
191+
raise AttributeError(
192+
"self._bill was not saved in the submit task context"
193+
)
194+
188195
submit_task.name = f"bill_submit:{self._bill.id}"
189196
submit_task.args = json.dumps([self._bill.id])
190197
submit_task.save()
@@ -194,6 +201,8 @@ def _create_submit_task(self) -> Iterator[PeriodicTask]:
194201
submit_task.name,
195202
)
196203

204+
del self._bill
205+
197206

198207
class Bill(StatusModel, TimeStampedModel):
199208
"""Model for a proposal to merge a particular pull request into the main branch"""

democrasite/webiscite/tests/test_models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ def test_create_from_github(self, user: User):
8686
assert bill.pk is not None
8787
assert bill._submit_task.enabled is True # noqa: SLF001
8888

89+
def test__create_submit_task(self):
90+
# just hit the error branch of finally clause
91+
with (
92+
pytest.raises(
93+
AttributeError,
94+
match="self._bill was not saved in the submit task context",
95+
),
96+
Bill.objects._create_submit_task(), # noqa: SLF001
97+
):
98+
pass
99+
89100

90101
class TestBill:
91102
def test_unique_open_pull_request(self, bill: Bill):

0 commit comments

Comments
 (0)