├── notifire ├── __init__.py ├── wsgi.py ├── firebase.py ├── connector.py ├── urls.py └── settings.py ├── requirements.pip ├── firestore.indexes.json ├── .firebaserc ├── firebase.json ├── firestore.rules ├── manage.py ├── LICENSE ├── README.md ├── .gitignore ├── templates └── notifire.html └── static └── js └── firebase.js /notifire/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /requirements.pip: -------------------------------------------------------------------------------- 1 | Django==2.2.9 2 | firebase-admin==3.2.1 -------------------------------------------------------------------------------- /firestore.indexes.json: -------------------------------------------------------------------------------- 1 | { 2 | "indexes": [], 3 | "fieldOverrides": [] 4 | } 5 | -------------------------------------------------------------------------------- /.firebaserc: -------------------------------------------------------------------------------- 1 | { 2 | "projects": { 3 | "default": "firegame-67784" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /firebase.json: -------------------------------------------------------------------------------- 1 | { 2 | "firestore": { 3 | "rules": "firestore.rules", 4 | "indexes": "firestore.indexes.json" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /firestore.rules: -------------------------------------------------------------------------------- 1 | rules_version = '2'; 2 | service cloud.firestore { 3 | match /databases/{database}/documents { 4 | match /{document=**} { 5 | allow write: if request.auth.uid != null; 6 | allow read: if true; 7 | } 8 | } 9 | } -------------------------------------------------------------------------------- /notifire/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for notifire project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'notifire.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'notifire.settings') 9 | try: 10 | from django.core.management import execute_from_command_line 11 | except ImportError as exc: 12 | raise ImportError( 13 | "Couldn't import Django. Are you sure it's installed and " 14 | "available on your PYTHONPATH environment variable? Did you " 15 | "forget to activate a virtual environment?" 16 | ) from exc 17 | execute_from_command_line(sys.argv) 18 | 19 | 20 | if __name__ == '__main__': 21 | main() 22 | -------------------------------------------------------------------------------- /notifire/firebase.py: -------------------------------------------------------------------------------- 1 | import time 2 | from datetime import timedelta 3 | from uuid import uuid4 4 | 5 | from firebase_admin import firestore, initialize_app 6 | 7 | __all__ = ['send_to_firebase', 'update_firebase_snapshot'] 8 | 9 | initialize_app() 10 | 11 | 12 | def send_to_firebase(raw_notification): 13 | db = firestore.client() 14 | start = time.time() 15 | db.collection('notifications').document(str(uuid4())).create(raw_notification) 16 | end = time.time() 17 | spend_time = timedelta(seconds=end - start) 18 | return spend_time 19 | 20 | 21 | def update_firebase_snapshot(snapshot_id): 22 | start = time.time() 23 | db = firestore.client() 24 | db.collection('notifications').document(snapshot_id).update( 25 | {'is_read': True} 26 | ) 27 | end = time.time() 28 | spend_time = timedelta(seconds=end - start) 29 | return spend_time 30 | -------------------------------------------------------------------------------- /notifire/connector.py: -------------------------------------------------------------------------------- 1 | from django.http import JsonResponse 2 | 3 | from .firebase import send_to_firebase, update_firebase_snapshot 4 | 5 | __all__ = ['send_message', 'make_as_read'] 6 | 7 | 8 | def send_message(request): 9 | message = request.GET.get('message', "") 10 | raw_notification = { 11 | "message": message, 12 | "is_read": False 13 | } 14 | spend_time = send_to_firebase(raw_notification) 15 | return JsonResponse({"message": 'Notification created', "spend_time": spend_time}, status=201) 16 | 17 | 18 | def make_as_read(request): 19 | snapshot_id = request.GET.get('snapshot_id') 20 | if not snapshot_id: 21 | return JsonResponse({'message': 'You have to send a snapshot id'}, status=400) 22 | 23 | spend_time = update_firebase_snapshot(snapshot_id) 24 | return JsonResponse(data={'message': 'OK', 'spend_time': spend_time}, status=200) 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Can 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Notifire Project 2 | ================= 3 |  4 |  5 |  6 | 7 | # Firebase example using with Django 8 | 9 | 10 | Installation 11 | ================ 12 | ```sh 13 | $ git clone -repository.git- 14 | $ cd notifire 15 | $ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/google_application_credentials.json 16 | $ python manage.py runserver 17 | ``` 18 | 19 | Directory layout 20 | ================ 21 | 22 | Tracker's directory structure looks as follows:: 23 | 24 | notifire/ 25 | ├── notifire 26 | │ ├── __init__.py 27 | │ ├── connector.py 28 | │ ├── firebase.py 29 | │ ├── settings.py 30 | │ ├── urls.py 31 | │ ├── wsgi.py 32 | └── templates 33 | │ ├── notifire.html 34 | └── static 35 | │ ├── js 36 | │ │ └── firebase.js 37 | └── .gitignore 38 | └── manage.py 39 | └── README.md 40 | └── LICENCE 41 | 42 | Licence 43 | ================ 44 | MIT -------------------------------------------------------------------------------- /notifire/urls.py: -------------------------------------------------------------------------------- 1 | """notifire URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/3.0/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path 18 | from django.views.generic import TemplateView 19 | 20 | from .connector import send_message, make_as_read 21 | 22 | urlpatterns = [ 23 | path('admin/', admin.site.urls), 24 | path('', TemplateView.as_view(template_name='notifire.html')), 25 | path('ajax/send-message/', send_message, name='send_message'), 26 | path('ajax/make-as-read/', make_as_read, name='make_as_read') 27 | ] 28 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | firebase-debug.log* 8 | 9 | # Firebase cache 10 | .firebase/ 11 | 12 | # Firebase config 13 | 14 | # Uncomment this if you'd like others to create their own Firebase project. 15 | # For a team working on the same Firebase project(s), it is recommended to leave 16 | # it commented so all members can deploy to the same project(s) in .firebaserc. 17 | # .firebaserc 18 | 19 | # Runtime data 20 | pids 21 | *.pid 22 | *.seed 23 | *.pid.lock 24 | 25 | # Directory for instrumented libs generated by jscoverage/JSCover 26 | lib-cov 27 | 28 | # Coverage directory used by tools like istanbul 29 | coverage 30 | 31 | # nyc test coverage 32 | .nyc_output 33 | 34 | 35 | # node-waf configuration 36 | .lock-wscript 37 | 38 | # Compiled binary addons (http://nodejs.org/api/addons.html) 39 | build/Release 40 | 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Output of 'npm pack' 49 | *.tgz 50 | 51 | # Yarn Integrity file 52 | .yarn-integrity 53 | 54 | # dotenv environment variables file 55 | .env 56 | 57 | .idea 58 | *.pyc -------------------------------------------------------------------------------- /templates/notifire.html: -------------------------------------------------------------------------------- 1 | {% load staticfiles %} 2 | 3 | 4 |
5 | 6 | 7 | 8 |` + snapshot.data().message + `
42 |