def adicionar(request): serializer = TaskSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_404_BAD_REQUEST)
def post_task(request): data = JSONParser().parse(request) serializer = TaskSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def createTask(request): data = JSONParser().parse(request) taskSerializer = TaskSerializer(data=data) if taskSerializer.is_valid(): taskSerializer.save() return JsonResponse(taskSerializer.data, status=201) return JsonResponse(taskSerializer.errors, status=400)
def get_context_data(self, **kwargs): params = (self.request.query_params if len(self.request.data) == 0 else self.request.data) queryset = self.model.objects.filter(company=self.request.company) accounts = Account.objects.filter(company=self.request.company) contacts = Contact.objects.filter(company=self.request.company) if self.request.user.role != "ADMIN" and not self.request.user.is_superuser: queryset = queryset.filter( Q(assigned_to__in=[self.request.user]) | Q(created_by=self.request.user)) accounts = accounts.filter( Q(created_by=self.request.user) | Q(assigned_to=self.request.user)).distinct() contacts = contacts.filter( Q(created_by=self.request.user) | Q(assigned_to=self.request.user)).distinct() request_post = params if request_post: if request_post.get("title"): queryset = queryset.filter( title__icontains=request_post.get("title")) if request_post.get("status"): queryset = queryset.filter(status=request_post.get("status")) if request_post.get("priority"): queryset = queryset.filter( priority=request_post.get("priority")) context = {} search = False if (params.get("title") or params.get("status") or params.get("priority")): search = True context["search"] = search if search: context["tasks"] = TaskSerializer(queryset.distinct(), many=True).data return context context["tasks"] = TaskSerializer(queryset.distinct(), many=True).data context["status"] = STATUS_CHOICES context["priority"] = PRIORITY_CHOICES users = [] if self.request.user.role == "ADMIN" or self.request.user.is_superuser: users = User.objects.filter( is_active=True, company=self.request.company).order_by("email") elif self.request.user.google.all(): users = [] else: users = User.objects.filter( role="ADMIN", company=self.request.company).order_by("email") context["users"] = UserSerializer(users, many=True).data if self.request.user == "ADMIN": context["teams_list"] = TeamsSerializer( Teams.objects.filter(company=self.request.company), many=True).data context["accounts_list"] = AccountSerializer(accounts, many=True).data context["contacts_list"] = ContactSerializer(contacts, many=True).data return context
def create_task(request): if request.method == 'POST': data = JSONParser().parse(request) serializer = TaskSerializer(data=data) if serializer.is_valid(): serializer.save() return JsonResponse(serializer.data, status=201) return JsonResponse(serializer.errors, status=400)
def create_task(request): request_data = JSONParser().parse(request) task = TaskSerializer(data=request_data) if task.is_valid(): task.save() return JsonResponse(task.data, safe=False, status=201) else: return JsonResponse(task.errors, safe=False, status=400)
def createTask(request): print(request) infoJson = JSONParser().parse(request) print(infoJson) serializer_class = TaskSerializer(data=infoJson) if serializer_class.is_valid(): serializer_class.save() return JsonResponse(serializer_class.data, status=201) return JsonResponse(serializer_class.errors, status=400)
def post_task(request): if request.method == "POST": data = JSONParser().parse(request) serializer = TaskSerializer(data=data) status = 404 if serializer.is_valid(): serializer.save() status = 200 return JsonResponse(serializer.errors, status=status)
def atualizar(request, pk): try: task = Task.objects.get(pk=pk) except Task.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) serializer = TaskSerializer(task, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get_task(_request, title): # pylint: disable=no-member try: data = TaskSerializer(Task.objects.filter(title=title), many=True) return JsonResponse(data.data, safe=False) except Task.DoesNotExist: return Http404("Task not found")
def get_task(request): try: title = JSONParser().parse(request)['title'] data = TaskSerializer(Task.objects.filter(title=title), many=True) return JsonResponse(data.data, safe=False) except Task.DoesNotExist: return HttpResponse("ERRO: Task Not Exist")
def get_context_data(self, **kwargs): params = ( self.request.query_params if len(self.request.data) == 0 else self.request.data ) queryset = self.model.objects.filter( org=self.request.org).order_by('-id') accounts = Account.objects.filter(org=self.request.org) contacts = Contact.objects.filter(org=self.request.org) if self.request.profile.role != "ADMIN" and not self.request.profile.is_admin: queryset = queryset.filter( Q(assigned_to__in=[self.request.profile]) | Q( created_by=self.request.profile) ) accounts = accounts.filter( Q(created_by=self.request.profile) | Q( assigned_to=self.request.profile) ).distinct() contacts = contacts.filter( Q(created_by=self.request.profile) | Q( assigned_to=self.request.profile) ).distinct() if params: if params.get("title"): queryset = queryset.filter( title__icontains=params.get("title")) if params.get("status"): queryset = queryset.filter(status=params.get("status")) if params.get("priority"): queryset = queryset.filter( priority=params.get("priority")) context = {} results_tasks = self.paginate_queryset( queryset.distinct(), self.request, view=self ) tasks = TaskSerializer(results_tasks, many=True).data if results_tasks: offset = queryset.filter(id__gte=results_tasks[-1].id).count() if offset == queryset.count(): offset = None else: offset = 0 context.update( { "tasks_count": self.count, "offset": offset, } ) context["tasks"] = tasks context["status"] = STATUS_CHOICES context["priority"] = PRIORITY_CHOICES context["accounts_list"] = AccountSerializer(accounts, many=True).data context["contacts_list"] = ContactSerializer(contacts, many=True).data return context
def post(self, request, pk, **kwargs): params = (self.request.query_params if len(self.request.data) == 0 else self.request.data) context = {} self.task_obj = Task.objects.get(pk=pk) if self.task_obj.company != request.company: return Response({ "error": True, "errors": "User company does not match with header...." }) if self.request.user.role != "ADMIN" and not self.request.user.is_superuser: if not ((self.request.user == self.task_obj.created_by) or (self.request.user in self.task_obj.assigned_to.all())): return Response( { "error": True, "errors": "You don't have Permission to perform this action", }, status=status.HTTP_401_UNAUTHORIZED, ) comment_serializer = CommentSerializer(data=params) if comment_serializer.is_valid(): if params.get("comment"): comment_serializer.save( task_id=self.task_obj.id, commented_by_id=self.request.user.id, ) if self.request.FILES.get("task_attachment"): attachment = Attachments() attachment.created_by = self.request.user attachment.file_name = self.request.FILES.get( "task_attachment").name attachment.task = self.task_obj attachment.attachment = self.request.FILES.get("task_attachment") attachment.save() comments = Comment.objects.filter( task__id=self.task_obj.id).order_by("-id") attachments = Attachments.objects.filter( task__id=self.task_obj.id).order_by("-id") context.update({ "task_obj": TaskSerializer(self.task_obj).data, "attachments": AttachmentsSerializer(attachments, many=True).data, "comments": CommentSerializer(comments, many=True).data, }) return Response(context)
class ProjectSerializer(serializers.ModelSerializer): tasks = TaskSerializer(many=True, read_only=True) tags = TagSerializer(many=True, read_only=True) class Meta: model = Project fields = ("id", "title", "description", "image_url", "app_link", "status", "tasks", "tags", "blog_link", "frontend_github_link", "backend_github_link", "created", "updated")
def get_context_data(self, **kwargs): context = {} contact_obj = get_object_or_404(Contact, pk=kwargs['pk']) context["contact_obj"] = ContactSerializer(contact_obj).data user_assgn_list = [ assigned_to.id for assigned_to in contact_obj.assigned_to.all() ] user_assigned_accounts = set( self.request.user.account_assigned_users.values_list("id", flat=True)) contact_accounts = set( contact_obj.account_contacts.values_list("id", flat=True)) if user_assigned_accounts.intersection(contact_accounts): user_assgn_list.append(self.request.user.id) if self.request.user == contact_obj.created_by: user_assgn_list.append(self.request.user.id) if self.request.user.role != "ADMIN" and not self.request.user.is_superuser: if self.request.user.id not in user_assgn_list: raise PermissionDenied assigned_data = [] for each in contact_obj.assigned_to.all(): assigned_dict = {} assigned_dict["id"] = each.id assigned_dict["name"] = each.email assigned_data.append(assigned_dict) if self.request.user.is_superuser or self.request.user.role == "ADMIN": users_mention = list( User.objects.filter( is_active=True, company=self.request.company).values("username")) elif self.request.user != contact_obj.created_by: users_mention = [{"username": contact_obj.created_by.username}] else: users_mention = list( contact_obj.assigned_to.all().values("username")) context.update({ "comments": CommentSerializer(contact_obj.contact_comments.all(), many=True).data, "attachments": AttachmentsSerializer(contact_obj.contact_attachment.all(), many=True).data, "assigned_data": assigned_data, "tasks": TaskSerializer(contact_obj.contacts_tasks.all(), many=True).data, "users_mention": users_mention, }) return context
class UserSerializer(serializers.ModelSerializer): team = TeamListSerializer() tasks = TaskSerializer(many=True, read_only=True) class Meta: model = User fields = ('id', 'team', "username", 'last_name', 'email', 'password', 'tasks' ) read_only_fields = ('id',) extra_kwargs = { 'password': {'required': True, 'allow_blank': False, 'min_length': 6}, 'tasks': {'required': False}, }
def get(self, request, pk, format=None): context = {} context["account_obj"] = AccountSerializer(self.account).data if self.request.user.role != "ADMIN" and not self.request.user.is_superuser: if not ((self.request.user == self.account.created_by) or (self.request.user in self.account.assigned_to.all())): raise PermissionDenied comment_permission = (True if (self.request.user == self.account.created_by or self.request.user.is_superuser or self.request.user.role == "ADMIN") else False) if self.request.user.is_superuser or self.request.user.role == "ADMIN": users_mention = list( User.objects.filter( is_active=True, company=self.request.company, ).values("username")) elif self.request.user != self.account.created_by: if self.account.created_by: users_mention = [{ "username": self.account.created_by.username }] else: users_mention = [] else: users_mention = [] context.update({ "comments": CommentSerializer(self.account.accounts_comments.all(), many=True).data, "attachments": AttachmentsSerializer(self.account.account_attachment.all(), many=True).data, "opportunity_list": OpportunitySerializer( Opportunity.objects.filter(account=self.account), many=True).data, "contacts": ContactSerializer(self.account.contacts.all(), many=True).data, "users": UserSerializer( User.objects.filter( is_active=True, company=self.request.company, ).order_by("email"), many=True, ).data, "cases": CaseSerializer(Case.objects.filter(account=self.account), many=True).data, "stages": STAGES, "sources": SOURCES, "countries": COUNTRIES, "currencies": CURRENCY_CODES, "case_types": CASE_TYPE, "case_priority": PRIORITY_CHOICE, "case_status": STATUS_CHOICE, "comment_permission": comment_permission, "tasks": TaskSerializer(self.account.accounts_tasks.all(), many=True).data, "invoices": InvoiceSerailizer(self.account.accounts_invoices.all(), many=True).data, "emails": EmailSerailizer(self.account.sent_email.all(), many=True).data, "users_mention": users_mention, }) return Response(context)
def get_all_tasks(_request): # pylint: disable=no-member data = TaskSerializer(Task.objects.all(), many=True) return JsonResponse(data.data, safe=False)
def get_context_data(self, **kwargs): context = {} user_assgn_list = [ assigned_to.id for assigned_to in self.task_obj.assigned_to.all() ] if self.request.user == self.task_obj.created_by: user_assgn_list.append(self.request.user.id) if self.request.user.role != "ADMIN" and not self.request.user.is_superuser: if self.request.user.id not in user_assgn_list: return Response( { "error": True, "errors": "You don't have Permission to perform this action", }, status=status.HTTP_403_FORBIDDEN, ) comments = Comment.objects.filter(task=self.task_obj).order_by("-id") attachments = Attachments.objects.filter( task=self.task_obj).order_by("-id") assigned_data = self.task_obj.assigned_to.values("id", "email") if self.request.user.is_superuser or self.request.user.role == "ADMIN": users_mention = list( User.objects.filter(is_active=True, ).values("username")) elif self.request.user != self.task_obj.created_by: users_mention = [{"username": self.task_obj.created_by.username}] else: users_mention = list( self.task_obj.assigned_to.all().values("username")) if self.request.user.role == "ADMIN" or self.request.user.is_superuser: users = User.objects.filter(is_active=True, ).order_by("email") else: users = User.objects.filter(role="ADMIN", ).order_by("email") if self.request.user == self.task_obj.created_by: user_assgn_list.append(self.request.user.id) if self.request.user.role != "ADMIN" and not self.request.user.is_superuser: if self.request.user.id not in user_assgn_list: return Response( { "error": True, "errors": "You don't have Permission to perform this action", }, status=status.HTTP_403_FORBIDDEN, ) team_ids = [user.id for user in self.task_obj.get_team_users] all_user_ids = users.values_list("id", flat=True) users_excluding_team_id = set(all_user_ids) - set(team_ids) users_excluding_team = User.objects.filter( id__in=users_excluding_team_id) context.update({ "task_obj": TaskSerializer(self.task_obj).data, "attachments": AttachmentsSerializer(attachments, many=True).data, "comments": CommentSerializer(comments, many=True).data, "users_mention": users_mention, "assigned_data": assigned_data, }) context["users"] = UserSerializer(users, many=True).data context["users_excluding_team"] = UserSerializer(users_excluding_team, many=True).data context["teams"] = TeamsSerializer(Teams.objects.all(), many=True).data return context
def get_task_by_id(request, task_id): if request.method == 'GET': task = Task.objects.get(pk=task_id) serializer = TaskSerializer(task) return JsonResponse(serializer.data, status=201, safe=False)
def get(self, request, pk, format=None): context = {} contact_obj = self.get_object(pk) context["contact_obj"] = ContactSerializer(contact_obj).data user_assgn_list = [ assigned_to.id for assigned_to in contact_obj.assigned_to.all() ] user_assigned_accounts = set( self.request.profile.account_assigned_users.values_list("id", flat=True)) contact_accounts = set( contact_obj.account_contacts.values_list("id", flat=True)) if user_assigned_accounts.intersection(contact_accounts): user_assgn_list.append(self.request.profile.id) if self.request.profile == contact_obj.created_by: user_assgn_list.append(self.request.profile.id) if self.request.profile.role != "ADMIN" and not self.request.profile.is_admin: if self.request.profile.id not in user_assgn_list: return Response( { "error": True, "errors": "You do not have Permission to perform this action", }, status=status.HTTP_403_FORBIDDEN, ) assigned_data = [] for each in contact_obj.assigned_to.all(): assigned_dict = {} assigned_dict["id"] = each.id assigned_dict["name"] = each.email assigned_data.append(assigned_dict) if self.request.profile.is_admin or self.request.profile.role == "ADMIN": users_mention = list( Profile.objects.filter( is_active=True, org=request.org).values("user__username")) elif self.request.profile != contact_obj.created_by: users_mention = [{ "username": contact_obj.created_by.user.username }] else: users_mention = list( contact_obj.assigned_to.all().values("username")) if request.profile == contact_obj.created_by: user_assgn_list.append(self.request.profile.id) context["address_obj"] = BillingAddressSerializer( contact_obj.address).data context["countries"] = COUNTRIES context.update({ "comments": CommentSerializer(contact_obj.contact_comments.all(), many=True).data, "attachments": AttachmentsSerializer(contact_obj.contact_attachment.all(), many=True).data, "assigned_data": assigned_data, "tasks": TaskSerializer(contact_obj.contacts_tasks.all(), many=True).data, "users_mention": users_mention, }) return Response(context)
def get_all(request): data = TaskSerializer(Task.objects.all(), many=True) return JsonResponse(data.data, safe=False)
def get_tasks(request): if request.method == 'GET': serializer = TaskSerializer(Task.objects.all(), many=True) return JsonResponse(serializer.data, safe=False)
def get_all_tasks(request): if request.method == 'GET': tasks = Task.objects.all() serializer = TaskSerializer(tasks, many=True) return JsonResponse(serializer.data, status=201, safe=False)
def get(self, request, pk, format=None): context = {} contact_obj = self.get_object(pk) context["contact_obj"] = ContactSerializer(contact_obj).data user_assgn_list = [ assigned_to.id for assigned_to in contact_obj.assigned_to.all() ] user_assigned_accounts = set( self.request.user.account_assigned_users.values_list( "id", flat=True) ) contact_accounts = set( contact_obj.account_contacts.values_list("id", flat=True) ) if user_assigned_accounts.intersection(contact_accounts): user_assgn_list.append(self.request.user.id) if self.request.user == contact_obj.created_by: user_assgn_list.append(self.request.user.id) if self.request.user.role != "ADMIN" and not self.request.user.is_superuser: if self.request.user.id not in user_assgn_list: raise PermissionDenied assigned_data = [] for each in contact_obj.assigned_to.all(): assigned_dict = {} assigned_dict["id"] = each.id assigned_dict["name"] = each.email assigned_data.append(assigned_dict) if self.request.user.is_superuser or self.request.user.role == "ADMIN": users_mention = list( User.objects.filter( is_active=True, company=self.request.company ).values("username") ) elif self.request.user != contact_obj.created_by: users_mention = [ {"username": contact_obj.created_by.username}] else: users_mention = list( contact_obj.assigned_to.all().values("username")) if self.request.user.role == "ADMIN" or self.request.user.is_superuser: self.users = User.objects.filter( is_active=True, company=self.request.company).order_by("email") else: self.users = User.objects.filter( role="ADMIN", company=self.request.company).order_by("email") if request.user == contact_obj.created_by: user_assgn_list.append(self.request.user.id) context["address_obj"] = BillingAddressSerializer( contact_obj.address).data context["users"] = UserSerializer(self.users, many=True).data context["countries"] = COUNTRIES context["teams"] = TeamsSerializer( Teams.objects.filter(company=request.company), many=True).data context.update( { "comments": CommentSerializer( contact_obj.contact_comments.all(), many=True).data, "attachments": AttachmentsSerializer( contact_obj.contact_attachment.all(), many=True).data, "assigned_data": assigned_data, "tasks": TaskSerializer( contact_obj.contacts_tasks.all(), many=True).data, "users_mention": users_mention, } ) return Response(context)
def get_tasks(request): tasks = Task.objects.all() serializer = TaskSerializer(tasks, many=True) return JsonResponse(serializer.data, safe=False)
def get(self, request, pk, format=None): self.account = self.get_object(pk=pk) if self.account.org != request.org: return Response( { "error": True, "errors": "User company doesnot match with header...." }, status=status.HTTP_404_NOT_FOUND, ) context = {} context["account_obj"] = AccountSerializer(self.account).data if self.request.profile.role != "ADMIN" and not self.request.profile.is_admin: if not ((self.request.profile == self.account.created_by) or (self.request.profile in self.account.assigned_to.all())): return Response( { "error": True, "errors": "You do not have Permission to perform this action", }, status=status.HTTP_403_FORBIDDEN, ) comment_permission = False if (self.request.profile == self.account.created_by or self.request.profile.is_admin or self.request.profile.role == "ADMIN"): comment_permission = True if self.request.profile.is_admin or self.request.profile.role == "ADMIN": users_mention = list( Profile.objects.filter( is_active=True, org=self.request.org).values("user__username")) elif self.request.profile != self.account.created_by: if self.account.created_by: users_mention = [{ "username": self.account.created_by.user.username }] else: users_mention = [] else: users_mention = [] context.update({ "attachments": AttachmentsSerializer(self.account.account_attachment.all(), many=True).data, "comments": CommentSerializer(self.account.accounts_comments.all(), many=True).data, "contacts": ContactSerializer(self.account.contacts.all(), many=True).data, "opportunity_list": OpportunitySerializer( Opportunity.objects.filter(account=self.account), many=True).data, "users": ProfileSerializer( Profile.objects.filter( is_active=True, org=self.request.org).order_by("user__email"), many=True, ).data, "cases": CaseSerializer(self.account.accounts_cases.all(), many=True).data, "stages": STAGES, "sources": SOURCES, "countries": COUNTRIES, "currencies": CURRENCY_CODES, "case_types": CASE_TYPE, "case_priority": PRIORITY_CHOICE, "case_status": STATUS_CHOICE, "comment_permission": comment_permission, "tasks": TaskSerializer(self.account.accounts_tasks.all(), many=True).data, "invoices": InvoiceSerailizer(self.account.accounts_invoices.all(), many=True).data, "emails": EmailSerializer(self.account.sent_email.all(), many=True).data, "users_mention": users_mention, }) return Response(context)
def get_context_data(self, **kwargs): params = (self.request.query_params if len(self.request.data) == 0 else self.request.data) queryset = self.model.objects.all() accounts = Account.objects.all() contacts = Contact.objects.all() if self.request.user.role != "ADMIN" and not self.request.user.is_superuser: queryset = queryset.filter( Q(assigned_to__in=[self.request.user]) | Q(created_by=self.request.user)) accounts = accounts.filter( Q(created_by=self.request.user) | Q(assigned_to=self.request.user)).distinct() contacts = contacts.filter( Q(created_by=self.request.user) | Q(assigned_to=self.request.user)).distinct() request_post = params if request_post: if request_post.get("title"): queryset = queryset.filter( title__icontains=request_post.get("title")) if request_post.get("status"): queryset = queryset.filter(status=request_post.get("status")) if request_post.get("priority"): queryset = queryset.filter( priority=request_post.get("priority")) context = {} search = False if params.get("title") or params.get("status") or params.get( "priority"): search = True context["search"] = search results_tasks = self.paginate_queryset(queryset.distinct(), self.request, view=self) tasks = TaskSerializer(results_tasks, many=True).data context["per_page"] = 10 context.update({ "tasks_count": self.count, "next": self.get_next_link(), "previous": self.get_previous_link(), "page_number": int(self.offset / 10) + 1, }) context["tasks"] = tasks context["status"] = STATUS_CHOICES context["priority"] = PRIORITY_CHOICES users = [] if self.request.user.role == "ADMIN" or self.request.user.is_superuser: users = User.objects.filter(is_active=True, ).order_by("email") elif self.request.user.google.all(): users = [] else: users = User.objects.filter(role="ADMIN", ).order_by("email") context["users"] = UserSerializer(users, many=True).data if self.request.user == "ADMIN": context["teams_list"] = TeamsSerializer(Teams.objects.all(), many=True).data context["accounts_list"] = AccountSerializer(accounts, many=True).data context["contacts_list"] = ContactSerializer(contacts, many=True).data return context
class UserSerializer(serializers.HyperlinkedModelSerializer): tasks = TaskSerializer(many=True, read_only=True) class Meta: model = User fields = ['id', 'username', 'tasks']