Esempio n. 1
0
    def update(self, request, id=None):

        ready_data_true =  {'success': True }
        ready_data_false = {'success': False}

        if not request.user.is_authenticated():
            ready_data_false["error"] = "Authorization is failed"
            return ready_data_false

        ext_posted_data = simplejson.loads(request.POST.get('items'))
        attrs = self.flatten_dict(ext_posted_data)
    
        try:
            del attrs['id']
        except KeyError:
            pass
    
        reason=[]
        if id:
            req = check(request, id, attrs=attrs)
            if req == True:
                user = Profile.objects.filter(pk=id).update(**attrs)
                user = Profile.objects.get(pk=id)
                ready_data_true['items'] = user
                ready_data_true['total'] = 1
                return ready_data_true
            else:
                ready_data_false['errors'] = req
                return ready_data_false
        else:
            reason.append(u'User ID is required')
            ready_data_false['errors'] = reason
            return ready_data_false
Esempio n. 2
0
    def read(self, request, id=None):
        
        role = getProfile(request).get_role_display()

        ready_data_true =  {'success': True }
        ready_data_false = {'success': False}

        if not request.user.is_authenticated():
            ready_data_false["error"] = "Authorization is failed"
            return ready_data_false

        if '/newapi/customer' in request.path:
            if id:
                req = customer_check(request, id, attrs=None)
                if req == True:
                    ready_data_true['items'] = Profile.objects.get(pk=id)
                    ready_data_true['total'] = 1
                    return ready_data_true
                else:
                    ready_data_false['errors'] = req
                    return ready_data_false
            else:

                if role == 'Vendor' or role == 'Vendor salesmen':
                    customers = []
                    
                    relationships = Relationship.objects.filter(vendor=getProfile(request))

                    for relationship in relationships:
                        customers.append(relationship.customer.parent_user.id)

                    if relationships.count() == 0:
                        ready_data_false['errors'] = 'No Customers'
                        return ready_data_false
                        
                    customers_keywords = Q()
                    for customer in customers:
                        customers_keywords = customers_keywords | Q(user=customer)

                    ready_data_true['items'] = Profile.objects.filter(customers_keywords)
                    ready_data_true['total'] = len(ready_data_true['items'])
                    return ready_data_true
            
            
        else:
            if id:
                req = check(request, id, attrs=None)
                if req == True:
                    ready_data_true['items'] = Profile.objects.get(pk=id)
                    ready_data_true['total'] = 1
                    return ready_data_true
                else:
                    ready_data_false['errors'] = req
                    return ready_data_false
            else:
                ready_data_true['items'] = Profile.objects.filter(parent_user=getParentuser(request))
                ready_data_true['total'] = len(ready_data_true['items'])
                return ready_data_true         
Esempio n. 3
0
    def create(self, request):
        ready_data_true =  {'success': True }
        ready_data_false = {'success': False}

        if not request.user.is_authenticated():
            ready_data_false["error"] = "Authorization is failed"
            return ready_data_false

        role = getProfile(request).get_role_display()

        ext_posted_data = simplejson.loads(request.POST.get('items'))
        attrs = self.flatten_dict(ext_posted_data)
        
        try:
            del attrs['id']
        except KeyError:
            pass
        
        
        for x in attrs:
            if attrs[x] == '':
                attrs[x] = None
            else:
                attrs[x] = attrs[x]
        
        req = check(request, id=None, attrs=attrs)
        if req == True:
            if role == 'Admin':
                #Creating user in auth_user table
                user = User.objects.create_user(username=attrs['username'], password=attrs['password'], email=attrs['email'])
                user.save
                user_id = user.id
                if attrs['parent_user_id'] == 'self':
                    parent_user_id = user_id
                else: parent_user_id = int(attrs['parent_user_id'])
            
                #Creating profile for new user
                profile = Profile.objects.create(description=attrs['description'],
                    email=attrs['email'], 
                    role=int(attrs['role']), 
                    user_id=user_id,
                    parent_user_id=parent_user_id,
                    company_address=attrs['company_address'],
                    company_name=attrs['company_name'],
                    first_name=attrs['first_name'],
                    last_name=attrs['last_name'])
                ready_data_true['items'] = profile
                ready_data_true['total'] = 1
                return ready_data_true
                
            else:
                ready_data_false['errors'] = u'Permission denied'
                return ready_data_false
        else:
            ready_data_false['errors'] = req
            return ready_data_false
Esempio n. 4
0
    def delete(self, request, id=None):
        
        ready_data_true =  {'success': True }
        ready_data_false = {'success': False}

        if not request.user.is_authenticated():
            ready_data_false["error"] = "Authorization is failed"
            return ready_data_false
            
        reason = []
        if id:
            req = check(request, id, attrs=None)
            if req == True:
                user = Profile.objects.get(pk=id)
                user.delete()
                return ready_data_true
            else:
                ready_data_false['errors'] = req
                return ready_data_false
        else:
            reason.append(u'User ID is required')
            ready_data_false['errors'] = reason
            return ready_data_false