Exemplo n.º 1
0
    def handle(self, *args, **options):
        if options['name'] and ' ' in options['name']:
            first_name, last_name = options['name'].split(' ')
        elif options['name']:
            first_name = options['name']
            last_name = ""
        else:
            first_name = options['username']
            last_name = ""

        if options['password']:
            password = options['password']
        else:
            password = getpass()

        person = Person(
            first_name=first_name,
            last_name=last_name,
            email=options['email'],
        )
        person.save()
        user = PS1User.objects.create_superuser(
            options['username'],
            email=options['email'],
            first_name=first_name,
            last_name=last_name,
        )

        self.set_password(user, password)

        person.user = user
        person.save()
Exemplo n.º 2
0
 def setUp(self):
     self.person = Person(
                 first_name = "Jay",
                 last_name="Hacker",
                 email="*****@*****.**",
                 membership_status="Starving",
                 )
     self.person.save()
Exemplo n.º 3
0
    def handle(self, *args, **options):
        data = json.loads(open(args[0], 'rb').read())
        self.id_map = {}

        Person.objects.all().delete()
        MemberPoint.objects.all().delete()

        for entry in data:
            if entry.has_key('contact_id'):
                with transaction.atomic(), reversion.create_revision():
                    reversion.set_comment("Imported from Zoho CRM")
                    crm_person = Person()
                    self.id_map[entry['contact_id']] = crm_person
                    dob = entry['date_of_birth']
                    if dob:
                        crm_person.birthday = dateutil.parser.parse(dob).date()
                    crm_person.city = entry['mailing_city']
                    crm_person.first_name = entry['first_name']

                    # for some reason the last name field contains the first name and salutation
                    if entry['salutation']:
                        starter_text = entry['salutation'] + " " + entry[
                            'first_name']
                    else:
                        starter_text = entry['first_name']
                    if entry['last_name'].strip().startswith(starter_text):
                        crm_person.last_name = entry['last_name'].strip(
                        )[len(starter_text):].strip()
                    else:
                        crm_person.last_name = entry['last_name'].strip()

                    crm_person.email = entry['primary_email']
                    crm_person.street_address = entry['mailing_street']
                    crm_person.mailing_city = entry['mailing_city']
                    crm_person.mailing_state = entry['mailing_state']
                    crm_person.zip_code = entry['mailing_zip']
                    crm_person.country = entry['mailing_country']

                    start_date = entry['membership_start_date']
                    if start_date:
                        crm_person.membership_start_date = dateutil.parser.parse(
                            start_date).date()

                    crm_person.membership_status = membership_status_map[
                        entry['membership_status']]
                    crm_person.save()

                    # Port
                    try:
                        existing_contact = Contact.objects.get(
                            contact_id=entry['contact_id'])
                        crm_person.user = existing_contact.user
                        crm_person.save()
                    except Contact.DoesNotExist:
                        pass

                    if (entry['payment_type'] == 'Paypal'
                            and entry['primary_email']
                        ) or entry['secondary_email']:
                        paypal = PayPal()
                        paypal.person = crm_person
                        if entry['secondary_email']:
                            paypal.email = entry['secondary_email']
                        else:
                            paypal.email = entry['primary_email']
                        if entry['membership_end_date']:
                            paypal.paid_up_until = dateutl.parser.parse(
                                entry['membership_end_date']).replace(
                                    tzinfo=tz)
                        paypal.save()

                    elif entry['payment_type'] == 'Cash' and entry[
                            'membership_end_date']:
                        cash = Cash()
                        cash.person = crm_person
                        cash.paid_up_until = dateutl.parser.parse(
                            entry['membership_end_date']).replace(tzinfo=tz)
                        cash.save()

                    # member points
                    if entry['member_points']:
                        points = int(float(entry['member_points']))
                        for point in range(1, points):
                            with transaction.atomic(
                            ), reversion.create_revision():
                                reason = "imported from zoho for {} {}".format(
                                    crm_person.first_name,
                                    crm_person.last_name)
                                MemberPoint.objects.create(
                                    reason=reason, owner=crm_person.user)

        for entry in data:

            if self.id_check(entry):
                pass
            elif entry.has_key('note_id'):
                note = Note()
                note.person = self.id_map[entry['parent_contact_id']]
                if entry['title']:
                    note.content = entry['title'].strip(
                    ) + ": " + entry['body']
                else:
                    note.content = entry['body']
                created_at = dateutil.parser.parse(
                    entry['created_at']).replace(tzinfo=tz)
                username = self.get_ps1user(created_at, entry['by'])
                user = PS1User.objects.get_users_by_field(
                    "sAMAccountName", username)[0]
                note.author = user
                note.save()
                note.created_at = created_at
                note.save()

            elif entry.has_key('msg_id'):
                email_record = EmailRecord()
                email_record.recipient = self.id_map[
                    entry['contact_zoho_crm_id']]
                email_record.subject = entry['subject']
                email_record.to_email = entry['to_email']
                email_record.from_email = entry['from_email']
                email_record.message = entry['body']
                email_record.status = "zoho"
                created_at = dateutil.parser.parse(
                    entry['sent_time']).replace(tzinfo=tz)
                email_record.sender = self.get_ps1user(created_at,
                                                       entry['sender'])
                email_record.save()
                email_record.created_at = created_at
                email_record.save()