Ejemplo n.º 1
0
def furniture():
    format = request.args.get('format', '', type=str)
    if format == 'json':
        data = redis_get('STYLE', 'ITEMS')
        if data is None:
            styles = statisitc.styles['available']
            data = {
                style_id: {
                    'style': styles[style_id]['style']
                }
                for style_id in styles
            }
            for style_id in data:
                if current_app.debug:
                    item_list = Item.query.filter(
                        Item.style_id == style_id).all()
                    items = [
                        random.SystemRandom().choice(item_list)
                        for _ in range(8)
                    ]
                else:
                    items = []
                    for item_id in current_app.config['ITEMS']['furniture'][
                            str(style_id)]:
                        items.append(Item.query.get(item_id))
                data[style_id]['items'] = items_json(items)
            data = json.dumps(data)
            redis_set('STYLE', 'ITEMS', data, expire=86400)
        return Response(data, mimetype='application/json')
    return render_template('user/furniture.html')
Ejemplo n.º 2
0
def oss_authorization(item_id, filename):
    dir_path = _generate_dir_path(item_id, 'item')
    image_hash = md5_with_time_salt(item_id, 'image')
    image_name = '%s.%s' % (image_hash, 'jpg')
    image_path = os.path.join(dir_path, image_name)
    callback = '{"callbackUrl": "%s", "callbackBody": "bucket=${bucket}&object=${object}&etag=${etag}"}' \
               % url_for('vendor.image_callback', _external=True)
    callback_base64 = base64.b64encode(callback.encode()).decode()
    policy_base64 = base64.b64encode(
        json.dumps(_oss_policy_generator(callback_base64)).encode()).decode()
    signature = _oss_signature_generator(policy_base64)
    params = {
        'key': image_path,
        'OSSAccessKeyId': current_app.config['OSS_ACCESS_ID'],
        'Signature': signature,
        'callback': callback_base64,
        'policy': policy_base64,
    }
    redis_set('OSS',
              image_path, {
                  'item_id': item_id,
                  'filename': filename
              },
              expire=600,
              serialize=True)
    return {
        'success': True,
        'url': 'http://%s' % current_app.config['OSS_HOST'],
        'params': params
    }
Ejemplo n.º 3
0
def brand_list():
    format = request.args.get('format', '', type=str)
    if format == 'json':
        data = redis_get('BRAND', 'ITEMS')
        if data is None:
            brands = statisitc.brands['available']
            data = {
                vendor_id: {
                    'brand': brands[vendor_id]['brand']
                }
                for vendor_id in brands
            }
            for vendor_id in data:
                if current_app.debug:
                    item_list = Item.query.filter(
                        Item.vendor_id == vendor_id, Item.is_deleted == False,
                        Item.is_component == False).all()
                    items = [
                        random.SystemRandom().choice(item_list)
                        for _ in range(5)
                    ]
                else:
                    items = current_app.config['ITEMS']['brands'][str(
                        vendor_id)]
                data[vendor_id]['items'] = items_json(items)
            data = json.dumps(data)
            redis_set('BRAND', 'ITEMS', data, expire=86400)
        return Response(data, mimetype='application/json')
    return render_template('user/brands.html')
Ejemplo n.º 4
0
def vendor_detail(vendor_id):
    if not current_app.debug and vendor_id not in [12801, 12803, 12806, 12836]:
        abort(404)
    format = request.args.get('format', '', type=str)
    if format == 'json':
        data = redis_get('BRAND_ITEMS', vendor_id)
        if data is None:
            data = {}
            if current_app.debug:
                for scene_id in [2, 3, 4, 6]:
                    scene = Scene.query.get(scene_id)
                    item_list = statisitc.item_query.filter(Item.vendor_id == vendor_id, Item.scene_id == scene_id).all()
                    if not item_list:
                        continue
                    items = [random.SystemRandom().choice(item_list) for _ in range(10)]
                    data[scene_id] = {'scene': scene.scene, 'items': items_json(items)}
            else:
                for scene_id in current_app.config['ITEMS']['vendor_detail'][str(vendor_id)].keys():
                    scene = Scene.query.get(scene_id)
                    items = []
                    for item_id in current_app.config['ITEMS']['vendor_detail'][str(vendor_id)][scene_id]:
                        items.append(Item.query.get(item_id))
                    data[scene_id] = {'scene': scene.scene, 'items': items_json(items)}
            data = json.dumps(data)
            redis_set('BRAND_ITEMS', vendor_id, data, expire=86400)
        return Response(data, mimetype='application/json')
    return render_template('user/brand_detail.html')
Ejemplo n.º 5
0
def send_sms_captcha(template, mobile):
    if not redis_get(SMS_CAPTCHA_SENT, mobile):
        captcha_chars = sms_captcha_generator()
        redis_set(SMS_CAPTCHA, mobile, captcha_chars)
        sms_generator(template, mobile, verify_code=captcha_chars)
        redis_set(SMS_CAPTCHA_SENT, mobile, True, 60)
        return True
    return False
Ejemplo n.º 6
0
 def send_email(self):
     if (self.email_type == VENDOR_EMAIL_CONFIRM or self.email_type == USER_EMAIL_CONFIRM) \
             and not self.email_confirmed:
         token = md5_with_time_salt(self.role.data, self.id.data)
         redis_set(CONFIRM_EMAIL, token, {'role': self.role.data, 'id': self.id.data,
                                          'email': self.email.data, 'action': 'confirm'}, serialize=True)
         url = url_for('service.verify', token=token, _external=True)
         send_email(self.email.data, EMAIL_CONFIRM_SUBJECT, self.email_type, url=url)
Ejemplo n.º 7
0
 def send_email(self):
     if self.email_type == USER_RESET_PASSWORD:
         token = md5_with_time_salt(self.email.data)
         redis_set(CONFIRM_EMAIL,
                   token, {
                       'email': self.email.data,
                       'action': 'reset_password'
                   },
                   serialize=True)
         url = url_for('service.verify', token=token, _external=True)
         send_email(self.email.data,
                    EMAIL_CONFIRM_SUBJECT,
                    self.email_type,
                    url=url)
Ejemplo n.º 8
0
 def send_email(self):
     if (self.email_type == VENDOR_EMAIL_CONFIRM or self.email_type == USER_EMAIL_CONFIRM) \
             and not self.email_confirmed:
         token = md5_with_time_salt(self.role.data, self.id.data)
         redis_set(CONFIRM_EMAIL,
                   token, {
                       'role': self.role.data,
                       'id': self.id.data,
                       'email': self.email.data,
                       'action': 'confirm'
                   },
                   serialize=True)
         url = url_for('service.verify', token=token, _external=True)
         send_email(self.email.data,
                    EMAIL_CONFIRM_SUBJECT,
                    self.email_type,
                    url=url)
Ejemplo n.º 9
0
def navbar():
    data = redis_get('INDEX_NAVBAR', 'ITEMS')
    if data is None:
        data = {}
        for scene_id in [2, 3, 4, 6]:   # 客厅 书房 卧室 餐厅
            scene = Scene.query.get(scene_id)
            if current_app.debug:
                item_list = statisitc.item_query.filter(Item.scene_id == scene_id).all()
                if not item_list:
                    items = []
                else:
                    items = [random.SystemRandom().choice(item_list) for _ in range(8)]
            else:
                items = current_app.config['ITEMS']['navbars'][str(scene_id)]
            data[scene.id] = {'scene': scene.scene, 'items': items_json(items)}
        data = json.dumps(data)
        redis_set('INDEX_NAVBAR', 'ITEMS', data, expire=86400)
    return Response(data, mimetype='application/json')
Ejemplo n.º 10
0
def oss_authorization(item_id, filename):
    dir_path = _generate_dir_path(item_id, 'item')
    image_hash = md5_with_time_salt(item_id, 'image')
    image_name = '%s.%s' % (image_hash, 'jpg')
    image_path = os.path.join(dir_path, image_name)
    callback = '{"callbackUrl": "%s", "callbackBody": "bucket=${bucket}&object=${object}&etag=${etag}"}' \
               % url_for('vendor.image_callback', _external=True)
    callback_base64 = base64.b64encode(callback.encode()).decode()
    policy_base64 = base64.b64encode(json.dumps(_oss_policy_generator(callback_base64)).encode()).decode()
    signature = _oss_signature_generator(policy_base64)
    params = {
        'key': image_path,
        'OSSAccessKeyId': current_app.config['OSS_ACCESS_ID'],
        'Signature': signature,
        'callback': callback_base64,
        'policy': policy_base64,
    }
    redis_set('OSS', image_path, {'item_id': item_id, 'filename': filename}, expire=600, serialize=True)
    return {'success': True, 'url': 'http://%s' % current_app.config['OSS_HOST'], 'params': params}
Ejemplo n.º 11
0
def brand_list():
    format = request.args.get('format', '', type=str)
    if format == 'json':
        data = redis_get('BRAND', 'ITEMS')
        if data is None:
            brands = statisitc.brands['available']
            data = {vendor_id: {'brand': brands[vendor_id]['brand']} for vendor_id in brands}
            for vendor_id in data:
                if current_app.debug:
                    item_list = Item.query.filter(Item.vendor_id == vendor_id, Item.is_deleted == False,
                                                  Item.is_component == False).all()
                    items = [random.SystemRandom().choice(item_list) for _ in range(5)]
                else:
                    items = current_app.config['ITEMS']['brands'][str(vendor_id)]
                data[vendor_id]['items'] = items_json(items)
            data = json.dumps(data)
            redis_set('BRAND', 'ITEMS', data, expire=86400)
        return Response(data, mimetype='application/json')
    return render_template('user/brands.html')
Ejemplo n.º 12
0
def furniture():
    format = request.args.get('format', '', type=str)
    if format == 'json':
        data = redis_get('STYLE', 'ITEMS')
        if data is None:
            styles = statisitc.styles['available']
            data = {style_id: {'style': styles[style_id]['style']} for style_id in styles}
            for style_id in data:
                if current_app.debug:
                    item_list = Item.query.filter(Item.style_id == style_id).all()
                    items = [random.SystemRandom().choice(item_list) for _ in range(8)]
                else:
                    items = []
                    for item_id in current_app.config['ITEMS']['furniture'][str(style_id)]:
                        items.append(Item.query.get(item_id))
                data[style_id]['items'] = items_json(items)
            data = json.dumps(data)
            redis_set('STYLE', 'ITEMS', data, expire=86400)
        return Response(data, mimetype='application/json')
    return render_template('user/furniture.html')
Ejemplo n.º 13
0
 def update(self):
     if self.type == USER_USERNAME_SETTING:
         if self.username.data != current_user.username and current_user.username_revisable:
             current_user.username = self.username.data
             current_user.username_revisable = False
     elif self.type == USER_PASSWORD_SETTING:
         current_user.password = self.password.data
         db.session.commit()
         logout_user()
         identity_changed.send(current_app._get_current_object(), identity=AnonymousIdentity())
         return
     else:  # email
         current_user.email = self.email.data
         current_user.email_confirmed = False
         token = md5_with_time_salt(self.email.data)
         redis_set(CONFIRM_EMAIL, token,
                   {'email': self.email.data, 'role': 'user', 'action': 'confirm', 'id': current_user.id},
                   serialize=True)
         url = url_for('service.verify', token=token, _external=True)
         send_email(self.email.data, EMAIL_CONFIRM_SUBJECT, USER_EMAIL_CONFIRM, url=url)
     db.session.commit()
Ejemplo n.º 14
0
def vendor_detail(vendor_id):
    if not current_app.debug and vendor_id not in [12801, 12803, 12806, 12836]:
        abort(404)
    format = request.args.get('format', '', type=str)
    if format == 'json':
        data = redis_get('BRAND_ITEMS', vendor_id)
        if data is None:
            data = {}
            if current_app.debug:
                for scene_id in [2, 3, 4, 6]:
                    scene = Scene.query.get(scene_id)
                    item_list = statisitc.item_query.filter(
                        Item.vendor_id == vendor_id,
                        Item.scene_id == scene_id).all()
                    if not item_list:
                        continue
                    items = [
                        random.SystemRandom().choice(item_list)
                        for _ in range(10)
                    ]
                    data[scene_id] = {
                        'scene': scene.scene,
                        'items': items_json(items)
                    }
            else:
                for scene_id in current_app.config['ITEMS']['vendor_detail'][
                        str(vendor_id)].keys():
                    scene = Scene.query.get(scene_id)
                    items = []
                    for item_id in current_app.config['ITEMS'][
                            'vendor_detail'][str(vendor_id)][scene_id]:
                        items.append(Item.query.get(item_id))
                    data[scene_id] = {
                        'scene': scene.scene,
                        'items': items_json(items)
                    }
            data = json.dumps(data)
            redis_set('BRAND_ITEMS', vendor_id, data, expire=86400)
        return Response(data, mimetype='application/json')
    return render_template('user/brand_detail.html')
Ejemplo n.º 15
0
def navbar():
    data = redis_get('INDEX_NAVBAR', 'ITEMS')
    if data is None:
        data = {}
        for scene_id in [2, 3, 4, 6]:  # 客厅 书房 卧室 餐厅
            scene = Scene.query.get(scene_id)
            if current_app.debug:
                item_list = statisitc.item_query.filter(
                    Item.scene_id == scene_id).all()
                if not item_list:
                    items = []
                else:
                    items = [
                        random.SystemRandom().choice(item_list)
                        for _ in range(8)
                    ]
            else:
                items = current_app.config['ITEMS']['navbars'][str(scene_id)]
            data[scene.id] = {'scene': scene.scene, 'items': items_json(items)}
        data = json.dumps(data)
        redis_set('INDEX_NAVBAR', 'ITEMS', data, expire=86400)
    return Response(data, mimetype='application/json')
Ejemplo n.º 16
0
def invite_distributor():
    if request.method == 'POST':
        token = md5_with_time_salt(current_user.id)
        redis_set(DISTRIBUTOR_REGISTER, token, current_user.id)
        return url_for('distributor.verify', token=token, _external=True)
    return render_template('vendor/invitation.html', vendor=current_user)
Ejemplo n.º 17
0
 def send_email(self):
     if self.email_type == USER_RESET_PASSWORD:
         token = md5_with_time_salt(self.email.data)
         redis_set(CONFIRM_EMAIL, token, {'email': self.email.data, 'action': 'reset_password'}, serialize=True)
         url = url_for('service.verify', token=token, _external=True)
         send_email(self.email.data, EMAIL_CONFIRM_SUBJECT, self.email_type, url=url)
Ejemplo n.º 18
0
def invite_distributor():
    if request.method == 'POST':
        token = md5_with_time_salt(current_user.id)
        redis_set(DISTRIBUTOR_REGISTER, token, current_user.id)
        return url_for('distributor.verify', token=token, _external=True)
    return render_template('vendor/invitation.html', vendor=current_user)
Ejemplo n.º 19
0
def image_captcha_generator(token=uuid4().hex):
    chars = id_generator()
    captcha_output = ic.generate(chars, format='jpeg')
    redis_set(IMAGE_CAPTCHA, token, chars, 3600)
    return captcha_output