endpoints

This commit is contained in:
Egor Matveev 2021-12-02 18:17:30 +03:00
parent 07a04af473
commit 255a364c34
17 changed files with 26 additions and 18 deletions

View File

@ -7,6 +7,7 @@ from SprintLib.utils import delete_file, write_bytes
class AccountView(BaseView):
view_file = "account.html"
required_login = True
endpoint = "account"
def get(self):
if "username" in self.request.GET.keys():

View File

@ -4,3 +4,4 @@ from SprintLib.BaseView import BaseView
class EnterView(BaseView):
view_file = "enter.html"
required_login = False
endpoint = "enter"

View File

@ -5,6 +5,7 @@ from SprintLib.BaseView import BaseView
class ExitView(BaseView):
required_login = True
endpoint = "exit"
def get(self):
logout(self.request)

View File

@ -5,5 +5,6 @@ from SprintLib.utils import get_bytes
class ImageView(BaseView):
endpoint = "image"
def get(self):
return HttpResponse(get_bytes(int(self.request.GET['id'])), content_type="image/jpg")

View File

@ -4,3 +4,4 @@ from SprintLib.BaseView import BaseView
class MainView(BaseView):
view_file = "main.html"
required_login = True
endpoint = ""

View File

@ -6,6 +6,7 @@ from SprintLib.BaseView import BaseView
class RatingView(BaseView):
view_file = "rating.html"
required_login = True
endpoint = "rating"
def get(self):
self.context["users"] = User.objects.filter(userinfo__verified=True).order_by(

View File

@ -7,6 +7,7 @@ from random import randrange
class SendCodeView(BaseView):
endpoint = "send_code"
def post_create(self):
username = self.request.POST["username"]
user = User.objects.filter(username=username).first()

View File

@ -4,3 +4,4 @@ from SprintLib.BaseView import BaseView
class SetSettingsView(BaseView):
required_login = True
view_file = 'set_settings.html'
endpoint = "admin/set"

View File

@ -5,6 +5,7 @@ from SprintLib.BaseView import BaseView
class SetsView(BaseView):
view_file = "sets.html"
required_login = True
endpoint = "sets"
def post(self):
set_name = self.request.POST["name"]

View File

@ -4,6 +4,7 @@ from SprintLib.BaseView import BaseView, AccessError
class SolutionView(BaseView):
view_file = 'solution.html'
required_login = True
endpoint = "solution"
def pre_handle(self):
if self.request.user.is_superuser:

View File

@ -8,6 +8,7 @@ from SprintLib.BaseView import BaseView
class SolutionsTableView(BaseView):
view_file = 'solutions_table.html'
required_login = True
endpoint = "solutions_table"
def get(self):
self.context['solutions'] = Solution.objects.filter(

View File

@ -7,6 +7,7 @@ from SprintLib.BaseView import BaseView
class TaskRuntimeView(BaseView):
view_file = 'task_runtime.html'
required_login = True
endpoint = "task_runtime"
def get(self):
progress = Progress.objects.get(task=self.entities.task, user=self.request.user)

View File

@ -8,6 +8,7 @@ from SprintLib.BaseView import BaseView, AccessError
class TaskSettingsView(BaseView):
view_file = "task_settings.html"
required_login = True
endpoint = "admin/task"
def pre_handle(self):
if self.entities.task not in self.request.user.userinfo.available_tasks:

View File

@ -10,6 +10,7 @@ from SprintLib.utils import write_bytes
class TaskView(BaseView):
required_login = True
view_file = "task.html"
endpoint = "task"
def get(self):
progress, _ = Progress.objects.get_or_create(

View File

@ -5,6 +5,7 @@ from SprintLib.BaseView import BaseView
class TasksView(BaseView):
view_file = "tasks.html"
required_login = True
endpoint = "tasks"
def post(self):
task_name = self.request.POST["name"]

View File

@ -1,23 +1,15 @@
from django.contrib import admin
from django.urls import path
import Main.views
from Main.views import *
urlpatterns = [
path("enter", EnterView.as_view()),
path("rating", RatingView.as_view()),
path("tasks", TasksView.as_view()),
path("account", AccountView.as_view()),
path("exit", ExitView.as_view()),
path("admin/task", TaskSettingsView.as_view()),
path("admin/set", SetSettingsView.as_view()),
path("sets", SetsView.as_view()),
path("task", TaskView.as_view()),
path("solution", SolutionView.as_view()),
path("solutions_table", SolutionsTableView.as_view()),
path("task_runtime", TaskRuntimeView.as_view()),
path("image", ImageView.as_view()),
path("send_code", SendCodeView.as_view()),
path("", MainView.as_view()),
path("admin/", admin.site.urls),
]
urlpatterns = [path("admin/", admin.site.urls)]
for v in dir(Main.views):
try:
view = eval(v)
except NameError:
continue
if hasattr(view, 'endpoint') and view.endpoint is not None:
urlpatterns.append(path(view.endpoint, view.as_view()))

View File

@ -19,6 +19,7 @@ class BaseView:
entities = EntityStorage()
required_login: Optional[bool] = None
view_file: Optional[str] = None
endpoint: Optional[str] = None
@classmethod
def as_view(cls):