def create(self, validated_data): area_id = self.context["request"].data["area_account_count"] validated_data['category'] = CategorySerializer().create(validated_data.pop('category')) has_accounts = 'accounts' in validated_data if has_accounts: accounts_data = validated_data.pop('accounts') from users.common import user_by_token user = user_by_token(self.context.get("request")) validated_data['creator'] = user with transaction.atomic(): scheduler_data = validated_data.pop('scheduler') validated_data['scheduler'] = Scheduler.objects.create(**scheduler_data) # SchedulerSerializer().create(scheduler_data) # (validated_data.pop('scheduler')) account_count = int(validated_data['accounts_num']) instance = super(TaskSerializer, self).create(validated_data) if area_id == -1: accounts = Account.objects.filter(Q(owner_id=user.id) | Q(owner__category__name=u'管理员'), Q(status='valid') | Q(status__icontains='verify')).order_by('using')[:account_count] else: accounts = Account.objects.filter(Q(owner_id=user.id) | Q(owner__category__name=u'管理员'), Q(status='valid') | Q(status__icontains='verify'), active_area_id=area_id).order_by('using')[:account_count] for account in accounts: TaskAccountRelationship.objects.create(account_id=account.id, task_id=instance.id) self.update_timestamp(instance) return instance
def logout(request): from users.common import user_by_token user = user_by_token(request) if user: user.token = '' user.save() return {}, 200 return { 'detail': 'You do not have permission to perform this action.' }, 403
def get_queryset(self): from users.common import user_by_token, is_admin user = user_by_token(self.request) queryset = self.queryset if user.enable_tasks == '' or is_admin( user) else TaskCategory.objects.filter( pk__in=user.enable_tasks.split(';')) from django.db.models import Q queryset = search( self.request, queryset, lambda qs, keyword: qs.filter( Q(name__icontains=keyword) | Q(processor=keyword))) return queryset
def create(self, validated_data): # print("#########", validated_data) with transaction.atomic(): owner = user_by_token(self.context.get("request")) validated_data['owner'] = owner try: return Account.objects.get( category_id=validated_data["category"], owner=owner.id, account=validated_data['account']) except ObjectDoesNotExist: return super(AccountSerializer, self).create(validated_data)
def get_queryset(request): from users.common import user_by_token, is_admin user = user_by_token(request) if request.method == 'GET' and 'all' in request.query_params: from django.db.models import Q queryset = Account.objects.filter( Q(owner_id=user.id) | Q(owner__category__name=u'管理员')) elif is_admin(user): queryset = Account.objects.all() else: queryset = Account.objects.filter(owner_id=user.id) from django.db.models import Q queryset = search( request, queryset, lambda qs, keyword: qs.filter( Q(account__icontains=keyword) | Q(email=keyword) | Q( name__icontains=keyword))) return queryset
def get_queryset(self): def try_str_to_int(s, default=0): try: return int(s) except ValueError: return default from django.db.models import Q from users.common import user_by_token, is_admin user = user_by_token(self.request) queryset = Task.objects.filter(~Q(status='cancelled')) if not is_admin(user): queryset = queryset.filter(creator_id=user.id) if 'status' in self.request.query_params: queryset = queryset.filter( status=self.request.query_params['status']) queryset = search( self.request, queryset, lambda qs, keyword: qs.filter( Q(pk=try_str_to_int(keyword)) | Q( creator__auth__last_name__icontains=keyword) | Q( name__icontains=keyword))) return queryset
def has_permission(self, request, view): from users.common import user_by_token, is_admin user = user_by_token(request) return user and is_admin(user)
def has_permission(self, request, view): from users.common import user_by_token return user_by_token(request)