Added data models and loading some information from DB

This commit is contained in:
2022-07-13 16:44:04 +03:00
parent ad87a47768
commit 890d50f230
9 changed files with 186 additions and 72 deletions
+6 -1
View File
@@ -1,3 +1,8 @@
from django.contrib import admin
from .models import CalendarData
from .models import MonthData
from .models import Event
# Register your models here.
admin.site.register(CalendarData)
admin.site.register(MonthData)
admin.site.register(Event)
+77 -1
View File
@@ -1,3 +1,79 @@
from django.db import models
# Create your models here.
class FaerunDate:
LAST_MONTH = 18
LEAP_MONTH = 11
ONE_DAY_MONTHS = (2, 6, 10, 11, 14, 17)
LAST_DAY = 30
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:
return False
elif self.year % 400 == 0:
return True
elif self.year % 100 == 0:
return False
elif self.year % 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})'
class MonthData(models.Model):
number = models.SmallIntegerField('Number', unique=True)
name = models.CharField('Name', max_length=64, unique=True)
folkname = models.CharField('Folkname', max_length=64, blank=True)
is_oneday = models.BooleanField('IsOneday')
def __str__(self):
return f'({self.number}, {self.name}, {self.folkname}, {self.is_oneday})'
class Event(models.Model):
year = models.SmallIntegerField('Year')
month = models.SmallIntegerField('Month')
day = models.SmallIntegerField('Day')
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})'
@@ -12,6 +12,10 @@ table.month, table.month th, table.month td {
background: gainsboro;
}
table.month th {
text-align: center;
}
table.month td {
text-align: right;
}
@@ -1,22 +0,0 @@
<div class="calendar">
{% include 'faerun_calendar/month.html' with title="Хаммер" subtitle="Глубокозимье" %}
{% include 'faerun_calendar/month.html' with title="Зимний солнцеворот" subtitle="Мертвозимье" oneday=1 %}
{% include 'faerun_calendar/month.html' with title="Альтурик" subtitle="Коготь зимы" %}
{% include 'faerun_calendar/month.html' with title="Чез" subtitle="Коготь закатов" %}
{% include 'faerun_calendar/month.html' with title="Тарсак" subtitle="Коготь бурь" %}
{% include 'faerun_calendar/month.html' with title="Зеленотравье" oneday=1 %}
{% include 'faerun_calendar/month.html' with title="Миртул" subtitle="Таяние" %}
{% include 'faerun_calendar/month.html' with title="Кайторн" subtitle="Время цветов" %}
{% include 'faerun_calendar/month.html' with title="Флеймрул" subtitle="Разгар лета" %}
{% include 'faerun_calendar/month.html' with title="Летний солнцеворот" oneday=1 %}
{% include 'faerun_calendar/month.html' with title="Шильдмит*" oneday=1 %}
{% include 'faerun_calendar/month.html' with title="Элесис" subtitle="Солнцестояние" %}
{% include 'faerun_calendar/month.html' with title="Элейнт" subtitle="Увядание" %}
{% include 'faerun_calendar/month.html' with title="Праздник урожая" oneday=1 %}
{% include 'faerun_calendar/month.html' with title="Марпенот" subtitle="Листопад" %}
{% include 'faerun_calendar/month.html' with title="Уктар" subtitle="Перегной" %}
{% include 'faerun_calendar/month.html' with title="Пир луны" oneday=1 %}
{% include 'faerun_calendar/month.html' with title="Найтал" subtitle="Спячка" %}
</div>
@@ -13,7 +13,14 @@
{% block content %}
<div class="calendarpage">
<h1 class="title">Календарь</h1>
{% include 'faerun_calendar/calendar.html' %}
<h2 class="year">{{year}}</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 %}
{% endif %}
{% endfor %}
</div>
</div>
{% endblock %}
@@ -1,53 +1,32 @@
<div class="month">
<table class="month">
<tr>
<th colspan="10" class="title">{{title}}</th>
<th colspan="10" class="title">{{month.name}}</th>
</tr>
{% if subtitle %}
<th colspan="10" class="subtitle">{{subtitle}}</th>
{% if month.folkname %}
<th colspan="10" class="subtitle">{{month.folkname}}</th>
{% endif %}
{% if oneday != 1 %}
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>10</td>
</tr>
<tr>
<td>11</td>
<td>12</td>
<td>13</td>
<td>14</td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
{% if title == "Миртул" %}
<td class="current">19</td>
{% else %}
<td>19</td>
{% endif %}
<td>20</td>
</tr>
<tr>
<td>21</td>
<td>22</td>
<td>23</td>
<td>24</td>
<td>25</td>
<td>26</td>
<td>27</td>
<td>28</td>
<td>29</td>
<td>30</td>
</tr>
{% if not month.is_oneday %}
{% for day in month_days %}
{% if forloop.counter0|divisibleby:10 %}
<tr>
{% endif %}
{% if year == calendar_data.current_year and month.number == calendar_data.current_month and day == calendar_data.current_day %}
<td class="current">
{% else %}
<td>
{% endif %}
<div>{{day}}</div>
</td>
{% if forloop.counter|divisibleby:10 %}
</tr>
{% endif %}
{% endfor %}
{% endif %}
</table>
</div>
+41 -1
View File
@@ -1,3 +1,43 @@
from django.test import TestCase
from .models import FaerunDate
class FaerunDateTestCase(TestCase):
class TestDate:
def __init__(self, date: FaerunDate, repr: str, is_leap_year: bool, is_valid: bool):
self.date = date
self.repr = repr
self.is_leap_year = is_leap_year
self.is_valid = is_valid
def setUp(self):
self.dates = (
FaerunDateTestCase.TestDate(FaerunDate(0, 0, 0), 'FaerunDate(0, 0, 0)', False, False),
FaerunDateTestCase.TestDate(FaerunDate(1, -1, 1), 'FaerunDate(1, -1, 1)', False, False),
FaerunDateTestCase.TestDate(FaerunDate(1, 1, 1), 'FaerunDate(1, 1, 1)', False, True),
FaerunDateTestCase.TestDate(FaerunDate(4, 1, 1), 'FaerunDate(4, 1, 1)', True, True),
FaerunDateTestCase.TestDate(FaerunDate(4, -1, 1), 'FaerunDate(4, -1, 1)', True, False),
FaerunDateTestCase.TestDate(FaerunDate(4, 0, 1), 'FaerunDate(4, 0, 1)', True, False),
FaerunDateTestCase.TestDate(FaerunDate(4, 19, 1), 'FaerunDate(4, 19, 1)', True, False),
FaerunDateTestCase.TestDate(FaerunDate(4, 1, -1), 'FaerunDate(4, 1, -1)', True, False),
FaerunDateTestCase.TestDate(FaerunDate(4, 1, 0), 'FaerunDate(4, 1, 0)', True, False),
FaerunDateTestCase.TestDate(FaerunDate(4, 1, 31), 'FaerunDate(4, 1, 31)', True, False),
FaerunDateTestCase.TestDate(FaerunDate(4, 2, 2), 'FaerunDate(4, 2, 2)', True, False),
FaerunDateTestCase.TestDate(FaerunDate(4, 1, 2), 'FaerunDate(4, 1, 2)', True, True),
FaerunDateTestCase.TestDate(FaerunDate(1, 11, 1), 'FaerunDate(1, 11, 1)', False, False),
FaerunDateTestCase.TestDate(FaerunDate(4, 11, 1), 'FaerunDate(4, 11, 1)', True, True),
)
def test_repr(self):
for date in self.dates:
self.assertEqual(date.date.__repr__(), date.repr)
def test_leap(self):
for date in self.dates:
self.assertEqual(date.date.is_leap_year, date.is_leap_year, date.date.__repr__())
def test_valid(self):
for date in self.dates:
self.assertEqual(date.date.is_valid, date.is_valid, date.date.__repr__())
# Create your tests here.
+26 -1
View File
@@ -1,6 +1,31 @@
from django.shortcuts import render
from .models import CalendarData
from .models import MonthData
def index(request):
return render(request, 'faerun_calendar/index.html')
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
calendar_data = CalendarData.objects.first()
month_data = MonthData.objects.all()
year = 1492
params = {
'calendar_data': calendar_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)