-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebase.py
More file actions
96 lines (82 loc) · 3.66 KB
/
firebase.py
File metadata and controls
96 lines (82 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import logging
import typing
from celery import shared_task
from django.utils import timezone
from firebase_admin.db import Reference as FbReference
from pyfirebase_mapswipe import models as firebase_models
from pyfirebase_mapswipe import utils as firebase_utils
from apps.project.models import FirebasePushStatusEnum, Organization
from main.config import Config
from main.logging import log_extra
logger = logging.getLogger(__name__)
class InvalidOrganizationPushException(Exception): ...
def handle_new_organization_on_firebase(organization: Organization, organization_ref: FbReference):
organization_data = firebase_models.FbOrganisation(
name=organization.name,
nameKey="",
description=organization.description if organization.description else firebase_models.UNDEFINED,
abbreviation=organization.abbreviation if organization.abbreviation else firebase_models.UNDEFINED,
isArchived=organization.is_archived,
)
organization_ref.set(
value={
**firebase_utils.serialize(organization_data),
},
)
def handle_organization_update_on_firebase(organization: Organization, organization_ref: FbReference):
organization_ref.update(
value=firebase_utils.serialize(
firebase_models.FbOrganisation(
name=organization.name,
nameKey="",
description=organization.description if organization.description else firebase_models.UNDEFINED,
abbreviation=organization.abbreviation if organization.abbreviation else firebase_models.UNDEFINED,
isArchived=organization.is_archived,
),
),
)
@shared_task
def push_organization_to_firebase(organization_id: int):
organization = Organization.objects.filter(id=organization_id).first()
if not organization:
return
organization.update_firebase_push_status(FirebasePushStatusEnum.PROCESSING)
try:
organization_ref = Config.FIREBASE_HELPER.ref(
Config.FirebaseKeys.organization(organization.id),
)
fb_organization: typing.Any = organization_ref.get()
if not organization.firebase_last_pushed:
if fb_organization is not None:
logger.error(
"push_to_firebase found a organization already in firebase when creating a organization",
extra=log_extra({"organization": organization.pk}),
)
raise InvalidOrganizationPushException
handle_new_organization_on_firebase(organization, organization_ref)
else:
if fb_organization is None:
logger.error(
"push_to_firebase did not find organization in firebase when updating a organization",
extra=log_extra({"organization": organization.pk}),
)
raise InvalidOrganizationPushException
handle_organization_update_on_firebase(organization, organization_ref)
except InvalidOrganizationPushException:
organization.update_firebase_push_status(FirebasePushStatusEnum.FAILED)
except Exception:
logger.error(
"push_to_firebase failed",
extra=log_extra({"organization": organization.pk}),
exc_info=True,
)
organization.update_firebase_push_status(FirebasePushStatusEnum.FAILED)
else:
organization.firebase_last_pushed = timezone.now()
organization.update_firebase_push_status(FirebasePushStatusEnum.SUCCESS, commit=False)
organization.save(
update_fields=[
"firebase_last_pushed",
"firebase_push_status",
],
)