class ProductsView(ListView):
    model = Product
    template_name = 'accounts/products.html'
    context_object_name = 'products'

    @method_decorator(login_required(login_url='login'))
    @method_decorator(allowed_users(allowed_roles=['admin']))
    def dispatch(self, *args, **kwargs):
        return super().dispatch(*args, **kwargs)
class UserPageView(TemplateView):
    template_name = 'accounts/user.html'

    @method_decorator(login_required(login_url='login'))
    @method_decorator(allowed_users(allowed_roles=['customer']))
    def dispatch(self, *args, **kwargs):
        return super().dispatch(*args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(UserPageView, self).get_context_data(**kwargs)
        orders = self.request.user.customer.orders.all()
        context['orders'] = orders
        context['total_orders'] = orders.count()
        context['delivered'] = orders.filter(status='Delivered').count()
        context['pending'] = orders.filter(status='Pending').count()
        return context
class OrderCreateView(CreateView):
    model = Order
    fields = ['product', 'status', 'note']
    template_name = 'accounts/order_form.html'

    @method_decorator(login_required(login_url='login'))
    @method_decorator(allowed_users(allowed_roles=['admin']))
    def dispatch(self, *args, **kwargs):
        return super().dispatch(*args, **kwargs)

    def form_valid(self, form):  # for_set_not_NULL_value
        form.instance.customer = self.request.user.customer
        return super().form_valid(form)

    def get_success_url(self):
        return reverse_lazy('customers', kwargs={'pk': self.object.customer.pk})
class CustomerView(DetailView):
    model = Customer
    template_name = 'accounts/customers.html'
    context_object_name = 'customer'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        customer = Customer.objects.get(id=self.kwargs.get('pk'))
        myFilter = OrderFilter(self.request.GET, queryset=customer.orders.all())
        orders = myFilter.qs
        context['myFilter'] = myFilter
        context['orders'] = orders
        return context

    @method_decorator(login_required(login_url='login'))
    @method_decorator(allowed_users(allowed_roles=['admin']))
    def dispatch(self, *args, **kwargs):
        return super().dispatch(*args, **kwargs)
class AccountSettingView(TemplateView):
    template_name = 'accounts/account_settings.html'
    form_class = CustomerForm

    @method_decorator(login_required(login_url='login'))
    @method_decorator(allowed_users(allowed_roles=['customer']))
    def dispatch(self, *args, **kwargs):
        self.instance = self.request.user.customer
        return super().dispatch(*args, **kwargs)

    def get(self, request, *args, **kwargs):
        form = self.form_class(instance=self.instance)
        return render(request, self.template_name, {'form': form})

    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST, request.FILES, instance=self.instance)
        if form.is_valid():
            form.save()
        return render(request, self.template_name, {'form': form})
class OrderUpdateView(UserPassesTestMixin, UpdateView):
    model = Order
    fields = ['product', 'status', 'note']
    template_name = 'accounts/order_form.html'

    @method_decorator(login_required(login_url='login'))
    @method_decorator(allowed_users(allowed_roles=['admin']))
    def dispatch(self, *args, **kwargs):
        return super().dispatch(*args, **kwargs)

    def test_func(self):
        order = self.get_object()
        if order.customer.id == self.request.user.id:
            return True
        else:
            return False

    def get_success_url(self):
        return reverse_lazy('customers', kwargs={'pk': self.object.customer.pk})
示例#7
0
#             restaurant.user = request.user
#             if 'photo' in request.FILES:
#                 restaurant.photo=request.FILES['photo']
#             restaurant.save()
#         else:    #in case if there is error in validation of input data
#             print(restaurant_form.errors)
#         return redirect("/restaurant")
#     else:      #in case if the method is not post
#         restaurant_form=RestaurantForm()
#
#     context_dic={"restaurant_form":restaurant_form}
#     return render(request,"main/add_restaurant.html",context_dic)


@method_decorator(login_required(login_url="/login/"), name='dispatch')
@method_decorator(allowed_users(allowed_roles=['restaurant']), name='dispatch')
class RestaurantListView(ListView):
    context_object_name = "restaurants"

    def get_queryset(self):
        return Restaurant.objects.filter(user=self.request.user)


@method_decorator(login_required(login_url="/login/"), name='dispatch')
@method_decorator(allowed_users(allowed_roles=['restaurant']), name='dispatch')
class RestaurantCreateView(CreateView):
    fields = ('name', 'description', 'photo', 'address', 'city', 'email',
              'contact_number')
    model = Restaurant

    def form_valid(self, form):