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