V1.30.11 models simplification
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import os
|
||||
from django.contrib import admin
|
||||
from django.utils.html import format_html
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from ..db_router import TestRouter
|
||||
from ..models.notification import Notification
|
||||
|
||||
class NotificationAdmin(admin.ModelAdmin):
|
||||
@@ -26,7 +28,42 @@ class NotificationAdmin(admin.ModelAdmin):
|
||||
|
||||
clone_notification.short_description = "Clone the selected notification"
|
||||
|
||||
actions = [clone_notification]
|
||||
def sync_notification(self, request, queryset):
|
||||
TestRouter.cloning = True
|
||||
for notif in queryset:
|
||||
live_qs = Notification.objects.using('live').raw(f'SELECT * from notification WHERE notification_id = {notif.pk}')
|
||||
if len(list(live_qs)) == 0:
|
||||
print("New notification to sync")
|
||||
live_notif = Notification()
|
||||
live_notif.pk = None
|
||||
live_notif.internal_name = notif.internal_name
|
||||
live_notif.internal_description = notif.internal_description
|
||||
live_notif.message_title = notif.message_title
|
||||
live_notif.message_body = notif.message_body
|
||||
live_notif.image_url = notif.image_url
|
||||
live_notif.schedule_date = notif.schedule_date
|
||||
live_notif.schedule_hook = notif.schedule_hook
|
||||
live_notif.schedule_sql = notif.schedule_sql
|
||||
live_notif.active = notif.active
|
||||
live_notif.save()
|
||||
else:
|
||||
for live_notif in live_qs:
|
||||
print(f'Update notification to sync {notif.internal_name} - Live: {live_notif.internal_name}')
|
||||
live_notif.internal_name = notif.internal_name
|
||||
live_notif.internal_description = notif.internal_description
|
||||
live_notif.message_title = notif.message_title
|
||||
live_notif.message_body = notif.message_body
|
||||
live_notif.image_url = notif.image_url
|
||||
live_notif.schedule_date = notif.schedule_date
|
||||
live_notif.schedule_hook = notif.schedule_hook
|
||||
live_notif.schedule_sql = notif.schedule_sql
|
||||
live_notif.active = notif.active
|
||||
live_notif.save()
|
||||
TestRouter.cloning = False
|
||||
|
||||
sync_notification.short_description = "Syncrchnonize with the live database"
|
||||
|
||||
actions = [clone_notification, sync_notification]
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ class TestRouter:
|
||||
A router to control all database operations on models
|
||||
"""
|
||||
live_app_labels = {'controlling'}
|
||||
cloning = False
|
||||
|
||||
def db_for_read(self, model, **hints):
|
||||
if model._meta.app_label == 'controlling':
|
||||
@@ -11,8 +12,10 @@ class TestRouter:
|
||||
return 'default'
|
||||
|
||||
def db_for_write(self, model, **hints):
|
||||
if model._meta.app_label == 'controlling':
|
||||
if model._meta.db_table == 'notification_history':
|
||||
if self.cloning == True:
|
||||
return 'live'
|
||||
elif model._meta.app_label == 'controlling':
|
||||
if model._meta.db_table == 'notification_history' or self.cloning == True:
|
||||
return 'live'
|
||||
else:
|
||||
raise Exception("This table cannot be changed!")
|
||||
|
||||
@@ -18,4 +18,6 @@ from .training_plan_day import TrainingPlanDay, TrainingPlanDayTranslation
|
||||
from .controlling import Controlling
|
||||
from .sports import Sport, SportTranslation
|
||||
from .app_text import AppText, AppTextTranslation
|
||||
from .notification import Notification
|
||||
from .notification import NotificationHistory, Notification
|
||||
from .customer import Customer
|
||||
from .exercises import Exercises
|
||||
@@ -0,0 +1,32 @@
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from ..models.sports import Sport
|
||||
from ..models.exercises import Exercises
|
||||
|
||||
class Customer(models.Model):
|
||||
customer_id = models.BigAutoField(primary_key=True)
|
||||
name = models.CharField(max_length=100, help_text='Last name', verbose_name=_("name"))
|
||||
firstname = models.CharField(max_length=100, help_text='First name', verbose_name=_("firstname"))
|
||||
email = models.CharField(max_length=100)
|
||||
sport = models.ForeignKey(Sport, on_delete=models.CASCADE)
|
||||
goal = models.CharField(max_length=20)
|
||||
fitness_level = models.CharField(max_length=20)
|
||||
date_add = models.DateField()
|
||||
synced_date = models.DateTimeField(blank=True,null=True)
|
||||
firebase_reg_token = models.CharField(max_length=255, blank=True, null=True)
|
||||
|
||||
def has_add_permission(self, request):
|
||||
return False
|
||||
|
||||
def has_delete_permission(self, request, obj=None):
|
||||
return False
|
||||
|
||||
class Meta:
|
||||
db_table = 'customer'
|
||||
verbose_name = _("Customer")
|
||||
verbose_name_plural = _("Customers")
|
||||
app_label = 'controlling'
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
@@ -0,0 +1,25 @@
|
||||
from django.db import models
|
||||
|
||||
from ..models.exercise_type import ExerciseType
|
||||
|
||||
class Exercises(models.Model):
|
||||
exercise_id = models.BigAutoField(primary_key=True)
|
||||
|
||||
customer_id = models.PositiveIntegerField()
|
||||
# exercise_type_id = models.IntegerField()
|
||||
exercise_type = models.ForeignKey(ExerciseType, on_delete=models.CASCADE)
|
||||
date_add = models.DateField()
|
||||
quantity = models.DecimalField(decimal_places=0, max_digits=6)
|
||||
unit = models.CharField(max_length=20)
|
||||
unit_quantity = models.DecimalField(decimal_places=0, max_digits=6,blank=True, null=True)
|
||||
|
||||
|
||||
def has_add_permission(self, request):
|
||||
return False
|
||||
|
||||
def has_delete_permission(self, request, obj=None):
|
||||
return False
|
||||
|
||||
class Meta:
|
||||
db_table = 'exercises'
|
||||
app_label = 'controlling'
|
||||
@@ -1,6 +1,8 @@
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from ..models.customer import Customer
|
||||
|
||||
class Notification(models.Model):
|
||||
notification_id = models.AutoField(primary_key=True)
|
||||
message_title = models.CharField(max_length=50)
|
||||
@@ -19,4 +21,18 @@ class Notification(models.Model):
|
||||
verbose_name_plural = _("Notifications")
|
||||
|
||||
def __str__(self):
|
||||
return self.internal_name
|
||||
return self.internal_name
|
||||
|
||||
class NotificationHistory(models.Model):
|
||||
notification_history_id = models.AutoField(primary_key=True)
|
||||
notification = models.ForeignKey(Notification, on_delete=models.CASCADE)
|
||||
customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
|
||||
response = models.CharField(max_length=255)
|
||||
notification_date = models.DateTimeField(blank=True, null=True)
|
||||
|
||||
class Meta:
|
||||
db_table = 'notification_history'
|
||||
app_label = 'controlling'
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.notification};{self.customer};{self.response}'
|
||||
@@ -1,10 +1,8 @@
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from ckeditor.fields import RichTextField
|
||||
|
||||
from .enums import LanguageTypes
|
||||
|
||||
|
||||
class Sport(models.Model):
|
||||
sport_id = models.AutoField(primary_key=True)
|
||||
name = models.CharField(max_length=200, help_text='Unique name',
|
||||
|
||||
@@ -10,6 +10,8 @@ LOCALE_PATHS = [os.path.join(BASE_DIR, 'locale')]
|
||||
# SECURITY WARNING: keep the secret key used in production secret!
|
||||
SECRET_KEY = os.environ['DJANGO_KEY']
|
||||
|
||||
os.environ["WORKOUTTEST_SETTING"] = "DEPLOY"
|
||||
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = False
|
||||
|
||||
|
||||
Reference in New Issue
Block a user