Coverage for actions/utils.py: 0.00%
16 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-13 17:07 -0700
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-13 17:07 -0700
1import datetime
3from django.contrib.contenttypes.models import ContentType
4from django.utils import timezone
6from .models import Action
9def create_action(user, verb, target=None):
10 # check for any similar action made in the last minute
11 now = timezone.now()
12 last_minute = now - datetime.timedelta(seconds=60)
13 similar_actions = Action.objects.filter(
14 user_id=user.id,
15 verb=verb,
16 created__gte=last_minute,
17 )
18 if target:
19 target_ct = ContentType.objects.get_for_model(target)
20 similar_actions = similar_actions.filter(
21 target_ct=target_ct,
22 target_id=target.id,
23 )
24 if not similar_actions:
25 # no existing actions found
26 action = Action(user=user, verb=verb, target=target)
27 action.save()
28 return True
29 return False