Example #1
0
 def obj_create(self, bundle, **kwargs):
     try:
         data = bundle.data
         user = bundle.request.user
         if user.is_anonymous():
             user = None
         if len(contact_is_valid(bundle)) > 0:
             raise BadRequest(contact_is_valid(bundle))
         print "user2 %s" % user
         data['dob'] = data['dob'] or None if 'dob' in data.keys(
         ) else None  # Empty string no good
         agencyId = data['agencyId']
         del data['agencyId']
         emails = [
             EmailAddress.get_or_create(email)[0]
             for email in data['emails'] if email
         ]
         map(lambda x: x.save(), emails)
         notes = [Note(content=data['notes'], user=user)]
         del data['notes']
         map(lambda x: x.save(), notes)
         titles = [
             Title(content=title) for title in data['titles'] if title
         ]
         del data['titles']
         map(lambda x: x.save(), titles)
         del data['emails']
         phone_numbers = [
             Phone(content=phone) for phone in data['phone_numbers']
             if phone
         ]
         del data['phone_numbers']
         map(lambda x: x.save(), phone_numbers)
         addresses = [
             Address(content=address) for address in data['addresses']
             if address
         ]
         map(lambda x: x.save(), addresses)
         del data['addresses']
         agency = Agency.objects.get(id=agencyId)
         thecontact = Contact(**data)
         thecontact.save()
         thecontact.titles = titles
         thecontact.emails = emails
         thecontact.phone_numbers = phone_numbers
         thecontact.addresses = addresses
         thecontact.notes = notes
         thecontact.agency_related_contacts = [agency]
         thecontact.creator = user
         thecontact.save()
         agency.save()
         bundle.obj = thecontact
         return bundle
     except Exception as e:
         logger.exception(e)
         raise BadRequest(str(e))
Example #2
0
 def obj_create(self, bundle, **kwargs):
     try:
         data = bundle.data
         user = bundle.request.user
         if user.is_anonymous():
             user = None
         if len(contact_is_valid(bundle)) > 0:
             raise BadRequest(contact_is_valid(bundle))
         print "user2 %s" % user
         data['dob'] = data['dob'] or None if 'dob' in data.keys() else None # Empty string no good
         agencyId = data['agencyId']
         del data['agencyId']
         emails = [EmailAddress.get_or_create(email)[0] for email in data['emails'] if email]
         map(lambda x: x.save(), emails)
         notes = [Note(content = data['notes'], user = user)]
         del data['notes']
         map(lambda x: x.save(), notes)
         titles = [Title(content = title) for title in data['titles'] if title]
         del data['titles']
         map(lambda x: x.save(), titles)
         del data['emails']
         phone_numbers = [Phone(content = phone) for phone in data['phone_numbers'] if phone]
         del data['phone_numbers']
         map(lambda x: x.save(), phone_numbers)
         addresses = [Address(content = address) for address in data['addresses'] if address]
         map(lambda x: x.save(), addresses)
         del data['addresses']
         agency = Agency.objects.get(id=agencyId)
         thecontact = Contact(**data)
         thecontact.save()
         thecontact.titles = titles
         thecontact.emails = emails
         thecontact.phone_numbers = phone_numbers
         thecontact.addresses = addresses
         thecontact.notes = notes
         thecontact.agency_related_contacts = [agency]
         thecontact.creator = user
         thecontact.save()
         agency.save()
         bundle.obj = thecontact
         return bundle
     except Exception as e:
         logger.exception(e)
         raise BadRequest (str(e))
Example #3
0
    def obj_update(self, bundle, **kwargs):
        data = bundle.data
        user = bundle.request.user
        contact = bundle.obj = Contact.objects.all_them().get(id=data['id'])
        if not (bundle.request.user.is_superuser or bundle.request.user.is_staff or bundle.request.user == bundle.obj.creator):
            raise BadRequest("It appears you cannot edit this contact")
        if len(contact_is_valid(bundle)) > 0:
            raise BadRequest(contact_is_valid(bundle))

        MAX_EMAIL_ADDRESSES = 1

        for field in ['first_name', 'last_name', 'middle_name', 'dob']:
            if field in data:
                setattr(contact, field, data[field])

        if 'hidden' in data:
            hidden = data['hidden']
            contact.hidden = hidden
            if hidden:
                #deactive all the emails
                map(lambda email: email.deactive(), contact.emails.all())
            else:
                for email in contact.emails.all():
                    if EmailAddress.objects.filter(content=email).count() > 1:
                        #there is more than one email
                        raise BadRequest("Another contact has been created with this email. To un-delete this contact please change its email address or remove it.")
                    else:
                        email.deprecated = None
                        email.save()

        if 'emails' in data:
            if type (data['emails']) == list:
                # Just a list of email addresses
                try:
                    for i in range(min(MAX_EMAIL_ADDRESSES, len(data['emails']))):
                        if data['emails'][i].strip() != '':
                            try:
                                email = contact.emails.all()[i]
                                email.content = data['emails'][i]
                                email.save()
                            except:
                                #the email is new and not edited
                                eaddr = EmailAddress(content = data['emails'][i])
                                eaddr.save()
                                contact.emails.add(eaddr)
                except Exception as e:
                    logger.exception("Failure to update email %s" % e)
            elif type (data['emails']) == dict:
                # By ID
                # Should be of the form { '123' : '*****@*****.**' }
                try:
                    contactEmails = contact.emails.all()
                    for pk, address in data['emails'].iteritems():
                        email = EmailAddress.objects.get(id = int(pk))
                        assert (email in contactEmails)
                        email.content = address
                        email.save()
                except Exception as e:
                    logger.exception("Failure to update email %s" % e)

        if 'notes' in data:
            notes = [Note(content = x) for x in data['notes'] if x]
            for note in notes:
                note.save()
            contact.notes = notes
        if 'phone' in data:
            phone_numbers = [Phone(content = x) for x in data['phone'] if x]
            for number in phone_numbers:
                number.save()
            contact.phone_numbers = phone_numbers
        if 'titles' in data:
            titles = [Title(content = x) for x in data['titles'] if x]
            for title in titles:
                title.save()
            contact.titles = titles
        if 'addresses' in data:
            addresses = [Address(content = x) for x in data['addresses'] if x]
            for address in addresses:
                address.save()
            contact.addresses = addresses
        contact.save()
        return bundle
Example #4
0
 def add_email(self, email):
     te, created = EmailAddress.get_or_create(email)
     if self.emails.filter(pk=te.pk).count() == 0:
         self.emails.add(te)
     te.save()
Example #5
0
    def obj_update(self, bundle, **kwargs):
        data = bundle.data
        user = bundle.request.user
        contact = bundle.obj = Contact.objects.all_them().get(id=data['id'])
        if not (bundle.request.user.is_superuser
                or bundle.request.user.is_staff
                or bundle.request.user == bundle.obj.creator):
            raise BadRequest("It appears you cannot edit this contact")
        if len(contact_is_valid(bundle)) > 0:
            raise BadRequest(contact_is_valid(bundle))

        MAX_EMAIL_ADDRESSES = 1

        for field in ['first_name', 'last_name', 'middle_name', 'dob']:
            if field in data:
                setattr(contact, field, data[field])

        if 'hidden' in data:
            hidden = data['hidden']
            contact.hidden = hidden
            if hidden:
                #deactive all the emails
                map(lambda email: email.deactive(), contact.emails.all())
            else:
                for email in contact.emails.all():
                    if EmailAddress.objects.filter(content=email).count() > 1:
                        #there is more than one email
                        raise BadRequest(
                            "Another contact has been created with this email. To un-delete this contact please change its email address or remove it."
                        )
                    else:
                        email.deprecated = None
                        email.save()

        if 'emails' in data:
            if type(data['emails']) == list:
                # Just a list of email addresses
                try:
                    for i in range(
                            min(MAX_EMAIL_ADDRESSES, len(data['emails']))):
                        if data['emails'][i].strip() != '':
                            try:
                                email = contact.emails.all()[i]
                                email.content = data['emails'][i]
                                email.save()
                            except:
                                #the email is new and not edited
                                eaddr = EmailAddress(content=data['emails'][i])
                                eaddr.save()
                                contact.emails.add(eaddr)
                except Exception as e:
                    logger.exception("Failure to update email %s" % e)
            elif type(data['emails']) == dict:
                # By ID
                # Should be of the form { '123' : '*****@*****.**' }
                try:
                    contactEmails = contact.emails.all()
                    for pk, address in data['emails'].iteritems():
                        email = EmailAddress.objects.get(id=int(pk))
                        assert (email in contactEmails)
                        email.content = address
                        email.save()
                except Exception as e:
                    logger.exception("Failure to update email %s" % e)

        if 'notes' in data:
            notes = [Note(content=x) for x in data['notes'] if x]
            for note in notes:
                note.save()
            contact.notes = notes
        if 'phone' in data:
            phone_numbers = [Phone(content=x) for x in data['phone'] if x]
            for number in phone_numbers:
                number.save()
            contact.phone_numbers = phone_numbers
        if 'titles' in data:
            titles = [Title(content=x) for x in data['titles'] if x]
            for title in titles:
                title.save()
            contact.titles = titles
        if 'addresses' in data:
            addresses = [Address(content=x) for x in data['addresses'] if x]
            for address in addresses:
                address.save()
            contact.addresses = addresses
        contact.save()
        return bundle
Example #6
0
 def add_email(self, email):
     te, created = EmailAddress.get_or_create(email)
     if self.emails.filter(pk=te.pk).count() == 0:
         self.emails.add(te)
     te.save()