Skip to content

Commit

Permalink
changed ID for company
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcomu committed Jul 21, 2016
1 parent 90b524f commit 2083b1e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
20 changes: 20 additions & 0 deletions contacts/migrations/0029_auto_20160721_1354.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.7 on 2016-07-21 13:54
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('contacts', '0028_auto_20160721_1317'),
]

operations = [
migrations.AlterField(
model_name='company',
name='company_custom_id',
field=models.IntegerField(null=True, verbose_name='Custom ID'),
),
]
2 changes: 1 addition & 1 deletion contacts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Meta:
@python_2_unicode_compatible
class Company(models.Model):

company_custom_id = models.IntegerField('Custom ID', null=False, unique=True)
company_custom_id = models.IntegerField('Custom ID', null=True)
company_name = models.CharField('Company Name', max_length=200, null=False)
company_short_name = models.CharField('Company Short Name', max_length=200, null=False)
company_business_name = models.CharField('Company Business Name', max_length=200, null=False)
Expand Down
9 changes: 3 additions & 6 deletions contacts/serializers_contact.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from rest_framework import serializers
from .models import Contact, ContactType, Company, CCRelation
from .serializers_company import CompanyTypeSerializer


class ContactTypeSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -34,16 +33,14 @@ def __init__(self, *args, **kwargs):
self.fields.pop(field_name)

def create(self, validated_data):
print validated_data
contact_role_data = validated_data.pop('role')
contact = Contact.objects.create(**validated_data)
for item in contact_role_data['relations']:
print item
try:
company = Company.objects.get(company_custom_id=item['company']['company_custom_id'])
contact_type = ContactType.objects.get(type_name=item['role'])
relationship = CCRelation.objects.create(company=company, contact_type=contact_type, contact=contact)
except Exception as e:
except:
contact.delete()
raise serializers.ValidationError({'company_type': ["Invalid role"]})

Expand All @@ -70,9 +67,9 @@ def update(self, instance, validated_data):
company = Company.objects.get(company_custom_id=item['company']['company_custom_id'])
contact_type = ContactType.objects.get(type_name=item['role'])
CCRelation.objects.create(company=company, contact_type=contact_type, contact=instance)
except Exception as e:
except:
raise serializers.ValidationError({'Validation Error': ["Invalid company and/or invalid contact_type"]})
except Exception as e:
except:
raise serializers.ValidationError({'Validation Error': ["Invalid company and/or invalid contact_type"]})
instance.save()
return instance
Expand Down
1 change: 0 additions & 1 deletion contacts/view_contact.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.http import HttpResponse
from .models import Contact
from .serializers_contact import ContactSerializer
from .helper import auth_decorator
Expand Down
4 changes: 2 additions & 2 deletions contacts/views_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def index(request):
return HttpResponse("Control is an illusion")


@api_view(['GET','POST'])
@api_view(['GET', 'POST'])
@auth_decorator
def all_companies(request,format=None, *args, **kwargs):
"""
Expand All @@ -37,7 +37,7 @@ def single_company(request, id, format=None):
Retrieve, update or delete a snippet instance.
"""
try:
company = Company.objects.get(company_custom_id=id)
company = Company.objects.get(id=id)
except Company.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)

Expand Down

0 comments on commit 2083b1e

Please sign in to comment.