Skip to content

Commit

Permalink
Fix exception on empty target date, invert tags condition
Browse files Browse the repository at this point in the history
  • Loading branch information
kaenganxt committed Feb 14, 2024
1 parent 9cc22b3 commit 0181f64
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/ekklesia_portal/datamodel.py
Expand Up @@ -426,6 +426,9 @@ def voting_start(self):

@property
def voting_end(self):
if self.target is None:
return

return self.target - timedelta(seconds=1)


Expand Down
14 changes: 7 additions & 7 deletions src/ekklesia_portal/lib/discourse.py
Expand Up @@ -25,25 +25,25 @@ class DiscourseError(Exception):
pass


def create_discourse_topic(config: DiscourseConfig, topic: DiscourseTopic, without_tags: bool = False):
def create_discourse_topic(config: DiscourseConfig, topic: DiscourseTopic, with_tags: bool = True):

headers = {'accept': 'application/json', 'api-key': config.api_key, 'api-username': config.api_username}

if without_tags:
req = {'raw': topic.content, 'category': config.category, 'title': topic.title}
else:
if with_tags:
req = {'raw': topic.content, 'category': config.category, 'title': topic.title, 'tags': topic.tags}
else:
req = {'raw': topic.content, 'category': config.category, 'title': topic.title}

with start_action(action_type="discourse_post", without_tags=without_tags) as action:
with start_action(action_type="discourse_post", with_tags=with_tags) as action:
resp = requests.post(f"{config.base_url}/posts.json", json=req, headers=headers)
action.add_success_fields(response=resp.json())

try:
resp.raise_for_status()
except HTTPError as e:
if e.response.status_code == 422 and not without_tags:
if e.response.status_code == 422 and with_tags:
# Try again once without tags
return create_discourse_topic(config, topic, True)
return create_discourse_topic(config, topic, with_tags=False)
else:
raise DiscourseError(e)

Expand Down

0 comments on commit 0181f64

Please sign in to comment.