Beispiel #1
0
 def update(self, request, *args, **kwargs):
     user = request.user
     queryset = self.get_queryset()[0]
     serializer = CompanySerializer(queryset, data=request.data, partial=True)
     if serializer.is_valid():
         serializer.save(account_owner=user)
         return Response(serializer.data, status=status.HTTP_202_ACCEPTED)
     else:
         return Response({
             'status': "400",
             'message': serializer.errors
         }, status=status.HTTP_400_BAD_REQUEST)
Beispiel #2
0
 def retrieve(self, request, pk=None):
     try:
         company = Company.objects.get(ASIN=pk)
     except Company.DoesNotExist:
         company = get_object_or_404(self.queryset, slug=pk)
     serializer = CompanySerializer(company)
     return Response(serializer.data)
class SupportSerializer(ModelSerializer):
    user = UserSerializer()
    company = CompanySerializer()

    class Meta:
        model = Support
        fields = '__all__'

    def update(self, instance, validated_data):
        with transaction.atomic():
            instance.save()

            # save user
            user = validated_data.get('user')
            if user != None:
                if user.get('email') != None:
                    instance.user.email = user.get('email')
                if user.get('username') != None:
                    instance.user.username = user.get('username')
                if user.get('first_name') != None:
                    instance.user.first_name = user.get('first_name')
                if user.get('last_name') != None:
                    instance.user.last_name = user.get('last_name')
            instance.user.save()

        return instance
Beispiel #4
0
 def test_can_read_company_list(self):
     response = self.client.get(reverse('company-list'), {"limit": 4})
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     json = response.json()
     companies = Company.objects.all()
     serializer = CompanySerializer(companies, many=True)
     self.assertEqual(json.get('results'), serializer.data)
Beispiel #5
0
	def to_representation(self, instance):
		
		representation = super(JobMatchSerializer, self).to_representation(instance)
		representation['company'] = CompanySerializer(instance.company).data
		representation['volunteer'] = VolunteerSerializer(instance.volunteer).data
		representation['job_areas'] = VolunteeringAreaSerializer(instance.job_areas).data
		return representation
Beispiel #6
0
    def test_retrieve(self):

        url = '/api/v1/company/{}/'.format(1)
        response = self.client.get(url, content_type="application/json")
        company = Company.objects.get(id=response.json()['company']['id'])
        company_serializer = CompanySerializer(company)
        self.assertEqual(company_serializer.data, response.json()['company'])
        self.assertEqual(response.status_code, status.HTTP_200_OK)
Beispiel #7
0
    def test_list(self):

        url = '/api/v1/company/'
        response = self.client.get(url, content_type="application/json")
        queryset = Company.objects.all()
        company_serializer = CompanySerializer(queryset, many=True)
        self.assertEqual(company_serializer.data, response.json()['companies'])
        self.assertEqual(response.status_code, status.HTTP_200_OK)
Beispiel #8
0
 def test_company_search_returns_correct_company(self):
     Company.objects.create(name="facebook", slug="facebook")
     response = self.client.get(reverse('company-list'), {"limit": 4, "q": "facebook"})
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     json = response.json()
     company = Company.objects.filter(name='facebook')
     serializer = CompanySerializer(company, many=True)
     self.assertEqual(json.get('results'), serializer.data)
    def test_valid_company_update(self):
        response = self.client.put(self.company_detail_url,
                                   self.valid_data,
                                   format='json')
        self.company = Company.objects.get(pk=self.company.account.id)
        serializer = CompanySerializer(self.company)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data, dict(serializer.data))
Beispiel #10
0
class ReviewSerializer(serializers.ModelSerializer):
    """ Serializer for model Review """

    reviewer = ReviewerSerializer()
    company = CompanySerializer()

    class Meta:
        model = Review
        fields = '__all__'
    def test_valid_company_creation(self):
        response = self.client.post(self.company_list_url,
                                    self.valid_company,
                                    format='json')
        companies = Company.objects.all()
        serializer = CompanySerializer(companies, many=True)

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.data, dict(*serializer.data))
Beispiel #12
0
class UserSerializer(serializers.ModelSerializer):
    company = CompanySerializer(read_only=True)
    m2m_workshops = WorkshopSerializer(read_only=True, many=True)

    class Meta:
        model = User
        fields = [
            'id', 'email', 'username', 'first_name', 'last_name', 'avatar',
            'date_joined', 'isAdmin', 'company', 'is_staff', 'address',
            'zip_code', 'city', 'country', 'available_credit', 'm2m_workshops'
        ]
Beispiel #13
0
class ReviewSerializer(serializers.ModelSerializer):
    company = CompanySerializer(
        read_only=True
    )

    job = JobSerializer(
        read_only=True
    )

    class Meta:
        model = Review
        fields = '__all__'
Beispiel #14
0
    def test_create(self):

        data = json.dumps({
            "name": "Company 1",
            "email": "*****@*****.**",
            "address": "Some address"
        })
        response = self.client.post('/api/v1/company/',
                                    data=data,
                                    content_type="application/json")
        company = Company.objects.get(id=response.json()['company']['id'])
        company_serializer = CompanySerializer(company)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
Beispiel #15
0
    def test_update(self):

        data = json.dumps({
            "name": "Company 1",
            "email": "*****@*****.**",
            "address": "Some address"
        })
        url = '/api/v1/company/{}/'.format(1)
        response = self.client.put(url,
                                   data=data,
                                   content_type="application/json")
        company = Company.objects.get(id=response.json()['company']['id'])
        company_serializer = CompanySerializer(company)
        self.assertEqual(company_serializer.data, response.json()['company'])
        self.assertEqual(response.status_code, status.HTTP_200_OK)
Beispiel #16
0
class ReviewSerializer(serializers.ModelSerializer):

    reviewer = ReviewerSerializer(read_only=True)
    company = CompanySerializer(read_only=True)
    createdAt = serializers.SerializerMethodField(method_name='get_created_at')
    updatedAt = serializers.SerializerMethodField(method_name='get_updated_at')

    class Meta:

        model = Review
        fields = ('id', 'reviewer', 'rating', 'title', 'summary', 'ip_address',
                  'company', 'createdAt', 'updatedAt')

    def create(self, validated_data):
        """
		Creates a new Review instance 

		"""
        reviewer = self.context.get('reviewer', None)
        company = self.context.get('company', None)
        ip_address = self.context.get('ip_address', None)
        rating = Review.objects.create(reviewer=reviewer,
                                       company=company,
                                       ip_address=ip_address,
                                       **validated_data)
        return rating

    def get_created_at(self, instance):
        """
		Give iso format to the update date
		"""
        return instance.created_at.isoformat()

    def get_updated_at(self, instance):
        """
		Give iso format to the cration date
		"""
        return instance.updated_at.isoformat()

    def validate(self, data):
        """
	    Checks that rating is between 1 and 5
	    """
        if data['rating'] > 5 or data['rating'] < 1:

            raise serializers.ValidationError("Not valid rating")
        return data
Beispiel #17
0
def company_list(request): 
    if request.method == 'GET': 
        company = Company.objects.all() 
        c_serializer = CompanySerializer(company, many=True) 
        return JSONResponse(c_serializer.data) 
 
    elif request.method == 'POST': 
        c_data = JSONParser().parse(request) 
        c_serializer = CompanySerializer(data=c_data, many=True) 
        if c_serializer.is_valid(): 
            c_serializer.save() 
            return JSONResponse(c_serializer.data,
            status=status.HTTP_201_CREATED) 
        return JSONResponse(c_serializer.errors,
        status=status.HTTP_400_BAD_REQUEST) 
Beispiel #18
0
class PersonSerializer(serializers.ModelSerializer):

    tags = TagSerializer(read_only=True, many=True)
    # friends = PersonSerializer(read_only=True, many=True)
    company = CompanySerializer(read_only=True, many=False)
    fruits = serializers.SerializerMethodField("favouriteFruits")
    vegetables = serializers.SerializerMethodField("favouriteVegetables")

    class Meta:
        model = Person
        fields = (
            "created_at",
            "modified_at",
            "_id",
            "index",
            "guid",
            "has_died",
            "balance",
            "picture",
            "age",
            "eyeColor",
            "name",
            "gender",
            "company",
            "email",
            "phone",
            "address",
            "about",
            "registered",
            "tags",
            "friends",
            "greeting",
            "fruits",
            "vegetables",
        )

    def favouriteFruits(self, obj):
        return [o.name for o in obj.favouriteFruits.all()]

    def favouriteVegetables(self, obj):
        return [o.name for o in obj.favouriteVegetables.all()]
Beispiel #19
0
def company_detail(request, pk): 
    try: 
        company = Company.objects.get(pk=pk) 
    except Company.DoesNotExist: 
        return HttpResponse(status=status.HTTP_404_NOT_FOUND) 
 
    if request.method == 'GET': 
        c_serializer = CompanySerializer(company) 
        return JSONResponse(c_serializer.data) 
 
    elif request.method == 'PUT': 
        c_data = JSONParser().parse(request) 
        c_serializer = CompanySerializer(company, data=c_data) 
        if c_serializer.is_valid(): 
            c_serializer.save() 
            return JSONResponse(c_serializer.data) 
        return JSONResponse(c_serializer.errors,
        status=status.HTTP_400_BAD_REQUEST) 
 
    elif request.method == 'DELETE': 
        company.delete() 
        return HttpResponse(status=status.HTTP_204_NO_CONTENT)
Beispiel #20
0
 def test_can_read_company_detail(self):
     response = self.client.get(reverse('company-detail', kwargs={"slug": self.test_company.slug}))
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     company = Company.objects.get(slug=self.test_company.slug)
     serializer = CompanySerializer(company)
     self.assertEqual(serializer.data, response.data)
Beispiel #21
0
class TodoSerializer(serializers.ModelSerializer):
    company = CompanySerializer(read_only=True)

    class Meta:
        model = Todo
        fields = '__all__'
    def test_company_read(self):
        response = self.client.get(self.company_detail_url)
        serializer = CompanySerializer(self.company)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data, dict(serializer.data))
Beispiel #23
0
    def test_create(self):
        """
		Tests review of first company 
		"""
        url = reverse("reviews:create",
                      kwargs={"company_id": self.company1.id})
        data = json.dumps({
            "review": {
                "rating": 3,
                "title": "New rating",
                "summary": "Some summary"
            }
        })
        reviewer = Reviewer.objects.get(username='******')
        response = self.client.post(url,
                                    data=data,
                                    HTTP_AUTHORIZATION='Token {}'.format(
                                        self.reviewer1.token),
                                    content_type='application/json')
        review = Review.objects.get(id=response.json()['review']['id'])
        review_serializer = ReviewSerializer(review)
        company_serializer = CompanySerializer(self.company1)
        reviewer_serializer = ReviewerSerializer(self.reviewer1)

        self.assertEqual(reviewer_serializer.data,
                         response.json()['review']['reviewer'])
        self.assertEqual(review_serializer.data, response.json()['review'])
        self.assertEqual(company_serializer.data,
                         response.json()['review']['company'])
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        """
		Tests review of second company 
		"""

        url = reverse("reviews:create",
                      kwargs={"company_id": self.company2.id})
        data = json.dumps({
            "review": {
                "rating": 3,
                "title": "New rating",
                "summary": "Some summary"
            }
        })
        reviewer = Reviewer.objects.get(username='******')
        response = self.client.post(url,
                                    data=data,
                                    HTTP_AUTHORIZATION='Token {}'.format(
                                        self.reviewer1.token),
                                    content_type='application/json')
        review = Review.objects.get(id=response.json()['review']['id'])
        review_serializer = ReviewSerializer(review)
        company_serializer = CompanySerializer(self.company2)
        reviewer_serializer = ReviewerSerializer(self.reviewer1)

        self.assertEqual(reviewer_serializer.data,
                         response.json()['review']['reviewer'])
        self.assertEqual(review_serializer.data, response.json()['review'])
        self.assertEqual(company_serializer.data,
                         response.json()['review']['company'])
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        """
		Tests review of third company and second reviewer
		"""

        url = reverse("reviews:create",
                      kwargs={"company_id": self.company3.id})
        data = json.dumps({
            "review": {
                "rating": 3,
                "title": "New rating",
                "summary": "Some summary"
            }
        })
        reviewer = Reviewer.objects.get(username='******')
        response = self.client.post(url,
                                    data=data,
                                    HTTP_AUTHORIZATION='Token {}'.format(
                                        self.reviewer2.token),
                                    content_type='application/json')
        review = Review.objects.get(id=response.json()['review']['id'])
        review_serializer = ReviewSerializer(review)
        company_serializer = CompanySerializer(self.company3)
        reviewer_serializer = ReviewerSerializer(self.reviewer2)

        self.assertEqual(reviewer_serializer.data,
                         response.json()['review']['reviewer'])
        self.assertEqual(review_serializer.data, response.json()['review'])
        self.assertEqual(company_serializer.data,
                         response.json()['review']['company'])
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        """
		Tests review of not found company
		"""
        # with self.assertRaisesMessage(Company.NotFound, 'Company matching query does not exist.'):
        url = reverse("reviews:create", kwargs={"company_id": 100})
        data = json.dumps({
            "review": {
                "rating": 3,
                "title": "New rating",
                "summary": "Some summary"
            }
        })
        reviewer = Reviewer.objects.get(username=self.reviewer2.username)
        response = self.client.post(url,
                                    data=data,
                                    HTTP_AUTHORIZATION='Token {}'.format(
                                        self.reviewer2.token),
                                    content_type='application/json')

        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)