class AddMonitor(CreateView): @method_decorator(login_required()) def post(self, request, user, *args, **kwargs): if not verify_captcha(request): messages.add_message(request, messages.ERROR, 'Captcha nie została uzupełniona poprawnie.') return redirect('/panel/') user_monitors = MonitorObject.objects.filter(user=user).count() if user_monitors >= MAX_USER_MONITORS: messages.add_message(request, messages.ERROR, 'Osiągnąłeś już maksymalną ilość monitorów.') return redirect('/panel/') form = AddMonitorForm(request.POST) if form.is_valid(): monitor_url = form.cleaned_data['url'] check_monitor = MonitorObject.objects.filter( user=user, url=monitor_url).exists() if check_monitor: messages.add_message(request, messages.ERROR, 'Dodałeś już taki URL.') return redirect('/panel/') form.save(user) messages.add_message(request, messages.SUCCESS, message='Dodano poprawnie.') return redirect('/panel/') else: invalid_form(request, form)
class ServersViewSet(viewsets.ViewSet): """ A simple ViewSet for listing user servers or retrieve server by id. """ @method_decorator(login_required()) def list(self, request, token, *args, **kwargs): user = User.objects.get(token=token) queryset = Server.objects.filter(shop__owner=user) serializer = ServerSerializer(queryset, many=True) return Response(serializer.data) @method_decorator(login_required()) def retrieve(self, request, token, *args, **kwargs): pk = kwargs['id'] queryset = Server.objects.get(id=pk) serializer = ServerSerializer(queryset) return Response(serializer.data)
class DeleteMonitor(DeleteView): model = MonitorObject @method_decorator(login_required()) def post(self, request, user, *args, **kwargs): monitor_id = request.POST.get('monitor_id') object = self.model.objects.filter(id=monitor_id, user=user) object.delete() messages.add_message(request, messages.SUCCESS, message='Monitor został usunięty.') return redirect('/panel/')
class DeleteAlert(DeleteView): model = Alert @method_decorator(login_required()) def post(self, request, user, *args, **kwargs): alert_id = request.POST.get('alert_id') alert = self.model.objects.filter(id=alert_id, user=user) alert.delete() messages.add_message(request, messages.SUCCESS, message='Powiadomienie zostało usunięte.') return redirect('/panel/')
class PanelView(View): @method_decorator(login_required()) def get(self, request, user, *args, **kwargs): monitors = MonitorObject.objects.filter(user=user) alerts = Alert.objects.filter(user=user) logs = Log.objects.filter( monitor_object__user_id=user.id).order_by('-id')[:10] context = { 'addMonitorForm': AddMonitorForm(), 'addAlertForm': AddAlertForm(), 'monitors': monitors, 'alerts': alerts, 'logs': logs } return render(request, 'panel/panel.html', context=context)
class CreateShop(CreateAPIView): """ Create new shop. """ @method_decorator(login_required()) def post(self, request, token, *args, **kwargs): name = request.data.get('name') if not name: return JsonResponse({'message': 'Missing name parameter.'}, status=411) shop = Shop( name=name, owner=User.objects.get(token=token) ) shop.save() return JsonResponse({'message': 'Shop has been created.'}, status=201)
class AddAlert(CreateView): @method_decorator(login_required()) def post(self, request, user, *args, **kwargs): form = AddAlertForm(request.POST) if form.is_valid(): alerts = Alert.objects.filter(type=form.cleaned_data['type'], user_id=user.id).exists() if alerts: messages.add_message(request, messages.ERROR, 'Taki typ powiadomienia już istnieje.') return redirect('/panel/') form.save(user) messages.add_message(request, messages.SUCCESS, message='Dodano poprawnie.') return redirect('/panel/') else: invalid_form(request, form) return redirect('/panel/')