示例#1
0
def test_position(session):
    position = add_position('testposition')

    position2 = AdPosition.get(position.id)
    assert position2.name == 'testposition'

    unit = add_unit('testunit', 300)
    assert len(position.units) == 0
    position.units = [unit]

    position3 = AdPosition.get(position.id)
    assert unit in position3.units
    assert position3 in unit.positions
示例#2
0
def new_position():
    form = PositionForm(request.form)
    form.estimate_num.hidden = True
    form.cpd_num.hidden = True
    form.max_order_num.hidden = True
    # the medium and size field will be set value at the same time: any data post to server side
    if form.medium.data and form.size.data:
        form_size = AdSize.get(form.size.data)
        form.units.choices = [(x.id, x.display_name)
                              for x in AdUnit.query.filter_by(
                                  medium_id=form.medium.data, size=form_size)]
    if request.method == 'POST' and form.validate():
        ad_position = AdPosition.add(name=form.name.data,
                                     description=form.description.data,
                                     size=AdSize.get(form.size.data),
                                     standard=form.standard.data,
                                     status=form.status.data,
                                     medium=Medium.get(form.medium.data),
                                     level=form.level.data,
                                     ad_type=form.ad_type.data,
                                     launch_strategy=form.launch_strategy.data,
                                     price=form.price.data)
        ad_position.units = AdUnit.gets(form.units.data)
        ad_position.cpd_num = sum([x.estimate_num for x in ad_position.units])
        ad_position.max_order_num = sum(
            [x.estimate_num for x in ad_position.units])
        ad_position.save()
        flash(u'新建展示位置成功!', 'success')
        return redirect(
            url_for("medium.position_detail", position_id=ad_position.id))
    return tpl('position.html', form=form, title=u"新建展示位置")
示例#3
0
def storage():
    dates_info = {}
    start_str = request.args.get('start_date',
                                 datetime.now().strftime(DATE_FORMAT))
    start = datetime.strptime(start_str, DATE_FORMAT).date()
    m_dict = defaultdict(list)
    dates_list = []
    for x in range(0, 30):
        current = start + timedelta(days=x)
        m_dict[current.month].append(current)
        dates_list.append(current)
    dates_info['dates'] = dates_list
    dates_info['months'] = [(m, len(d_list)) for m, d_list in m_dict.items()]
    medium_id = int(request.args.get('selected_medium', 0))
    select_medium = [(m.id, m.name) for m in Medium.all()]
    select_medium.insert(0, (0, u'全部媒体'))
    positions_info = AdPosition.all_positions_info_by_date()
    if medium_id:
        medium = Medium.get(medium_id)
        positions_info = medium.positions_info_by_date()
    return tpl(
        'storage.html',
        dates_info=dates_info,
        medium=select_medium,
        medium_id=medium_id,
        positions_info=positions_info,
        start_date=start_str,
        per_start_date=(start - timedelta(days=30)).strftime(DATE_FORMAT),
        next_start_date=(start + timedelta(days=30)).strftime(DATE_FORMAT))
示例#4
0
def position_detail(position_id):
    position = AdPosition.get(position_id)
    if not position:
        abort(404)
    form = PositionForm(request.form)
    form.units.choices = [(x.id, x.display_name)
                          for x in position.suitable_units]

    if request.method == 'POST':
        if form.validate():
            position.name = form.name.data
            position.description = form.description.data
            position.size = AdSize.get(form.size.data)
            position.standard = form.standard.data
            position.status = form.status.data
            position.units = AdUnit.gets(form.units.data)
            position.medium = Medium.get(form.medium.data)
            position.level = form.level.data
            position.ad_type = form.ad_type.data
            position.cpd_num = form.cpd_num.data
            position.max_order_num = form.max_order_num.data
            position.price = form.price.data
            position.launch_strategy = form.launch_strategy.data
            position.save()
            flash(u'保存成功!', 'success')
        else:
            flash(u'保存失败!', 'danger')
        return tpl('position.html',
                   form=form,
                   title=position.display_name,
                   position=position)
    else:
        form.name.data = position.name
        form.description.data = position.description
        form.size.data = position.size.id
        form.standard.data = position.standard
        form.status.data = position.status
        form.units.data = [x.id for x in position.units]
        form.medium.data = position.medium.id
        form.level.data = position.level
        form.ad_type.data = position.ad_type
        form.cpd_num.data = position.cpd_num or 0
        form.max_order_num.data = position.max_order_num
        form.price.data = position.price
        form.estimate_num.data = position.estimate_num
        form.launch_strategy.data = position.launch_strategy
        form.estimate_num.readonly = True
        sortby = request.args.get('sortby', '')
        orderby = request.args.get('orderby', '')
        reverse = orderby != 'asc'
        items = [o for o in position.order_items]
        schedules = AdItem.items_sort_scheduels(items, sortby, reverse)
    return tpl('position.html',
               form=form,
               title=position.display_name,
               position=position,
               schedules=schedules,
               orderby=orderby)
示例#5
0
    def __init__(self, *args, **kwargs):
        super(UnitForm, self).__init__(*args, **kwargs)
        self.target.choices = TARGET_CN.items()
        self.status.choices = STATUS_CN.items()
        self.size.choices = [(x.id, x.name) for x in AdSize.all()]
        self.positions.choices = [(x.id, x.display_name)
                                  for x in AdPosition.all()]

        self.medium.choices = [(x.id, x.name) for x in Medium.all()]
示例#6
0
    def __init__(self, *args, **kwargs):
        super(UnitForm, self).__init__(*args, **kwargs)
        self.target.choices = TARGET_CN.items()
        self.status.choices = STATUS_CN.items()
        self.size.choices = [(x.id, x.name) for x in AdSize.all()]
        self.positions.choices = [(x.id, x.display_name)
                                  for x in AdPosition.all()]

        self.medium.choices = [(x.id, x.name) for x in Medium.all()]
示例#7
0
def unit_detail(unit_id):
    unit = AdUnit.get(unit_id)
    if not unit:
        abort(404)
    form = UnitForm(request.form)

    if request.method == 'POST':
        if form.validate():
            unit.name = form.name.data
            unit.description = form.description.data
            unit.size = AdSize.get(form.size.data)
            unit.margin = form.margin.data
            unit.target = form.target.data
            unit.status = form.status.data
            unit.positions = AdPosition.gets(form.positions.data)
            unit.medium = Medium.get(form.medium.data)
            unit.estimate_num = form.estimate_num.data
            unit.save()
            flash(u'保存成功!', 'success')

        if form.medium.data:
            form.positions.choices = [
                (x.id, x.display_name)
                for x in AdPosition.query.filter_by(medium_id=form.medium.data)
            ]
            return tpl('unit.html',
                       form=form,
                       title=unit.display_name,
                       unit=unit)
    else:
        form.name.data = unit.name
        form.description.data = unit.description
        form.size.data = unit.size.id
        form.margin.data = unit.margin
        form.target.data = unit.target
        form.status.data = unit.status
        form.positions.data = [x.id for x in unit.positions]
        form.positions.choices = [
            (x.id, x.display_name)
            for x in AdPosition.query.filter_by(medium_id=unit.medium.id)
        ]
        form.medium.data = unit.medium.id
        form.estimate_num.data = unit.estimate_num
        sortby = request.args.get('sortby', '')
        orderby = request.args.get('orderby', '')
        reverse = orderby != 'asc'
        items = [o for o in unit.order_items]
        schedules = AdItem.items_sort_scheduels(items, sortby, reverse)
    return tpl('unit.html',
               form=form,
               title=unit.display_name,
               unit=unit,
               schedules=schedules,
               orderby=orderby)
示例#8
0
 def validate(self):
     if Form.validate(self):
         positions = AdPosition.gets(self.positions.data)
         medium = Medium.get(self.medium.data)
         for p in positions:
             if p.medium != medium:
                 self.positions.errors.append(
                     u"%s不属于%s" % (p.display_name, medium.name))
                 return False
         return True
     else:
         return False
示例#9
0
 def validate(self):
     if Form.validate(self):
         positions = AdPosition.gets(self.positions.data)
         medium = Medium.get(self.medium.data)
         for p in positions:
             if p.medium != medium:
                 self.positions.errors.append(u"%s不属于%s" %
                                              (p.display_name, medium.name))
                 return False
         return True
     else:
         return False
示例#10
0
def add_schedules(order, data):
    """新增订单项, 排期项"""
    items = json.loads(data)
    for item in items:
        position = AdPosition.get(item['position'])
        ad_item = AdItem.add(order=order, sale_type=item['sale_type'], special_sale=item['special_sale'],
                             position=position, creator=g.user, create_time=datetime.now())
        ad_item.price = position.price
        ad_item.description = item['description']
        ad_item.item_status = ITEM_STATUS_NEW
        ad_item.save()
        for (date_str, num_str) in item['schedule'].items():
            _date = datetime.strptime(date_str, DATE_FORMAT).date()
            num = int(num_str)
            AdSchedule.add(item=ad_item, num=num, date=_date)
示例#11
0
def check_schedules_post(data):
    """检查排期数据是否合法"""
    try:
        items = json.loads(data)
    except ValueError:
        return '1', "JSON数据格式出错啦"
    else:
        status = 0
        msg = ''
        for item in items:
            position = AdPosition.get(item['position'])
            for (date_str, num_str) in item['schedule'].items():
                _date = datetime.strptime(date_str, DATE_FORMAT).date()
                num = int(num_str)
                if position.can_order_num(_date) < num:
                    status = 1
                    msg += u'%s 最多只能预订 %s \n' % (position.display_name, position.can_order_num(_date))
    return status, msg
示例#12
0
def unit_to_position(unit_id):
    unit = AdUnit.get(unit_id)
    form = PositionForm(request.form)
    form.estimate_num.hidden = True
    form.cpd_num.hidden = True
    form.max_order_num.hidden = True
    form.medium.hidden = True
    form.units.hidden = True

    if request.method == 'POST':
        if form.validate():
            position = AdPosition.add(name=form.name.data,
                                      description=form.description.data,
                                      size=AdSize.get(form.size.data),
                                      standard=form.standard.data,
                                      status=form.status.data,
                                      medium=Medium.get(form.medium.data),
                                      level=form.level.data,
                                      ad_type=form.ad_type.data,
                                      price=form.price.data)
            position.units = AdUnit.gets(form.units.data)
            position.cpd_num = sum([x.estimate_num for x in position.units])
            position.max_order_num = sum(
                [x.estimate_num for x in position.units])
            position.save()
            return redirect(
                url_for("medium.position_detail", position_id=position.id))
    else:
        form.name.data = unit.name
        form.description.data = unit.description
        form.medium.data = unit.medium.id
        form.size.data = unit.size.id
        form.status.data = unit.status
        form.units.data = [unit.id]

    form.size.hidden = True
    return tpl('position.html',
               form=form,
               title=u"创建广告单元(%s)对应的展示位置" % unit.name)
示例#13
0
def new_unit():
    form = UnitForm(request.form)

    if form.medium.data:
        form.positions.choices = [
            (x.id, x.display_name)
            for x in AdPosition.query.filter_by(medium_id=form.medium.data)
        ]

    if request.method == 'POST' and form.validate():
        adUnit = AdUnit.add(name=form.name.data,
                            description=form.description.data,
                            size=AdSize.get(form.size.data),
                            margin=form.margin.data,
                            target=form.target.data,
                            status=form.status.data,
                            medium=Medium.get(form.medium.data),
                            estimate_num=form.estimate_num.data)
        adUnit.positions = AdPosition.gets(form.positions.data)
        adUnit.save()
        return redirect(url_for("medium.unit_to_position", unit_id=adUnit.id))
    return tpl('unit.html', form=form, title=u"新建广告单元")
示例#14
0
def storage():
    dates_info = {}
    start_str = request.args.get('start_date', datetime.now().strftime(DATE_FORMAT))
    start = datetime.strptime(start_str, DATE_FORMAT).date()
    m_dict = defaultdict(list)
    dates_list = []
    for x in range(0, 30):
        current = start + timedelta(days=x)
        m_dict[current.month].append(current)
        dates_list.append(current)
    dates_info['dates'] = dates_list
    dates_info['months'] = [(m, len(d_list)) for m, d_list in m_dict.items()]
    medium_id = int(request.args.get('selected_medium', 0))
    select_medium = [(m.id, m.name) for m in Medium.all()]
    select_medium.insert(0, (0, u'全部媒体'))
    positions_info = AdPosition.all_positions_info_by_date()
    if medium_id:
        medium = Medium.get(medium_id)
        positions_info = medium.positions_info_by_date()
    return tpl('storage.html', dates_info=dates_info, medium=select_medium,
               medium_id=medium_id, positions_info=positions_info, start_date=start_str,
               per_start_date=(start - timedelta(days=30)).strftime(DATE_FORMAT),
               next_start_date=(start + timedelta(days=30)).strftime(DATE_FORMAT))
示例#15
0
def schedule_info():
    """ajax 获取排期数据"""
    start_date = datetime.strptime(request.values.get('start'), DATE_FORMAT).date()
    end_date = datetime.strptime(request.values.get('end'), DATE_FORMAT).date()
    position = AdPosition.get(request.values.get('position'))
    return jsonify(position.get_schedule(start_date, end_date))
示例#16
0
def storage_info():
    """ajax 获取库存数据"""
    date = datetime.strptime(request.values.get('date'), DATE_FORMAT).date()
    position = AdPosition.get(request.values.get('position_id'))
    return jsonify(position.get_storage_info(date))
示例#17
0
def position_list():
    # order = Order.get(request.values.get('order'))
    # sale_type = request.values.get('sale_type')
    # special_sale = request.values.get('special_sale')
    return jsonify([(p.id, p.display_name) for p in AdPosition.all()])
示例#18
0
def positions():
    positions = AdPosition.all()
    return tpl('positions.html', positions=positions)
示例#19
0
def storage_info():
    """ajax 获取库存数据"""
    date = datetime.strptime(request.values.get('date'), DATE_FORMAT).date()
    position = AdPosition.get(request.values.get('position_id'))
    return jsonify(position.get_storage_info(date))
示例#20
0
def add_position(name, medium=None, size=None):
    size = size or add_size(300, 50)
    medium = medium or add_medium(TEST_MEDIUM)
    position = AdPosition.add(
        name=name, description='', size=size, standard='', status=1, medium=medium)
    return position