-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add swapper * add tests and enable swapper * more versions of python * add databases * fix tests * migrate to mariadb * fix test model * add tastypie test * update docs * fix tests for swapped models * add tests for admin panel * cleanup tests * add test device swap owner * fix links * remove pyest.ini * fix docs * add notes for tests * removed redundant model * split api tests * remove comment code * update docs * update to create default device only when required * add tests for swapped model --------- Co-authored-by: Mojca Rojko <[email protected]>
- Loading branch information
Showing
24 changed files
with
399 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -380,6 +380,74 @@ logins on the same device, you do not wish the old user to receive messages whil | |
Via DRF, any creation of device with an already existing registration ID will be transformed into an update. | ||
If done manually, you are responsible for deleting the old device entry. | ||
|
||
Using custom FCMDevice model | ||
---------------------------- | ||
|
||
If there's a need to store additional information or change type of fields in the FCMDevice model. | ||
You could simple override this model. To do this, inherit your model from the AbstractFCMDevice class. | ||
|
||
In your ``your_app/models.py``: | ||
|
||
.. code-block:: python | ||
import uuid | ||
from django.db import models | ||
from fcm_django.models import AbstractFCMDevice | ||
class CustomDevice(AbstractFCMDevice): | ||
# fields could be overwritten | ||
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) | ||
# could be added new fields | ||
updated_at = models.DateTimeField(auto_now=True) | ||
In your ``settings.py``: | ||
|
||
.. code-block:: python | ||
FCM_DJANGO_FCMDEVICE_MODEL = "your_app.CustomDevice" | ||
In the DB will be two tables one that was created by this package and other your own. New data will appears only in your own table. | ||
If you don't want default table appears in the DB then you should remove ``fcm_django`` out of ``INSTALLED_APPS`` at ``settings.py``: | ||
|
||
.. code-block:: python | ||
INSTALLED_APPS = ( | ||
... | ||
# "fcm_django", - remove this line | ||
"your_app", # your app should appears | ||
... | ||
) | ||
After setup your own ``Model`` don't forget to create ``migrations`` for your app and call ``migrate`` command. | ||
|
||
After removing ``"fcm_django"`` out of ``INSTALLED_APPS``. You will need to re-register the Device in order to see it in the admin panel. | ||
This can be accomplished as follows at ``your_app/admin.py``: | ||
|
||
.. code-block:: python | ||
from django.contrib import admin | ||
from fcm_django.admin import DeviceAdmin | ||
from your_app.models import CustomDevice | ||
admin.site.unregister(CustomDevice) | ||
admin.site.register(CustomDevice, DeviceAdmin) | ||
If you choose to move forward with swapped models then: | ||
|
||
1. On existed project you have to keep in mind there are required manual work to move data from one table to anther. | ||
2. If there's any tables with FK to swapped model then you have to deal with them on your own. | ||
|
||
Note: This functionality based on `Swapper <https://pypi.org/project/swapper/>`_ that based on functionality | ||
that allow to use a `custom User model <https://docs.djangoproject.com/en/4.2/topics/auth/customizing/#substituting-a-custom-user-model>`_. | ||
So this functionality have the same limitations. | ||
The most is important limitation it is that is difficult to start out with a default (non-swapped) model | ||
and then later to switch to a swapped implementation without doing some migration hacking. | ||
|
||
Python 3 support | ||
---------------- | ||
- ``fcm-django`` is fully compatible with Python 3.7+ | ||
|
@@ -407,3 +475,17 @@ Contributing | |
|
||
To setup the development environment, simply do ``pip install -r requirements_dev.txt`` | ||
To manually run the pre-commit hook, run `pre-commit run --all-files`. | ||
|
||
Because there's possibility to use swapped models therefore tests contains two config files: | ||
|
||
1. with default settings and non swapped models ``settings/default.py`` | ||
2. and with overwritten settings only that required by swapper - ``settings/swap.py`` | ||
|
||
To run tests locally you could use ``pytest``, and if you need to check migrations on different DB then you have to specify environment variable ``DATABASE_URL`` ie | ||
|
||
.. code-block:: console | ||
export DATABASE_URL=postgres://postgres:[email protected]:5432/postgres | ||
export DJANGO_SETTINGS_MODULE=tests.settings.default | ||
# or export DJANGO_SETTINGS_MODULE=tests.settings.swap | ||
pytest |
This file contains 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 |
---|---|---|
|
@@ -201,6 +201,14 @@ Sending messages in bulk | |
# Or (send_message parameters include: messages, dry_run, app) | ||
FCMDevice.objects.send_message(Message(...)) | ||
Sending messages raises all the errors that ``firebase-admin`` raises, so make sure | ||
they are caught and dealt with in your application code: | ||
|
||
- ``FirebaseError`` – If an error occurs while sending the message to the FCM service. | ||
- ``ValueError`` – If the input arguments are invalid. | ||
|
||
For more info, see https://firebase.google.com/docs/reference/admin/python/firebase_admin.messaging#firebase_admin.messaging.BatchResponse | ||
|
||
Subscribing or Unsubscribing Users to topic | ||
------------------------------------------- | ||
|
||
|
@@ -372,6 +380,74 @@ logins on the same device, you do not wish the old user to receive messages whil | |
Via DRF, any creation of device with an already existing registration ID will be transformed into an update. | ||
If done manually, you are responsible for deleting the old device entry. | ||
|
||
Using custom FCMDevice model | ||
---------------------------- | ||
|
||
If there's a need to store additional information or change type of fields in the FCMDevice model. | ||
You could simple override this model. To do this, inherit your model from the AbstractFCMDevice class. | ||
|
||
In your ``your_app/models.py``: | ||
|
||
.. code-block:: python | ||
import uuid | ||
from django.db import models | ||
from fcm_django.models import AbstractFCMDevice | ||
class CustomDevice(AbstractFCMDevice): | ||
# fields could be overwritten | ||
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) | ||
# could be added new fields | ||
updated_at = models.DateTimeField(auto_now=True) | ||
In your ``settings.py``: | ||
|
||
.. code-block:: python | ||
FCM_DJANGO_FCMDEVICE_MODEL = "your_app.CustomDevice" | ||
In the DB will be two tables one that was created by this package and other your own. New data will appears only in your own table. | ||
If you don't want default table appears in the DB then you should remove ``fcm_django`` out of ``INSTALLED_APPS`` at ``settings.py``: | ||
|
||
.. code-block:: python | ||
INSTALLED_APPS = ( | ||
... | ||
# "fcm_django", - remove this line | ||
"your_app", # your app should appears | ||
... | ||
) | ||
After setup your own ``Model`` don't forget to create ``migrations`` for your app and call ``migrate`` command. | ||
|
||
After removing ``"fcm_django"`` out of ``INSTALLED_APPS``. You will need to re-register the Device in order to see it in the admin panel. | ||
This can be accomplished as follows at ``your_app/admin.py``: | ||
|
||
.. code-block:: python | ||
from django.contrib import admin | ||
from fcm_django.admin import DeviceAdmin | ||
from your_app.models import CustomDevice | ||
admin.site.unregister(CustomDevice) | ||
admin.site.register(CustomDevice, DeviceAdmin) | ||
If you choose to move forward with swapped models then: | ||
|
||
1. On existed project you have to keep in mind there are required manual work to move data from one table to anther. | ||
2. If there's any tables with FK to swapped model then you have to deal with them on your own. | ||
|
||
Note: This functionality based on `Swapper <https://pypi.org/project/swapper/>`_ that based on functionality | ||
that allow to use a `custom User model <https://docs.djangoproject.com/en/4.2/topics/auth/customizing/#substituting-a-custom-user-model>`_. | ||
So this functionality have the same limitations. | ||
The most is important limitation it is that is difficult to start out with a default (non-swapped) model | ||
and then later to switch to a swapped implementation without doing some migration hacking. | ||
|
||
Python 3 support | ||
---------------- | ||
- ``fcm-django`` is fully compatible with Python 3.7+ | ||
|
@@ -399,3 +475,17 @@ Contributing | |
|
||
To setup the development environment, simply do ``pip install -r requirements_dev.txt`` | ||
To manually run the pre-commit hook, run `pre-commit run --all-files`. | ||
|
||
Because there's possibility to use swapped models therefore tests contains two config files: | ||
|
||
1. with default settings and non swapped models ``settings/default.py`` | ||
2. and with overwritten settings only that required by swapper - ``settings/swap.py`` | ||
|
||
To run tests locally you could use ``pytest``, and if you need to check migrations on different DB then you have to specify environment variable ``DATABASE_URL`` ie | ||
|
||
.. code-block:: console | ||
export DATABASE_URL=postgres://postgres:[email protected]:5432/postgres | ||
export DJANGO_SETTINGS_MODULE=tests.settings.default | ||
# or export DJANGO_SETTINGS_MODULE=tests.settings.swap | ||
pytest |
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[tool.pytest.ini_options] | ||
pythonpath = ["."] | ||
DJANGO_SETTINGS_MODULE= "tests.testing_settings" | ||
DJANGO_SETTINGS_MODULE = "tests.settings.default" |
This file contains 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 |
---|---|---|
|
@@ -2,3 +2,4 @@ Django>=3.2 | |
django-tastypie>=0.14.0 | ||
djangorestframework>=3.9.2 | ||
firebase-admin>=6.2,<7 | ||
swapper>=1.3.0 |
This file contains 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 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
Empty file.
This file contains 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 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,7 @@ | ||
from .base import * | ||
|
||
INSTALLED_APPS += [ | ||
"fcm_django", | ||
] | ||
|
||
IS_SWAP = False # Only to distinguish which model is used in the tests |
This file contains 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,9 @@ | ||
from .base import * | ||
|
||
INSTALLED_APPS += [ | ||
"tests.swapped_models", | ||
] | ||
|
||
FCM_DJANGO_FCMDEVICE_MODEL = "swapped_models.CustomDevice" | ||
|
||
IS_SWAP = True # Only to distinguish which model is used in the tests |
Empty file.
This file contains 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,8 @@ | ||
from django.contrib import admin | ||
|
||
from fcm_django.admin import DeviceAdmin | ||
|
||
from .models import CustomDevice | ||
|
||
admin.site.unregister(CustomDevice) | ||
admin.site.register(CustomDevice, DeviceAdmin) |
Oops, something went wrong.