[Django API] homepage - models.py

from django.db import models
from ckeditor.fields import RichTextField
from ckeditor_uploader.fields import RichTextUploadingField
from filer.fields.image import FilerImageField
from filer.fields.file import FilerFileField
from django.shortcuts import get_object_or_404
from django.utils import timezone
from django.contrib.auth.models import User
from crum import get_current_user
from datetime import date

import logging

logger = logging.getLogger('aipass')

# Internal Code

STATUS = (
    (1, 'Active'),
    (2, 'Inactive'),
)


# Web 

class Code(models.Model):
    pid                 = models.ForeignKey('self', on_delete=models.PROTECT, related_name='homepage_code_pid', blank=True, null=True)
    name                = models.CharField(max_length=200)
    title               = models.CharField(max_length=500, blank=True, null=True)
    description         = models.TextField(blank=True, null=True)
    display_order       = models.IntegerField(default=0, blank=True, null=True)

    status              = models.IntegerField(choices=STATUS, default=1)
    creator             = models.ForeignKey(User, on_delete=models.PROTECT, related_name='homepage_code_creator', null=True, blank=True)      
    modifier            = models.ForeignKey(User, on_delete=models.PROTECT, related_name='homepage_code_modifier', null=True, blank=True)       
    created             = models.DateTimeField(default=timezone.now)
    modified            = models.DateTimeField(default=timezone.now)
    
    def __str__(self):
        # if self.pid:
        #     return_msg = str(self.pid) + '.' + str(self.name)
        # else:
        #     return_msg = str(self.name)
        return self.title

    def save(self, *args, **kwargs):
        ''' On save, update timestamps '''
        user = get_current_user()

        if not self.id:
            self.created = timezone.now()
            try:
                self.creator = user
            except:
                self.creator = self.creator
        self.modified = timezone.now()
        try:
            self.modifier = user
        except:
            self.modifier = self.modifier
        return super(Code, self).save(*args, **kwargs)

def get_code_id(code_name):
    try:
        type_id = Code.objects.get(name=code_name).id
    except:
        type_id = 0

    return {'pid': type_id}

class Content(models.Model):
    type                = models.ForeignKey(Code, on_delete=models.PROTECT, limit_choices_to=get_code_id('homepage_content_type'), related_name='content_type', blank=True, null=True)
    style               = models.ForeignKey(Code, on_delete=models.PROTECT, limit_choices_to=get_code_id('homepage_content_style'), related_name='content_style', blank=True, null=True)
    category            = models.ForeignKey(Code, on_delete=models.PROTECT, limit_choices_to=get_code_id('homepage_content_category'), related_name='content_category', blank=True, null=True)
    
    pid                 = models.ForeignKey('self', on_delete=models.PROTECT, related_name='homepage_content_pid', blank=True, null=True)
    
    name                = models.CharField(max_length=200)
    title               = models.CharField(max_length=500)
    description         = models.TextField(blank=True, null=True)
    
    url_type            = models.ForeignKey(Code, on_delete=models.PROTECT, limit_choices_to=get_code_id('homepage_url_type'), related_name='content_url_type', blank=True, null=True)
    url                 = models.CharField(max_length=500, blank=True)
    url_parameter       = models.CharField(max_length=500, blank=True)
    icon                = models.CharField(max_length=200, blank=True)
    view_count          = models.IntegerField(default=0, blank=True)
    display_order       = models.IntegerField(default=0, blank=True, null=True)
    
    content_body        = RichTextUploadingField(blank=True, null=True)

    image1              = FilerImageField(on_delete=models.PROTECT, null=True, blank=True, related_name="homepage_content_image1")
    image2              = FilerImageField(on_delete=models.PROTECT, null=True, blank=True, related_name="homepage_content_image2")
    image3              = FilerImageField(on_delete=models.PROTECT, null=True, blank=True, related_name="homepage_content_image3")
    file1               = FilerFileField(on_delete=models.PROTECT, null=True, blank=True, related_name="homepage_content_file1")
    file2               = FilerFileField(on_delete=models.PROTECT, null=True, blank=True, related_name="homepage_content_file2")
    file3               = FilerFileField(on_delete=models.PROTECT, null=True, blank=True, related_name="homepage_content_file3")
    text1               = models.CharField(max_length=500, blank=True)
    text2               = models.CharField(max_length=500, blank=True)
    text3               = models.CharField(max_length=500, blank=True)
    int1                = models.IntegerField(default=0, blank=True)
    int2                = models.IntegerField(default=0, blank=True)
    int3                = models.IntegerField(default=0, blank=True)

    status              = models.IntegerField(choices=STATUS, default=1)

    creator             = models.ForeignKey(User, on_delete=models.PROTECT, related_name='homepage_content_creator', null=True, blank=True)      
    modifier            = models.ForeignKey(User, on_delete=models.PROTECT, related_name='homepage_content_modifier', null=True, blank=True)       
    created             = models.DateTimeField(default=timezone.now)
    modified            = models.DateTimeField(default=timezone.now)
    
    def __str__(self):
        if self.pid:
            return_msg = str(self.pid) + '.' + str(self.name)
        else:
            return_msg = str(self.name)
        return return_msg

    def save(self, *args, **kwargs):
        ''' On save, update timestamps '''
        user = get_current_user()

        if not self.id:
            self.created = timezone.now()
            try:
                self.creator = user
            except:
                self.creator = self.creator
        self.modified = timezone.now()
        try:
            self.modifier = user
        except:
            self.modifier = self.modifier
        return super(Content, self).save(*args, **kwargs)


댓글

이 블로그의 인기 게시물

[Django Install] 9.1 sc제일은행 nginx-django 연결

[Django APP] django-widget-tweaks

[Django App] django-user-agents