81 lines
2.4 KiB
Python
81 lines
2.4 KiB
Python
from django import forms
|
|
from django.contrib.auth import views as auth_views, password_validation
|
|
from django.contrib.auth.forms import UsernameField
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
from django.contrib import messages
|
|
from django.core.exceptions import PermissionDenied
|
|
from django.http import HttpResponseForbidden, HttpResponseRedirect
|
|
from django.views.generic import TemplateView
|
|
from django.views.generic.detail import DetailView
|
|
from django.views.generic.edit import CreateView, UpdateView
|
|
from django.urls import reverse, reverse_lazy
|
|
from django.utils.translation import gettext_lazy as _
|
|
from trading.models import User, Commodity, MaxCommodityError
|
|
|
|
|
|
class IndexView(LoginRequiredMixin, TemplateView):
|
|
template_name = "trading/index.html"
|
|
|
|
|
|
class CommodityDetailView(DetailView):
|
|
template_name = "trading/c/detail.html"
|
|
model = Commodity
|
|
|
|
|
|
class CommodityCreateView(LoginRequiredMixin, CreateView):
|
|
template_name = "trading/c/create.html"
|
|
model = Commodity
|
|
fields = (
|
|
"name",
|
|
"in_circulation",
|
|
)
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context["object"] = self.request.user
|
|
return context
|
|
|
|
def form_valid(self, form):
|
|
user = self.request.user
|
|
if not user.can_create_commodity():
|
|
raise MaxCommodityError(user)
|
|
form.instance.created_by = user
|
|
return super(CreateView, self).form_valid(form)
|
|
|
|
|
|
class UserProfileView(DetailView):
|
|
template_name = "trading/u/profile.html"
|
|
model = User
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
if self.object.is_active:
|
|
status = "Active"
|
|
else:
|
|
status = "Inactive"
|
|
context["object_status"] = status
|
|
return context
|
|
|
|
|
|
class UserUpdateSettingsForm(forms.ModelForm):
|
|
class Meta:
|
|
model = User
|
|
fields = (
|
|
"username",
|
|
"email",
|
|
)
|
|
field_classes = {"username": UsernameField}
|
|
|
|
def __init__(self, user, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
class UserSettingsView(LoginRequiredMixin, UpdateView):
|
|
template_name = "trading/account/settings.html"
|
|
model = User
|
|
fields = ("username", "email")
|
|
success_url = reverse_lazy("trading:settings")
|
|
|
|
def get_object(self, queryset=None):
|
|
return self.request.user
|