Esempio n. 1
0
def generate_lookup(store_id):
    from application.models.base import db
    ''' insert store info on lookup'''

    lookups_title = _descriptions()
    version = 1

    try:
        for name, type, description in lookups_title:
            lookup = Lookup.create(store_id=store_id,
                                   name=name,
                                   type=type,
                                   description=description,
                                   version=version)
            if lookup is not None:
                lookup.save()
        db.session.commit()
    except Exception as e:
        raise e

    result = Lookup.get_all_store_ids(store_id)

    lookup_ids = dict()
    for lookups in result:
        lookup_ids[str(lookups.name).strip()] = lookups.id

    return lookup_ids
Esempio n. 2
0
def lookupvalues_delete(lookupvalue_id):
    from application.models.lookup import Lookup
    from application.models.lookup import LookupValue
    lookupvalue = LookupValue.find(lookupvalue_id)

    version = lookupvalue.version
    LookupValue.update_version(lookupvalue.lookup_id, version, lookupvalue.id)
    Lookup.update_version(lookupvalue.lookup_id, version)

    lookup = Lookup.find(lookupvalue.lookup_id)

    from werkzeug.datastructures import ImmutableMultiDict
    request.args = ImmutableMultiDict(dict(store_filter=lookup.store_id, lookup_filter=lookup.id))
    return lookupvalues()
Esempio n. 3
0
def check_looupvalue(lookup_id, changevalues):
    from application.nutils.excel import safe_convert_unicode
    lookup_by_store_and_name = Lookup.get_description_by_store_id(lookup_id)
    description = lookup_by_store_and_name.description
    sections = LookupValue.find_all_by_lookup_id_by_order(lookup_id)[0]

    for changevalue in changevalues:
        value = changevalue['value']
        if not value and not changevalue['orders'] and not changevalue[
                'section']:
            continue
        if not value:
            return u"此行%s必须有值" % description
        else:
            value = safe_convert_unicode(value)
            if LookupValue.find_value_in_lookupvalue_by_lookup_id_by_order_by_value(
                    lookup_id, value):
                return u"%s已经存在" % value
        if not isinstance(changevalue['orders'], int):
            return u"次序必须是数值"
        if sections.section:
            if not changevalue['section']:
                return u"类别必须有值"
        else:
            if changevalue['section']:
                return u"类别必须为空"
Esempio n. 4
0
def lookupvalues_add(lookup_id):
    from application.models.lookup import Lookup
    from application.createlookupvalues import add_lookupvalue, check_looupvalue

    lookup_by_store_and_name = Lookup.get_description_by_store_id(lookup_id)
    descriptions = get_stores_descriptions_and_lookupvalues(lookup_id)
    version = lookup_by_store_and_name.version

    store_name = Store.find_storename_by_store_id(lookup_by_store_and_name.store_id).name

    if request.method == 'POST':
        changevalues = convert_changevalues(request.form)
        message = check_looupvalue(lookup_id, changevalues)
        if message:
            flash_error(message)
            return redirect(url_for('ops.lookupvalues_add', lookup_id=lookup_id), code=303)

        count = add_lookupvalue(lookup_id, version, changevalues)
        if count is None:
            flash_error(u"更新失败!")
        elif count == 0:
            flash_info(u"未发生更新!")
        else:
            flash_success(u"成功更新%s条信息!" % count)

        return redirect(url_for('ops.lookupvalues_add', lookup_id=lookup_id), code=303)

    return render_template('ops/lookupvalues_add.html', store_name=store_name, lookup_by_id=lookup_by_store_and_name,
                           descriptions=descriptions, lookup_id=lookup_id)
Esempio n. 5
0
    def match_intent_level_lookups(store_ids):
        from application.models.hwaccount import HwjdAccount
        stores = HwjdAccount.find_all_stores()
        if store_ids:
            stores = filter(lambda s: unicode(s.store_id) in store_ids, stores)
        for store_config in stores:
            from application.models.lookup import Lookup
            isr_lookup = Lookup.find_by_name_and_store(store_config.store_id,
                                                       'intent-level')

            hwjd_lookupvalues = HwjdLookup.find_by_type(u'市场-级别')
            for index, item in enumerate(hwjd_lookupvalues):
                from application.models.lookup import LookupValue
                isr_lookupvalue = LookupValue()
                isr_lookupvalue.code = item.name
                isr_lookupvalue.value = item.name
                isr_lookupvalue.lookup_id = isr_lookup.id
                isr_lookupvalue.parent_id = -1
                # order从10开始,按照10步进
                isr_lookupvalue.order = 10 + index * 10
                isr_lookupvalue.version = isr_lookup.version + 1
                isr_lookupvalue.vendor_code = item.code
                isr_lookupvalue.vendor_value = item.name
                db.session.add(isr_lookupvalue)

            isr_lookup.version += 1
Esempio n. 6
0
def lookupvalues():
    from application.forms.ops import LookupvalueForm
    from application.models.lookup import LookupValue

    selections = remove_empty_storename(get_stores_selection())

    search_lookupvalue = LookupvalueForm(request.args)
    search_lookupvalue.store_filter.choices = selections

    lookup_id = request.args.get('lookup_filter', None)
    descriptions = get_stores_descriptions_and_lookupvalues(lookup_id)

    if search_lookupvalue.store_filter.data and search_lookupvalue.store_filter.data not in ('None', 'all'):
        store_id = search_lookupvalue.store_filter.data
        store_name = Store.find_storename_by_store_id(store_id).name

        from application.models.lookup import Lookup
        id_and_description = Lookup.get_descriptions_by_store_id(store_id)
        search_lookupvalue.lookup_filter.choices = [(lookup.id, lookup.description) for lookup in id_and_description]
    else:
        store_id = ''
        store_name = ''
        search_lookupvalue.lookup_filter.choices = {}

    if lookup_id and lookup_id is not None:
        lookupvalueslist = LookupValue.find_all_by_lookup_id_by_order(lookup_id)
    else:
        lookupvalueslist = []

    return render_template('ops/lookupvalue.html', selected_menu=LOOKUPVALUE_MGMT_SUMMARY, form=search_lookupvalue,
                           lookupvalues=lookupvalueslist, descriptions=descriptions, storename=store_name,
                           lookup_id=lookup_id, store_id=store_id)
Esempio n. 7
0
def get_lookups(store_id):
    # workaround the issue client side lose store id after ucenter related upgrade
    if store_id <= 0:
        abort(401)

    names = request.args.getlist('names[]', None)

    if names:
        lookups = Lookup.find_all_by_names_of_store(store_id, names)
    else:
        lookups = Lookup.find_all_by_store_wo_page(store_id)

    lookups_dict = {}
    for lookup in lookups:
        lookups_dict[lookup.name] = lookup

    return lookups_dict.values()
Esempio n. 8
0
def get_lookupvalues_by_lookup_name(store_id, name):
    lookup = Lookup.find_by_name_and_store(store_id, name)

    if not lookup:
        abort(404,
              description=gettext(u'lookup with name %(name)s is not found',
                                  name=name))

    return LookupValue.find_all_by_lookup_id(lookup.id)
Esempio n. 9
0
def edit(selected_menu, campaign=None):
    current_user = g.user
    store_id = get_or_set_store_id()

    from application.forms.campaign import CampaignForm

    form = None
    campaign_cars = []
    if campaign:
        campaign_cars = campaign.related_cars.split(',')
        form = CampaignForm(obj=campaign)
    else:
        form = CampaignForm()

    lookup = Lookup.find_by_name_and_store(store_id, 'intent-car')

    if lookup:
        cars = LookupValue.find_all_by_lookup_id(lookup.id)
        form.related_cars.choices = [(car.code, car.value) for car in cars]

    if form.related_cars.choices is None:
        form.related_cars.choices = []

    if request.method == 'GET':
        form.related_cars.data = campaign_cars

    if request.method == 'POST' and form.validate_on_submit():
        if campaign is None:
            campaign = Campaign()
            campaign.store_id = store_id
        form.populate_obj(campaign)
        campaign.related_cars = u','.join(form.related_cars.data)
        campaign.save_and_flush()
        flash_success(gettext(u'campaign saved'))

        return redirect(url_for('campaigns.campaigns'))
        # form.
        # campaign = form.po
        # if form.validate_on_submit():
        # json_r = uc_login(form.username.data, form.password.data)
        #
        # if json_r and json_r['status'] == 0 and get_user_type(json_r) == 6:
        #     user = save_user_from_uc(json_r)
        #     add_user(user)
        #     return redirect(referer or url_for('user.dashboard'))
        #
        # flash_error(gettext(u'login failed'))

    return render_template('campaigns/edit.html',
                           selected_menu=selected_menu,
                           campaign=campaign,
                           form=form,
                           back_url=back_url(url_for('campaigns.campaigns')))
Esempio n. 10
0
def get_lookup_descriptions():
    store_id = request.args.get('store_id', None)

    if store_id is None:
        abort(400, description=gettext('store_id is empty'))

    id_and_description = Lookup.get_descriptions_by_store_id(store_id)

    lookups_dict = dict()

    for lookups in id_and_description:
        lookups_dict[lookups.id] = lookups.description

    return lookups_dict
Esempio n. 11
0
def add_lookupvalue(lookup_id, version, changevalues):
    from application.models.base import db
    from application.nutils.excel import convert_value_to_code, safe_convert_unicode

    count = 0

    try:
        for changevalue in changevalues:
            if not changevalue['value'] and not changevalue['orders']:
                continue
            lookupvalue = LookupValue.create(
                code=convert_value_to_code(
                    safe_convert_unicode(changevalue['value'])),
                value=safe_convert_unicode(changevalue['value']),
                lookup_id=lookup_id,
                parent_id='-1',
                order=safe_convert_unicode(changevalue['orders']),
                section=safe_convert_unicode(changevalue['section']),
                version=version,
            )
            db.session.add(lookupvalue)
            count += 1
        db.session.commit()
    except Exception as e:
        raise e

    if count == 0: return count

    try:
        Lookup.update_version(lookup_id, version)
        LookupValue.update_version(lookup_id, version)
        db.session.commit()
    except Exception as e:
        raise e

    return count
Esempio n. 12
0
def get_stores_descriptions_and_lookupvalues(lookup_id=None):
    from application.models.lookup import Lookup

    descriptions = dict()
    descriptions["description"] = u"意向车型"
    descriptions["id"] = u"序号"
    descriptions["code"] = u"代号"
    descriptions["value"] = u"意向车型"
    descriptions["orders"] = u"次序"
    descriptions["section"] = u"类别"
    descriptions["version"] = u"版本"

    if lookup_id is not None:
        try:
            description = Lookup.get_description_by_store_id(
                lookup_id).description
            descriptions["description"] = description.strip()
            descriptions["value"] = description.strip()
        except Exception:
            pass

    return descriptions
Esempio n. 13
0
    def match_intent_car_lookups(store_ids):
        from application.models.hwaccount import HwjdAccount
        stores = HwjdAccount.find_all_stores()
        if store_ids:
            stores = filter(lambda s: unicode(s.store_id) in store_ids, stores)
        for store_config in stores:
            hwjd_lookupvalues = HwjdLookup.find_by_make_models(
                store_config.make, store_config.models_array())

            if hwjd_lookupvalues and len(hwjd_lookupvalues) > 0:
                from application.models.lookup import Lookup
                isr_lookup = Lookup.find_by_name_and_store(
                    store_config.store_id, 'intent-car')

                for index, item in enumerate(hwjd_lookupvalues):
                    # 六位的code证明这个值不是具体车型的值,不做mapping
                    if len(item.code) > 6 and item.name != item.extra:
                        from application.models.lookup import LookupValue
                        isr_lookupvalue = LookupValue()
                        # 没用用hwjd的code是因为在报表里, 有可能直接用code来做显示了
                        from application.utils import underscore_join_str
                        isr_lookupvalue.code = u'%s_%s' % (
                            item.model, underscore_join_str(item.extra))
                        isr_lookupvalue.value = item.extra
                        isr_lookupvalue.lookup_id = isr_lookup.id
                        isr_lookupvalue.parent_id = -1
                        # order从10开始,按照10步进
                        isr_lookupvalue.order = 10 + index * 10
                        isr_lookupvalue.section = item.model
                        isr_lookupvalue.version = isr_lookup.version + 1
                        isr_lookupvalue.vendor_code = item.code
                        isr_lookupvalue.vendor_value = item.extra
                        isr_lookupvalue.vendor_section = item.name
                        db.session.add(isr_lookupvalue)

                isr_lookup.version += 1
Esempio n. 14
0
def _descriptions(store_id=2):
    from application.models.base import db
    search_result = Lookup.find_name_type_description_by_store(
        db.session, store_id)
    if len(search_result) == 0: return False
    return search_result
Esempio n. 15
0
def get_stores_descriptions(store_id):
    from application.models.lookup import Lookup

    selections = Lookup.get_descriptions_by_store_id(store_id)
    return selections
Esempio n. 16
0
def get_lookup_values_by_store(store_id):
    ids = Lookup.get_all_store_ids(store_id)
    return LookupValue.find_all_by_lookup_ids([id.id for id in ids])