init
This commit is contained in:
0
web/__init__.py
Normal file
0
web/__init__.py
Normal file
BIN
web/__pycache__/__init__.cpython-37.pyc
Normal file
BIN
web/__pycache__/__init__.cpython-37.pyc
Normal file
Binary file not shown.
BIN
web/__pycache__/apps.cpython-37.pyc
Normal file
BIN
web/__pycache__/apps.cpython-37.pyc
Normal file
Binary file not shown.
BIN
web/__pycache__/models.cpython-37.pyc
Normal file
BIN
web/__pycache__/models.cpython-37.pyc
Normal file
Binary file not shown.
BIN
web/__pycache__/urls.cpython-37.pyc
Normal file
BIN
web/__pycache__/urls.cpython-37.pyc
Normal file
Binary file not shown.
BIN
web/__pycache__/views.cpython-37.pyc
Normal file
BIN
web/__pycache__/views.cpython-37.pyc
Normal file
Binary file not shown.
5
web/apps.py
Normal file
5
web/apps.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class WebConfig(AppConfig):
|
||||
name = 'web'
|
0
web/management/__init__.py
Normal file
0
web/management/__init__.py
Normal file
0
web/management/commands/__init__.py
Normal file
0
web/management/commands/__init__.py
Normal file
72
web/management/commands/datafill.py
Normal file
72
web/management/commands/datafill.py
Normal file
@@ -0,0 +1,72 @@
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import numpy as np
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.management import BaseCommand
|
||||
|
||||
from web.models import Activity, ActivityLog, User
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
def handle(self, *args, **options):
|
||||
User.objects.all().delete()
|
||||
Activity.objects.all().delete()
|
||||
ActivityLog.objects.all().delete()
|
||||
|
||||
activity_sleeping = Activity(type='Сон')
|
||||
activity_sleeping.save()
|
||||
activity_eating = Activity(type='Еда')
|
||||
activity_eating.save()
|
||||
activity_studying = Activity(type='Учёба')
|
||||
activity_studying.save()
|
||||
activity_working = Activity(type='Работа')
|
||||
activity_working.save()
|
||||
activity_logistics = Activity(type='Транспорт')
|
||||
activity_logistics.save()
|
||||
activity_pastime = Activity(type='Досуг')
|
||||
activity_pastime.save()
|
||||
|
||||
user1 = User(name='Вася', email='vasya@abra.me')
|
||||
user1.save()
|
||||
user2 = User(name='Петя', email='petya@abra.me')
|
||||
user2.save()
|
||||
|
||||
start_date = datetime(2018, 12, 1, 0, 0, 0)
|
||||
days = (datetime.utcnow() - start_date) // timedelta(days=1) - 1
|
||||
|
||||
for i in range(days):
|
||||
day_start = start_date + timedelta(days=1) * i
|
||||
|
||||
def add_activity(user, activity, start_offset, end_offset, variance):
|
||||
activity_log = ActivityLog(
|
||||
user=user,
|
||||
activity=activity,
|
||||
start_time=day_start + start_offset + variance * abs(np.random.normal()),
|
||||
end_time=day_start + end_offset - variance * abs(np.random.normal()),
|
||||
)
|
||||
try:
|
||||
activity_log.clean()
|
||||
except ValidationError as e:
|
||||
print(e.message)
|
||||
return
|
||||
|
||||
activity_log.save()
|
||||
print(activity_log)
|
||||
|
||||
add_activity(user1, activity_sleeping, timedelta(hours=-24 + 22), timedelta(hours=8), timedelta(hours=2))
|
||||
add_activity(user1, activity_eating, timedelta(hours=8), timedelta(hours=8.5), timedelta(minutes=5))
|
||||
add_activity(user1, activity_logistics, timedelta(hours=8.5), timedelta(hours=9.5), timedelta(minutes=10))
|
||||
add_activity(user1, activity_working, timedelta(hours=9.5), timedelta(hours=14), timedelta(minutes=30))
|
||||
add_activity(user1, activity_eating, timedelta(hours=14), timedelta(hours=14.5), timedelta(minutes=5))
|
||||
add_activity(user1, activity_working, timedelta(hours=14.5), timedelta(hours=18), timedelta(minutes=30))
|
||||
add_activity(user1, activity_logistics, timedelta(hours=18), timedelta(hours=19), timedelta(minutes=10))
|
||||
add_activity(user1, activity_eating, timedelta(hours=20), timedelta(hours=20.5), timedelta(minutes=5))
|
||||
add_activity(user1, activity_pastime, timedelta(hours=20.5), timedelta(hours=22), timedelta(minutes=10))
|
||||
|
||||
add_activity(user2, activity_sleeping, timedelta(hours=0), timedelta(hours=6), timedelta(hours=1))
|
||||
add_activity(user2, activity_pastime, timedelta(hours=6), timedelta(hours=7), timedelta(minutes=10))
|
||||
add_activity(user2, activity_logistics, timedelta(hours=7), timedelta(hours=8), timedelta(minutes=10))
|
||||
add_activity(user2, activity_studying, timedelta(hours=8), timedelta(hours=18), timedelta(minutes=30))
|
||||
add_activity(user2, activity_logistics, timedelta(hours=18), timedelta(hours=19), timedelta(minutes=10))
|
||||
add_activity(user2, activity_eating, timedelta(hours=19), timedelta(hours=20), timedelta(minutes=10))
|
||||
add_activity(user2, activity_pastime, timedelta(hours=20), timedelta(hours=24), timedelta(minutes=10))
|
45
web/migrations/0001_initial.py
Normal file
45
web/migrations/0001_initial.py
Normal file
@@ -0,0 +1,45 @@
|
||||
# Generated by Django 2.1.4 on 2018-12-26 19:52
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Activity',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('type', models.TextField()),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='ActivityLog',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('start_time', models.DateTimeField()),
|
||||
('end_time', models.DateTimeField()),
|
||||
('logged_at', models.DateTimeField(auto_now_add=True)),
|
||||
('activity', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='web.Activity')),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='User',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.TextField()),
|
||||
('email', models.TextField()),
|
||||
],
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='activitylog',
|
||||
name='user',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='web.User'),
|
||||
),
|
||||
]
|
0
web/migrations/__init__.py
Normal file
0
web/migrations/__init__.py
Normal file
BIN
web/migrations/__pycache__/0001_initial.cpython-37.pyc
Normal file
BIN
web/migrations/__pycache__/0001_initial.cpython-37.pyc
Normal file
Binary file not shown.
BIN
web/migrations/__pycache__/__init__.cpython-37.pyc
Normal file
BIN
web/migrations/__pycache__/__init__.cpython-37.pyc
Normal file
Binary file not shown.
39
web/models.py
Normal file
39
web/models.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import models
|
||||
|
||||
|
||||
class User(models.Model):
|
||||
name = models.TextField()
|
||||
email = models.TextField()
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.name}'
|
||||
|
||||
|
||||
class Activity(models.Model):
|
||||
type = models.TextField()
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.type}'
|
||||
|
||||
|
||||
class ActivityLog(models.Model):
|
||||
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||
activity = models.ForeignKey(Activity, on_delete=models.CASCADE)
|
||||
|
||||
start_time = models.DateTimeField()
|
||||
end_time = models.DateTimeField()
|
||||
logged_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
def clean(self):
|
||||
if self.start_time >= self.end_time:
|
||||
raise ValidationError(f'Illegal times: {self}')
|
||||
|
||||
for other_activity in ActivityLog.objects.filter(user=self.user).exclude(id=self.id):
|
||||
l = max(self.start_time, other_activity.start_time)
|
||||
r = min(self.end_time, other_activity.end_time)
|
||||
if r > l:
|
||||
raise ValidationError(f'Intersects with another activity: \n{self}\n{other_activity}\n{l}, {r}')
|
||||
|
||||
def __str__(self):
|
||||
return f'ActivityLog[{self.user}, {self.activity}] {self.start_time}-{self.end_time} ({self.end_time - self.start_time})'
|
19
web/urls.py
Normal file
19
web/urls.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from django.urls import path
|
||||
|
||||
from web import views
|
||||
|
||||
urlpatterns = [
|
||||
path('',
|
||||
views.UserListView.as_view(), name='users'),
|
||||
path('users/new',
|
||||
views.UserCreateView.as_view(), name='users_new'),
|
||||
path('users/<int:user_id>/',
|
||||
views.UserDetailView.as_view(), name='user'),
|
||||
|
||||
path('users/<int:user_id>/activity_log/new',
|
||||
views.ActivityLogCreateView.as_view(), name='activity_log_create'),
|
||||
path('users/<int:user_id>/activity_log/<int:activity_log_id>/update',
|
||||
views.ActivityLogUpdateView.as_view(), name='activity_log_update'),
|
||||
path('users/<int:user_id>/activity_log/<int:activity_log_id>/delete',
|
||||
views.ActivityLogDeleteView.as_view(), name='activity_log_delete'),
|
||||
]
|
56
web/views.py
Normal file
56
web/views.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from django.urls import reverse_lazy, reverse
|
||||
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
|
||||
|
||||
from web.models import Activity, User, ActivityLog
|
||||
|
||||
|
||||
class UserListView(ListView):
|
||||
model = User
|
||||
|
||||
|
||||
class UserCreateView(CreateView):
|
||||
model = User
|
||||
fields = ['name', 'email']
|
||||
success_url = reverse_lazy('users')
|
||||
|
||||
|
||||
class UserDetailView(DetailView):
|
||||
model = User
|
||||
context_object_name = 'user'
|
||||
pk_url_kwarg = 'user_id'
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['activity_logs'] = ActivityLog.objects.filter(user=self.object).order_by('start_time')
|
||||
return context
|
||||
|
||||
|
||||
class ActivityLogCreateView(CreateView):
|
||||
model = ActivityLog
|
||||
fields = ['activity', 'user', 'start_time', 'end_time']
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse('user', kwargs={
|
||||
'user_id': self.object.user.id,
|
||||
})
|
||||
|
||||
|
||||
class ActivityLogUpdateView(UpdateView):
|
||||
model = ActivityLog
|
||||
fields = ['activity', 'user', 'start_time', 'end_time']
|
||||
pk_url_kwarg = 'activity_log_id'
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse('user', kwargs={
|
||||
'user_id': self.object.user.id,
|
||||
})
|
||||
|
||||
|
||||
class ActivityLogDeleteView(DeleteView):
|
||||
model = ActivityLog
|
||||
pk_url_kwarg = 'activity_log_id'
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse('user', kwargs={
|
||||
'user_id': self.object.user.id,
|
||||
})
|
Reference in New Issue
Block a user