• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

iplweb / bpp / 18634744198

19 Oct 2025 07:00PM UTC coverage: 31.618% (-29.9%) from 61.514%
18634744198

push

github

mpasternak
Merge branch 'release/v202510.1270'

657 of 9430 branches covered (6.97%)

Branch coverage included in aggregate %.

229 of 523 new or added lines in 42 files covered. (43.79%)

11303 existing lines in 316 files now uncovered.

14765 of 39346 relevant lines covered (37.53%)

0.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

14.61
src/pbn_export_queue/tasks.py
1
from django.apps import apps
1✔
2

3
from django_bpp.celery_tasks import app
1✔
4
from long_running.util import wait_for_object
1✔
5

6
from .models import PBN_Export_Queue, SendStatus
1✔
7

8

9
@app.task
1✔
10
def task_sprobuj_wyslac_do_pbn(pk):
1✔
UNCOV
11
    p = wait_for_object(PBN_Export_Queue, pk)
×
UNCOV
12
    res = p.send_to_pbn()
×
13

UNCOV
14
    match res:
×
UNCOV
15
        case SendStatus.RETRY_LATER:
×
UNCOV
16
            task_sprobuj_wyslac_do_pbn.apply_async(args=[pk], countdown=5 * 60)
×
17

UNCOV
18
        case SendStatus.RETRY_SOON:
×
19
            # np. 423 Locked
UNCOV
20
            task_sprobuj_wyslac_do_pbn.apply_async(args=[pk], countdown=60)
×
21

UNCOV
22
        case SendStatus.RETRY_MUCH_LATER:
×
23
            # PraceSerwisoweException
UNCOV
24
            task_sprobuj_wyslac_do_pbn.apply_async(args=[pk], countdown=60 * 60 * 3)
×
25

UNCOV
26
        case SendStatus.FINISHED_OKAY:
×
27
            # After successful send, check for more items in queue
UNCOV
28
            check_and_send_next_in_queue()
×
UNCOV
29
            return
×
30

UNCOV
31
        case SendStatus.FINISHED_ERROR | SendStatus.RETRY_AFTER_USER_AUTHORISED:
×
UNCOV
32
            return
×
33

UNCOV
34
        case _:
×
35
            raise NotImplementedError(
36
                f"Return status for background send to PBN not supported {res=}"
37
            )
38

39

40
def check_and_send_next_in_queue():
1✔
41
    """Check if there are more items waiting to be sent and start sending them."""
42
    # Find the next item that hasn't been processed yet
43
    # Priority order:
44
    # 1. Items that were never attempted (wysylke_podjeto=None)
45
    # 2. Items that are waiting but not finished
UNCOV
46
    next_items = PBN_Export_Queue.objects.filter(
×
47
        wysylke_podjeto=None,
48
        wysylke_zakonczono=None,
49
    ).order_by("zamowiono")[:5]  # Process up to 5 items at once
50

UNCOV
51
    for item in next_items:
×
52
        # Start sending each item in background
53
        task_sprobuj_wyslac_do_pbn.delay(item.pk)
×
54

UNCOV
55
    if next_items:
×
56
        return len(next_items)
×
UNCOV
57
    return 0
×
58

59

60
@app.task
1✔
61
def kolejka_wyczysc_wpisy_bez_rekordow():
1✔
UNCOV
62
    for elem in PBN_Export_Queue.objects.all():
×
UNCOV
63
        if not elem.check_if_record_still_exists():
×
UNCOV
64
            elem.delete()
×
65

66

67
@app.task
1✔
68
def kolejka_ponow_wysylke_prac_po_zalogowaniu(pk):
1✔
69
    # Użytkownik o ID pk zalogował się.
70
    # Odśwież do wysyłki prace które były jego po zalogowaniu
UNCOV
71
    for elem in PBN_Export_Queue.objects.filter(
×
72
        retry_after_user_authorised=True, zamowil_id=pk, wysylke_zakonczono=None
73
    ):
UNCOV
74
        task_sprobuj_wyslac_do_pbn.delay(elem.pk)
×
75

76
    # ... ale i odświez prace wszystkich użytkowników, którzy mają jego konto
77
    # jako konto do wysyłki:
UNCOV
78
    for elem in PBN_Export_Queue.objects.filter(
×
79
        retry_after_user_authorised=True,
80
        zamowil__przedstawiaj_w_pbn_jako_id=pk,
81
        wysylke_zakonczono=None,
82
    ):
UNCOV
83
        task_sprobuj_wyslac_do_pbn.delay(elem.pk)
×
84

85

86
@app.task
1✔
87
def queue_pbn_export_batch(app_label, model_name, record_ids, user_id):
1✔
88
    """
89
    Queue multiple records for PBN export in batch.
90

91
    Args:
92
        app_label: Django app label (e.g. 'bpp')
93
        model_name: Model name (e.g. 'wydawnictwo_ciagle')
94
        record_ids: List of record IDs to queue
95
        user_id: User ID who initiated the export
96
    """
UNCOV
97
    from django.contrib.auth import get_user_model
×
98

UNCOV
99
    from pbn_api.exceptions import AlreadyEnqueuedError
×
100

UNCOV
101
    User = get_user_model()
×
102

UNCOV
103
    try:
×
UNCOV
104
        user = User.objects.get(pk=user_id)
×
UNCOV
105
    except User.DoesNotExist:
×
UNCOV
106
        return
×
107

UNCOV
108
    model = apps.get_model(app_label, model_name)
×
109

UNCOV
110
    for record_id in record_ids:
×
UNCOV
111
        try:
×
UNCOV
112
            record = model.objects.get(pk=record_id)
×
UNCOV
113
            try:
×
UNCOV
114
                PBN_Export_Queue.objects.sprobuj_utowrzyc_wpis(user, record)
×
115
                # Send to PBN in background
UNCOV
116
                queue_entry = PBN_Export_Queue.objects.filter_rekord_do_wysylki(
×
117
                    record
118
                ).first()
UNCOV
119
                if queue_entry:
×
UNCOV
120
                    task_sprobuj_wyslac_do_pbn.delay(queue_entry.pk)
×
UNCOV
121
            except AlreadyEnqueuedError:
×
122
                # Record already in queue, skip
UNCOV
123
                pass
×
UNCOV
124
        except model.DoesNotExist:
×
125
            # Skip records that don't exist
UNCOV
126
            pass
×
127
        except Exception:
×
128
            # Skip records with other errors
129
            pass
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc