def api_dealers_create(request):
    return_list = []
    f = open("/Users/peello/Downloads/dealers.txt")
    for line in f:
        line_split = line.split("|")
        dealer_dict = {
            "id": int(line_split[0]),
            "userId": int(line_split[1]),
            "repAccountId": line_split[2],
            "accountId": line_split[3],
            "company": line_split[4]
        }
        return_list.append(dealer_dict)

        rep_exists = False
        try:
            rep = UserProfile.objects.get(account_id=dealer_dict["repAccountId"])
            rep_exists = True

        except Exception as e:
            print("  ** REP NOT FOUND - {}".format(dealer_dict["repAccountId"]))

        if rep_exists:
            try:
                user = User(username=dealer_dict["accountId"], is_staff=False, is_active=True, is_superuser=False)
                user.set_password(dealer_dict["accountId"])
                user.save()

                user_profile = UserProfile(user=user,
                                           account_rep=rep,
                                           user_type="DEALER",
                                           account_id=dealer_dict["accountId"],
                                           company=dealer_dict["company"])
                user_profile.save()
                print("  ** CREATED - UserId (%s)  UserProfileId (%s)" % (user.pk, user_profile.pk))
            except Exception as e:
                print("  ** ERROR - %s : %s" % (dealer_dict["accountId"], e))
    f.close()

    return HttpResponse(json.dumps(return_list), content_type="application/json")
def import_document_partner(document_partner):
    return_value = "Success"
    try:
        # First, lets get the rep for this partner, if there is no rep, do not import...
        rep_user_profile = UserProfile.objects.get(account_id=document_partner["rep_id"])

        # Now lets import the partner...
        create_partner = False
        partner_exists = False
        try:
            user = User.objects.get(username=document_partner["account_id"])
            user_profile = UserProfile.objects.get(user=user)
            user_profile.account_rep = rep_user_profile
            user_profile.company = document_partner["name"][:100]
            user_profile.save()

            partner_exists = True

        except Exception as e:
            create_partner = True

        if create_partner:
            if document_partner["account_id"] and document_partner["name"]:
                try:
                    user = User(username=document_partner["account_id"][:30], is_staff=False, is_active=True,
                                is_superuser=False)
                    user.set_password(document_partner["account_id"][:30])
                    user.first_name = document_partner["name"][:30]
                    user.save()

                    user_profile = UserProfile(user=user, user_type="DEALER",
                                               account_id=document_partner["account_id"][:50],
                                               account_rep=rep_user_profile, company=document_partner["name"][:100])
                    user_profile.save()
                    partner_exists = True

                except Exception as e:
                    return_value = "Import partner (%s) : %s" % (document_partner["account_id"], e)
            else:
                return_value = "No account_id or name"

        if partner_exists:
            # First, lets delete all existing addresses for this partner.
            try:
                address_list = UserAddress.objects.filter(user_profile=user_profile).all()
                for address in address_list:
                    address.delete()

            except Exception as e:
                pass

            # Import BillTo address...
            if document_partner["billToName"] and document_partner["billToStreet"] and document_partner[
                "billToCity"] and document_partner["billToRegion"] and document_partner["billToPostalCode"] and \
                    document_partner["billToCountry"]:
                try:
                    user_address = UserAddress(user_profile=user_profile, address_type="BILLTO",
                                               name=document_partner["billToName"][:200],
                                               address1=document_partner["billToStreet"][:50],
                                               city=document_partner["billToCity"][:30],
                                               state=document_partner["billToRegion"][:30],
                                               postal_code=document_partner["billToPostalCode"][:9],
                                               country=document_partner["billToCountry"][:30])
                    user_address.save()
                except Exception as e:
                    pass

            # Import ShipTo addresses...
            for ship_to in document_partner["shipTo"]:
                if ship_to["shipToName"] and ship_to["shipToAddressId"] and ship_to["shipToStreet"] \
                        and ship_to["shipToCity"] and ship_to["shipToRegion"] and ship_to["shipToPostalCode"] \
                        and ship_to["shipToCountry"]:
                    # First see if it exists so we don't import duplicate addresses
                    try:
                        user_address = UserAddress.objects.get(user_profile=user_profile, address_type="SHIPTO",
                                                               address_id=ship_to["shipToAddressId"][:200])
                    except Exception as e:
                        try:
                            user_address = UserAddress(user_profile=user_profile, address_type="SHIPTO",
                                                       name=ship_to["shipToName"][:200],
                                                       address_id=ship_to["shipToAddressId"][:200],
                                                       address1=ship_to["shipToStreet"][:50],
                                                       city=ship_to["shipToCity"][:30],
                                                       state=ship_to["shipToRegion"][:30],
                                                       postal_code=ship_to["shipToPostalCode"][:9],
                                                       country=ship_to["shipToCountry"][:30])
                            user_address.save()
                        except Exception as e:
                            pass

    except Exception as e:
        return_value = "Rep not found (%s) : %s" % (document_partner["rep_id"], e)

    return return_value
def api_reps_create(request):
    rep_list = []
    rep_list.append({"company": "Raz Hodgeman", "first_name": "Raz", "last_name": "Hodgeman", "account_id": "056", "email": "*****@*****.**", "phone": "949-374-4011"})
    rep_list.append({"company": "Raz Hodgeman", "first_name": "Raz", "last_name": "Hodgeman", "account_id": "058", "email": "*****@*****.**", "phone": "949-374-4011"})
    rep_list.append({"company": "Mike Burns", "first_name": "Mike", "last_name": "Burns", "account_id": "041", "email": "*****@*****.**", "phone": "760-473-7445"})
    rep_list.append({"company": "Greg Taylor (GT)", "first_name": "Greg", "last_name": "Taylor", "account_id": "051", "email": "*****@*****.**", "phone": "904-806-4169"})
    rep_list.append({"company": "Greg Taylor (GT)", "first_name": "Greg", "last_name": "Taylor", "account_id": "052", "email": "*****@*****.**", "phone": "904-806-4169"})
    rep_list.append({"company": "Ernie Yagi", "first_name": "Ernie", "last_name": "Yagi", "account_id": "005", "email": "*****@*****.**", "phone": "808-479-9343"})
    rep_list.append({"company": "Mark Neustadter", "first_name": "Mark", "last_name": "Neustadter", "account_id": "039", "email": "*****@*****.**", "phone": "609-442-4009"})
    rep_list.append({"company": "Lance & Amy Gilman", "first_name": "Lance", "last_name": "Gilman", "account_id": "055", "email": "*****@*****.**", "phone": "702-862-9095"})
    rep_list.append({"company": "Brian Jolin", "first_name": "Brian", "last_name": "Jolin", "account_id": "035", "email": "*****@*****.**", "phone": "817-368-3061"})
    rep_list.append({"company": "Karen Moldstad", "first_name": "Karen", "last_name": "Moldstad", "account_id": "009", "email": "*****@*****.**", "phone": "206-295-5626"})
    rep_list.append({"company": "Rose Macke", "first_name": "Rose", "last_name": "Macke", "account_id": "024", "email": "mackeclan@cox.", "phone": "949-433-1626"})
    rep_list.append({"company": "Paul Kuchno", "first_name": "Paul", "last_name": "Kuchno", "account_id": "080", "email": "*****@*****.**", "phone": "787-603-8535"})
    rep_list.append({"company": "Kevin Campion", "first_name": "Kevin", "last_name": "Campion", "account_id": "085", "email": "*****@*****.**", "phone": "805-550-5562"})
    rep_list.append({"company": "Jeff Clark", "first_name": "Jeff", "last_name": "Clark", "account_id": "083", "email": "*****@*****.**", "phone": "503-706-1775"})
    rep_list.append({"company": "Marc Angelillo", "first_name": "Marc", "last_name": "Angelillo", "account_id": "029", "email": "*****@*****.**", "phone": "617-842-9705"})
    rep_list.append({"company": "Barry Mayers", "first_name": "Barry", "last_name": "Mayers", "account_id": "115", "email": "*****@*****.**", "phone": "246-428-4390"})
    rep_list.append({"company": "Gabriella", "first_name": "Gabriella", "last_name": "", "account_id": "106", "email": "*****@*****.**", "phone": "599-767-2252"})
    rep_list.append({"company": "PJ Bahama", "first_name": "PJ", "last_name": "Bahama", "account_id": "111", "email": "*****@*****.**", "phone": "242-394-3910"})
    rep_list.append({"company": "Geary Guay", "first_name": "Geary", "last_name": "Guay", "account_id": "025", "email": "*****@*****.**", "phone": "303-517-9928"})
    rep_list.append({"company": "Sheri Letow", "first_name": "Sheri", "last_name": "Letow", "account_id": "078", "email": "*****@*****.**", "phone": "469-366-1518"})

    count = 0
    for rep in rep_list:
        count += 1

        print("%s - %s : %s" % (count, rep["account_id"], rep["company"]))

        create_rep = False
        try:
            user = User.objects.get(username=rep["account_id"])
            user.first_name = rep["first_name"]
            user.last_name = rep["last_name"]
            user.email = rep["email"]
            user.save()

            user_profile = UserProfile.objects.get(user=user)
            user_profile.company = rep["company"]
            user_profile.phone = rep["phone"]
            user_profile.save()

            print("  ** FOUND - UserId (%s)  UserProfileId (%s)" % (user.pk, user_profile.pk))

        except Exception as e:
            print("  ** NOT FOUND - %s" % (e))
            create_rep = True

        if create_rep:
            try:
                user = User(username=rep["account_id"], is_staff=False, is_active=True, is_superuser=False)
                user.set_password(rep["account_id"])
                user.first_name = rep["first_name"]
                user.last_name = rep["last_name"]
                user.email = rep["email"]
                user.save()

                user_profile = UserProfile(user=user, user_type="REP", account_id=rep["account_id"], company=rep["company"], phone=rep["phone"])
                user_profile.save()
                print("  ** CREATED - UserId (%s)  UserProfileId (%s)" % (user.pk, user_profile.pk))
            except Exception as e:
                print("  ** ERROR - import_rep (%s) : %s" % (rep["account_id"], e))

    return HttpResponse(json.dumps(rep_list), content_type="application/json")