# Path: app/storage.py
# class OverwriteStorage(FileSystemStorage):
#     """
#     Class for implementing overwrite user's avatar when change.
#     """
# 
#     def get_available_name(self, name, max_length=None):
#         """
#         Returns a filename that's free on the target storage system, and
#         available for new content to be written to.
#         """
#         if self.exists(name):
#             os.remove(os.path.join(settings.MEDIA_ROOT, name))
# 
#         return name


import json
import logging
from collections import namedtuple
from django.db import models
from django.db.models import Avg, Count, Q
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from django.core.validators import MaxValueValidator, MinValueValidator
from django.urls import reverse
from django.utils.html import escape
from .storage import OverwriteStorage


logger = logging.getLogger('changes')


BookRelatedData = namedtuple('BookRelatedData', ['author', 'category', 'lang', 'user'])


# ----------------------------------------------------------------------------------------------------------------------
class TheUser(models.Model):
    """
    Class for user objects in database.
    """
    REMINDER_TEMPLATE = json.dumps({
        "common": {
            "fb_page": True,
            "fb_group": True,
            "twitter": True,
            "vk": True,
            "disabled_all": False
        },
        "api": {
            "app_rate": True
        },
        "web": {
            "app_download": True,
        },
    })

    id_user = models.OneToOneField(User)

# Path: app/storage.py
# class OverwriteStorage(FileSystemStorage):
#     """
#     Class for implementing overwrite user's avatar when change.
#     """
# 
#     def get_available_name(self, name, max_length=None):
#         """
#         Returns a filename that's free on the target storage system, and
#         available for new content to be written to.
#         """
#         if self.exists(name):
#             os.remove(os.path.join(settings.MEDIA_ROOT, name))
# 
#         return name


# Path: app/models.py
import json
import logging
from collections import namedtuple
from django.db import models
from django.db.models import Avg, Count, Q
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from django.core.validators import MaxValueValidator, MinValueValidator
from django.urls import reverse
from django.utils.html import escape
from .storage import OverwriteStorage



logger = logging.getLogger('changes')


BookRelatedData = namedtuple('BookRelatedData', ['author', 'category', 'lang', 'user'])


# ----------------------------------------------------------------------------------------------------------------------
class TheUser(models.Model):
    """
    Class for user objects in database.
    """
    REMINDER_TEMPLATE = json.dumps({
        "common": {
            "fb_page": True,
            "fb_group": True,
            "twitter": True,
            "vk": True,
            "disabled_all": False
        },
        "api": {
            "app_rate": True
        },
        "web": {
            "app_download": True,
        },
    })

    id_user = models.OneToOneField(User)

    user_photo = models.ImageField(blank=True, upload_to='user', storage=OverwriteStorage())