-
Notifications
You must be signed in to change notification settings - Fork 0
chore(organization): sync organization data into firebase. #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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", | ||
| ], | ||
| ) |
25 changes: 25 additions & 0 deletions
25
apps/project/migrations/0009_organization_firebase_last_pushed_and_more.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Generated by Django 5.2.4 on 2025-07-25 09:42 | ||
|
|
||
| import apps.project.models | ||
| import django_choices_field.fields | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('project', '0008_merge_20250722_0437'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name='organization', | ||
| name='firebase_last_pushed', | ||
| field=models.DateTimeField(blank=True, help_text='The latest time when organization was pushed to firebase', null=True), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='organization', | ||
| name='firebase_push_status', | ||
| field=django_choices_field.fields.IntegerChoicesField(blank=True, choices=[(1, 'Pending'), (2, 'Processing'), (3, 'Success'), (4, 'Failed')], choices_enum=apps.project.models.FirebasePushStatusEnum, null=True), | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule firebase
updated
7 files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.