Reworked DB structure
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
from django.contrib import admin
|
||||
from .models import CalendarData
|
||||
from .models import YearData
|
||||
from .models import MonthData
|
||||
from .models import DayData
|
||||
from .models import Event
|
||||
from .models import CalendarData
|
||||
|
||||
admin.site.register(CalendarData)
|
||||
admin.site.register(YearData)
|
||||
admin.site.register(MonthData)
|
||||
admin.site.register(DayData)
|
||||
admin.site.register(Event)
|
||||
admin.site.register(CalendarData)
|
||||
|
||||
+55
-52
@@ -1,79 +1,82 @@
|
||||
from django.db import models
|
||||
from django.core.validators import RegexValidator
|
||||
from django.core.validators import MinValueValidator
|
||||
from django.core.validators import MaxValueValidator
|
||||
|
||||
|
||||
class FaerunDate:
|
||||
LAST_MONTH = 18
|
||||
LEAP_MONTH = 11
|
||||
ONE_DAY_MONTHS = (2, 6, 10, 11, 14, 17)
|
||||
LAST_DAY = 30
|
||||
class YearData(models.Model):
|
||||
number = models.SmallIntegerField('Number', validators=[
|
||||
RegexValidator(r'^-?[1-9]\d{0,3}$', 'Year must be not zero and between -9999 and 9999')
|
||||
])
|
||||
|
||||
def __init__(self, year: int, month: int, day: int):
|
||||
self.year = year
|
||||
self.month = month
|
||||
self.day = day
|
||||
self.is_leap_year = self._is_leap_year()
|
||||
self.is_valid = self._is_valid()
|
||||
|
||||
def __repr__(self):
|
||||
return f'{type(self).__name__}({self.year}, {self.month}, {self.day})'
|
||||
|
||||
def _is_leap_year(self) -> bool:
|
||||
if self.year == 0:
|
||||
@property
|
||||
def is_leap(self):
|
||||
if self.number == 0:
|
||||
return False
|
||||
elif self.year % 400 == 0:
|
||||
elif self.number % 400 == 0:
|
||||
return True
|
||||
elif self.year % 100 == 0:
|
||||
elif self.number % 100 == 0:
|
||||
return False
|
||||
elif self.year % 4 == 0:
|
||||
elif self.number % 4 == 0:
|
||||
return True
|
||||
return False
|
||||
|
||||
def _is_valid(self) -> bool:
|
||||
if self.year == 0:
|
||||
return False
|
||||
|
||||
if not 0 < self.month <= FaerunDate.LAST_MONTH:
|
||||
return False
|
||||
|
||||
if not self.is_leap_year and self.month == self.__class__.LEAP_MONTH:
|
||||
return False
|
||||
|
||||
if not 0 < self.day <= FaerunDate.LAST_DAY:
|
||||
return False
|
||||
|
||||
if self.day > 1 and self.month in FaerunDate.ONE_DAY_MONTHS:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
class CalendarData(models.Model):
|
||||
current_year = models.SmallIntegerField('CurrentYear')
|
||||
current_month = models.SmallIntegerField('CurrentMonth')
|
||||
current_day = models.SmallIntegerField('CurrentDay')
|
||||
leap_month = models.SmallIntegerField('LeapMonth')
|
||||
|
||||
def __str__(self):
|
||||
return f'({self.current_year}, {self.current_month}, {self.current_day}, {self.leap_month})'
|
||||
return f'({self.number}, {self.is_leap})'
|
||||
|
||||
|
||||
class MonthData(models.Model):
|
||||
number = models.SmallIntegerField('Number', unique=True)
|
||||
number = models.SmallIntegerField('Number', unique=True, validators=[
|
||||
MinValueValidator(1),
|
||||
MaxValueValidator(18),
|
||||
])
|
||||
name = models.CharField('Name', max_length=64, unique=True)
|
||||
folkname = models.CharField('Folkname', max_length=64, blank=True)
|
||||
is_oneday = models.BooleanField('IsOneday')
|
||||
is_leap_month = models.BooleanField('IsLeapMonth')
|
||||
|
||||
def __str__(self):
|
||||
return f'({self.number}, {self.name}, {self.folkname}, {self.is_oneday})'
|
||||
return f'({self.number}, {self.name}, {self.folkname}, {self.is_oneday}, {self.is_leap_month})'
|
||||
|
||||
|
||||
class DayData(models.Model):
|
||||
number = models.SmallIntegerField('Number', unique=True, validators=[
|
||||
MinValueValidator(1),
|
||||
MaxValueValidator(30),
|
||||
])
|
||||
month = models.ForeignKey(MonthData, on_delete=models.CASCADE)
|
||||
year = models.ForeignKey(YearData, on_delete=models.CASCADE)
|
||||
|
||||
def __str__(self):
|
||||
return f'({self.number, self.month})'
|
||||
|
||||
|
||||
class CalendarData(models.Model):
|
||||
current_day = models.ForeignKey(DayData, on_delete=models.CASCADE)
|
||||
|
||||
def __str__(self):
|
||||
return f'({self.current_day})'
|
||||
|
||||
@property
|
||||
def current_month(self):
|
||||
try:
|
||||
return getattr(self.current_day, 'month')
|
||||
except AttributeError:
|
||||
return None
|
||||
|
||||
@property
|
||||
def current_year(self):
|
||||
try:
|
||||
return getattr(self.current_day, 'year')
|
||||
except AttributeError:
|
||||
return None
|
||||
|
||||
|
||||
class Event(models.Model):
|
||||
year = models.SmallIntegerField('Year')
|
||||
month = models.SmallIntegerField('Month')
|
||||
day = models.SmallIntegerField('Day')
|
||||
day = models.ForeignKey(DayData, on_delete=models.CASCADE)
|
||||
time = models.TimeField('Time')
|
||||
title = models.CharField('Title', max_length=250)
|
||||
description = models.TextField('Description', max_length=2500)
|
||||
|
||||
def __str__(self):
|
||||
return f'({self.year}, {self.month}, {self.day}, {self.time}, {self.title})'
|
||||
return f'({self.day}, {self.time}, {self.title})'
|
||||
|
||||
@@ -5,6 +5,10 @@ div.calendarpage {
|
||||
border-radius: 20px;
|
||||
}
|
||||
|
||||
h2.year {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
table.month, table.month th, table.month td {
|
||||
border: 1px solid white;
|
||||
border-collapse: collapse;
|
||||
|
||||
@@ -13,14 +13,14 @@
|
||||
{% block content %}
|
||||
<div class="calendarpage">
|
||||
<h1 class="title">Календарь</h1>
|
||||
<h2 class="year">{{year}}</h2>
|
||||
|
||||
<h2 class="year">{{year_data.number}}</h2>
|
||||
<div class="calendar">
|
||||
{% for month in month_data %}
|
||||
{% if month.number != calendar_data.leap_month or is_leap_year %}
|
||||
{% include 'faerun_calendar/month.html' with month=month month_days=month_days %}
|
||||
{% if not month.is_leap_month or year_data.is_leap %}
|
||||
{% include 'faerun_calendar/month.html' with month=month calendar_data=calendar_data month_days=month_days %}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
<tr>
|
||||
{% endif %}
|
||||
|
||||
{% if year == calendar_data.current_year and month.number == calendar_data.current_month and day == calendar_data.current_day %}
|
||||
{% if day == calendar_data.current_day.number and year_data == calendar_data.current_year and month == calendar_data.current_month %}
|
||||
<td class="current">
|
||||
{% else %}
|
||||
<td>
|
||||
|
||||
@@ -4,5 +4,6 @@ from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('', views.index, name='index'),
|
||||
path('<int:year>', views.year_page, name='index'),
|
||||
]
|
||||
|
||||
|
||||
+25
-17
@@ -1,31 +1,39 @@
|
||||
from django.shortcuts import render
|
||||
from .models import CalendarData
|
||||
from django.http import HttpResponse
|
||||
from django.http import HttpResponseNotFound
|
||||
|
||||
from .models import YearData
|
||||
from .models import MonthData
|
||||
from .models import DayData
|
||||
from .models import CalendarData
|
||||
from .models import Event
|
||||
|
||||
|
||||
def index(request):
|
||||
def is_leap_year(year) -> bool:
|
||||
if year == 0:
|
||||
return False
|
||||
elif year % 400 == 0:
|
||||
return True
|
||||
elif year % 100 == 0:
|
||||
return False
|
||||
elif year % 4 == 0:
|
||||
return True
|
||||
return False
|
||||
def year_page(request, year: int):
|
||||
try:
|
||||
year_data = YearData.objects.get(number=year)
|
||||
except YearData.DoesNotExist:
|
||||
return HttpResponseNotFound("<h1>404 Not Found</h1>")
|
||||
|
||||
calendar_data = CalendarData.objects.first()
|
||||
month_data = MonthData.objects.all()
|
||||
|
||||
year = 1492
|
||||
calendar_data = CalendarData.objects.first()
|
||||
|
||||
params = {
|
||||
'calendar_data': calendar_data,
|
||||
'year_data': year_data,
|
||||
'month_data': month_data,
|
||||
'month_days': tuple(i+1 for i in range(30)),
|
||||
'year': year,
|
||||
'is_leap_year': is_leap_year(year),
|
||||
}
|
||||
|
||||
return render(request, 'faerun_calendar/index.html', params)
|
||||
|
||||
|
||||
def index(request):
|
||||
calendar_data = CalendarData.objects.first()
|
||||
|
||||
try:
|
||||
current_year = getattr(getattr(CalendarData.objects.first(), 'current_year'), 'number')
|
||||
except AttributeError:
|
||||
current_year = 0
|
||||
|
||||
return year_page(request, current_year)
|
||||
|
||||
Reference in New Issue
Block a user