exercise_types with image and translations

This commit is contained in:
Bossanyi Tibor 2020-07-18 06:39:49 +02:00
parent 8983c7105b
commit c9f22e0f96
4 changed files with 134 additions and 16 deletions

View File

@ -1,7 +1,51 @@
from django.contrib import admin
from django.utils.html import format_html
from django.utils.translation import ugettext_lazy as _
from .models import ExerciseType
from .models import ExerciseTypeImage
from .models import ExerciseTypeTranslation
admin.site.register(ExerciseType)
admin.site.register(ExerciseTypeImage)
class ImageInline(admin.StackedInline):
model = ExerciseTypeImage
extra = 0
fields = ["name", "url", "type", "get_image_preview"]
readonly_fields = ("get_image_preview",)
def get_image_preview(self, obj):
image_url = '/media/' + str(obj.url)
if obj.pk:
return format_html('<img src="{url}" title="{url}" width="30%" height="30%"/> ' \
.format(url=image_url))
get_image_preview.short_description = _("Image Preview")
class TranslationInline(admin.TabularInline):
model = ExerciseTypeTranslation
fields = ('language_code', 'name', 'description')
extra = 0
class ExerciseTypeAdmin(admin.ModelAdmin):
list_display = ('exercise_type_id', 'name_colored', 'active')
search_fields = ['name']
def name_colored(self, obj):
if obj.active:
color_code = '7bc863'
else:
color_code = 'C20000'
html = '<span style="color: #{};">{}</span>˓→'.format(color_code, obj.name)
return format_html(html)
name_colored.admin_order_field = 'name'
name_colored.short_description = 'name'
inlines = [
ImageInline,
TranslationInline
]
admin.site.register(ExerciseType, ExerciseTypeAdmin)

View File

@ -1,33 +1,80 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _
class ExerciseType(models.Model):
class UnitTypes(models.TextChoices):
REPEAT = 'repeat'
SECOND = 'second'
MINUTE = 'minute'
METER = 'meter'
CM= 'centimeter'
PERCENT = 'percent'
CALORIES = 'calories'
KG = 'kilogram'
exercise_type_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=100)
description = models.TextField(max_length=1000, blank=True, null=True)
unit = models.CharField(max_length=50, blank=True, null=True)
name = models.CharField(max_length=100, help_text='The name should be in English here')
description = models.TextField(max_length=1000, blank=True, null=True, help_text='The description should be in '
'English here')
unit = models.CharField(choices=UnitTypes.choices, default=UnitTypes.REPEAT, max_length=50, blank=True, null=True)
unit_quantity = models.BooleanField(default=0, blank=True, null=True)
unit_quantity_unit = models.CharField(max_length=50, blank=True, null=True)
unit_quantity_unit = models.CharField(choices=UnitTypes.choices, default=UnitTypes.KG, max_length=50, blank=True, null=True)
active = models.BooleanField(default=0, blank=True, null=True)
class Meta:
managed = False
db_table = 'exercise_type'
verbose_name = _("Exercise")
verbose_name_plural = _("Exercises")
def __str__(self):
return self.name
class ExerciseTypeImage(models.Model):
class ImageTypes(models.TextChoices):
IMAGE = 'image'
VIDEO = 'video'
MENU = 'menu'
image_id = models.AutoField(primary_key=True)
exercise_type_id = models.IntegerField(blank=True, null=True)
exercise_type = models.ForeignKey(ExerciseType, on_delete=models.CASCADE)
name = models.CharField(max_length=50, blank=True, null=True)
type = models.CharField(choices=ImageTypes.choices, default=ImageTypes.IMAGE, max_length=50, blank=True, null=True)
url = models.CharField(max_length=200, blank=True, null=True)
url = models.ImageField(upload_to='images/', help_text='The menu image size is 1366x768')
class Meta:
managed = False
db_table = 'exercise_type_image'
db_table = 'exercise_type_image'
verbose_name = _("Image")
verbose_name_plural = _("Images")
def __str__(self):
return self.name
'''
def image_tag(self):
from django.utils.html import escape
return u'<img src="%s" />' % escape(self.obj.url)
image_tag.short_description = 'Image'
image_tag.allow_tags = True
'''
class ExerciseTypeTranslation(models.Model):
class LanguageTypes(models.TextChoices):
EN = "en"
HU = "hu"
translation_id = models.AutoField(primary_key=True)
exercise_type = models.ForeignKey(ExerciseType, on_delete=models.CASCADE)
language_code = models.CharField(max_length=2, choices=LanguageTypes.choices, default=LanguageTypes.HU)
name = models.CharField(max_length=100)
description = models.TextField(max_length=1000, blank=True, null=True)
class Meta:
db_table = 'exercise_type_translation'
verbose_name = _("Translation")
verbose_name_plural = _("Translations")
def __str__(self):
return self.name

View File

@ -123,3 +123,29 @@ USE_TZ = True
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_true': {
'()': 'django.utils.log.RequireDebugTrue',
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'filters': ['require_debug_true'],
},
},
'loggers': {
'mylogger': {
'handlers': ['console'],
'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
'propagate': True,
},
},
}

View File

@ -17,10 +17,11 @@ Including another URLconf
"""
from django.contrib import admin
from django.urls import path
from django.urls import path, include
from . import views
from django.conf import settings #
from django.conf.urls.static import static # n
urlpatterns = [
#path('', views.index, name = "index"),
path('admin/', admin.site.urls),
]
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)