Exemplo n.º 1
0
def save_profile(backend, user, response, *args, **kwargs):

    if backend.name == 'facebook':

        try:
            customer = user.get_customer()
        except:
            customer = None
        if customer is None:
            user.is_customer = True
            user.save()
            customer = Customer(user_id=user.id)
            try:
                customer.birthday = datetime.datetime.strptime(
                    response['birthday'], '%m/%d/%Y').strftime('%Y-%m-%d')
            except:
                pass
            try:
                url = "http://graph.facebook.com/%s/picture?type=large" \
                    % response["id"]
                if url:
                    from urllib.request import urlopen, HTTPError
                    from django.template.defaultfilters import slugify
                    from django.core.files.base import ContentFile
                    avatar = urlopen(url)
                    customer.profile_picture.save(
                        slugify(user.username + " social") + '.jpg',
                        ContentFile(avatar.read()))
            except HTTPError:
                pass
            customer.save()
            Activity.push(user, 201,
                          user.username + ' is register with facebook')
        Activity.push(user, 101,
                      user.username + ' log in as customer with facebook.')
Exemplo n.º 2
0
def customer_create(request):
    params = json.loads(request.body.decode("utf-8"))
    data = {}

    blacklist = Blacklist.objects.all()
    for man in blacklist:
        nameRatio = fuzz.partial_ratio(man.name, params['name'])
        nationRatio = fuzz.partial_ratio(man.nationality, params['nation'])
        addrRatio = fuzz.partial_ratio(man.address, params['address'])
        print(nameRatio, addrRatio, nationRatio)
        if (nameRatio > 70 or addrRatio > 80
                or (nameRatio + nationRatio) > 170):
            log = AlertLog()
            log.name = params['name']
            log.operate = '開戶'
            log.reason = '疑似為高風險或黑名單人物'
            log.save()
            return common_response(data, message='黑名單人物')

    try:
        with transaction.atomic():
            distrcit = District.objects.get(id=params['district']['id'])
            customer = Customer()
            customer.name = params['name']
            customer.roc_id = params['roc_id']
            customer.gender = params['gender']
            customer.birthday = params['birthday']
            customer.cell_phone = params['cell_phone']
            customer.address = params['address']
            customer.district = distrcit
            customer.password = get_sha256_value(params['cell_phone'])
            customer.email = params['email']
            customer.save()
            data['id'] = customer.id

            # 產生帳戶號碼
            code = generate_16_digits_code()
            exist_codes = [account.code for account in Account.objects.all()]
            while code in exist_codes:
                code = generate_16_digits_code()

            # 新增帳戶
            account = Account()
            account.code = code
            account.customer = Customer.objects.get(id=customer.id)

            # 預設第1筆(新台幣)
            currency_list = Currency.objects.filter(id=1)
            if len(currency_list) != 0:
                account.currency = currency_list[0]
            account.save()

        return common_response(data)
    except Exception as e:
        return common_response(data, message=str(e))