Exemplo n.º 1
0
def base_rule(request):
    '''基本计费规则配置'''
    ctx = {}
    if request.method == 'POST':
        action = request.POST.get('action', '')
        if action == 'add':

            r = BaseRule()
            _save_attr_(r, request)
            park = request.POST.get('parkinglot')
            if park:
                r.parkinglot = ParkingLot.objects.filter(id=int(park)).first()
                r.save()

        elif action == 'update':
            id = request.POST.get('id', '')
            r = BaseRule.objects.filter(id=id).first()
            _save_attr_(r, request)
            park = request.POST.get('parkinglot')
            if park:
                r.parkinglot = ParkingLot.objects.filter(id=int(park)).first()
                r.save()

        elif action == 'delete':
            ids = request.POST.getlist('ids', '')
            u = BaseRule.objects.filter(id__in=ids).all()
            for item in u:
                item.status = -1
                item.save()

    ctx['parkinglots'] = ParkingLot.objects.all()
    ctx['rules'] = ctx['objects'] = BaseRule.objects.filter(status=0).all()
    return (ctx, 'base_rule.html')
Exemplo n.º 2
0
def parking_lot(request):

    ctx = {'menu': 'parkinglot'}

    if request.method == 'POST':
        action = request.POST.get('action', '')
        if action == 'add':

            r = ParkingLot()
            _save_attr_(r, request)
        elif action == 'update':
            id = request.POST.get('id', '')
            r = ParkingLot.objects.filter(id=id)
            _save_attr_(r.first(), request)

        elif action == 'delete':
            ids = request.POST.getlist('ids', '')
            u = ParkingLot.objects.filter(id__in=ids).all()
            for item in u:
                item.status = -1
                item.save()

    ctx['parkinglot'] = ctx['objects'] = ParkingLot.objects.filter(
        status=0).all()

    return (ctx, 'parking_lot.html')
Exemplo n.º 3
0
def gate(request):
    ctx = {}
    gate = Gate.objects.filter(status=0).all()

    if request.method == 'POST':
        action = request.POST.get('action', '')
        if action == 'add':
            r = Gate()
            park = request.POST.get('parkinglot')
            if park:
                r.parkinglot = ParkingLot.objects.filter(id=int(park)).first()
                r.save()
            _save_attr_(r, request)
            t = request.POST.get('use_type', '')
            if t:
                t = int(t)
            if t == 1:
                url = 'http://parking.metatype.cn/wechat/parkin/' + str(
                    r.parkinglot.id) + '/' + str(r.id) + '/'
            elif t == 2:
                url = 'http://parking.metatype.cn/wechat/parkout/' + str(
                    r.parkinglot.id) + '/' + str(r.id) + '/'
            elif t == 0:
                url = 'http://parking.metatype.cn/wechat/parkout/' + str(
                    r.parkinglot.id) + '/'
            c_name = 'code_' + str(r.parkinglot.id) + '_' + str(
                r.id) + '_' + str(t)
            r.code = make_qrcode(url, c_name + '.png')
            r.save()
        elif action == 'update':
            id = request.POST.get('id', '')
            r = Gate.objects.filter(id=id).first()
            park = request.POST.get('parkinglot')
            if park:
                r.parkinglot = ParkingLot.objects.filter(id=int(park)).first()
                r.save()

            _save_attr_(r, request)

        elif action == 'delete':
            ids = request.POST.getlist('ids', '')
            u = Gate.objects.filter(id__in=ids).all()
            for item in u:
                item.status = -1
                item.save()
        elif action == 'select':
            use_type = request.POST.get('use_type')
            if use_type:
                gate = Gate.objects.filter(use_type=use_type, status=0).all()
                ctx['tip'] = use_type

        elif action == 'barcode':
            pass

    ctx['parkinglots'] = ParkingLot.objects.filter(status=0).all()
    ctx['gates'] = ctx['objects'] = gate

    return (ctx, 'gate.html')
Exemplo n.º 4
0
def coupon_type(request):
    ''' 优惠券类型设置 '''

    ctx = {}

    objects = Coupons.objects.all().filter(is_delete=0)

    if request.method == 'POST':
        action = request.POST.get('action', '')

        if action == 'add':

            r = Coupons()
            _save_attr_(r, request)

        elif action == 'update':
            id = request.POST.get('id', '')
            if id:
                r = Coupons.objects.filter(id=int(id)).first()
                _save_attr_(r, request)

        elif action == 'search':
            ctx['name'] = name = request.POST.get('name', '')
            ctx['type'] = type = request.POST.get('type', '')

            if type:
                objects = objects.filter(type=int(type))
            if name:
                objects = objects.filter(name__contains=name)

        elif action == 'delete':
            ids = request.POST.getlist('ids', '')
            Coupons.objects.filter(id__in=ids).update(is_delete=1)

    ctx['objects'] = objects.order_by('-update_time')

    return (ctx, 'coupon_type.html')
Exemplo n.º 5
0
def company(request):
    ctx = {}
    companys = Company.objects.exclude(status=-1).all()

    def operate_in_batch(status, request):
        ids = request.POST.getlist('ids', '')
        u = Company.objects.filter(id__in=ids).all()
        for item in u:
            item.status = status
            item.save()

    def op(r, request):
        parking = request.POST.get('suit')
        if parking:
            r.parkinglot = ParkingLot.objects.filter(id=int(parking)).first()
            r.save()

    if request.method == 'POST':
        action = request.POST.get('action', '')
        if action == 'add':
            r = Company()
            _save_attr_(r, request)
            op(r, request)

        elif action == 'update':
            id = request.POST.get('id', '')
            r = Company.objects.filter(id=id).first()
            _save_attr_(r, request)
            op(r, request)

        elif action == 'delete':
            operate_in_batch(-1, request)

        elif action == 'inact':
            operate_in_batch(0, request)

        elif action == 'act':
            operate_in_batch(1, request)

        elif action == 'refund':
            r = Refund()
            _save_attr_(r, request)

        elif action == 'validate':
            account = request.POST.get('account', '')
            id = request.POST.get('id', '')
            if id:
                r = Company.objects.filter(account=account.strip()).exclude(
                    id=int(id))
            else:
                r = Company.objects.filter(account=account.strip())
            if r.exists():
                return JsonResponse({'valid': False})

            return JsonResponse({'valid': True})

        elif action == 'export':
            if companys:
                e = xlwt.Workbook(encoding='utf-8')
                w = e.add_sheet(u'商家管理')
                w.write(0, 0, '商家名称')
                w.write(0, 1, '商家地址')
                w.write(0, 2, '登陆账号')
                w.write(0, 3, '联系人姓名')
                w.write(0, 4, '联系人电话')
                w.write(0, 5, '余额')
                w.write(0, 6, '时长')
                w.write(0, 7, '限制类型')
                w.write(0, 8, '限制规则')
                w.write(0, 9, '限制张数')
                w.write(0, 10, '商家描述')
                w.write(0, 11, '所属停车场')
                w.write(0, 12, '状态')
                row = 1
                for i in companys:
                    w.write(row, 0, i.name)
                    w.write(row, 1, i.address)
                    w.write(row, 2, i.account)
                    w.write(row, 3, i.owner)
                    w.write(row, 4, i.phone)
                    w.write(row, 5, i.balance)
                    w.write(row, 6, i.duration)
                    w.write(row, 7, i.get_type_display())
                    w.write(row, 8, i.rule)
                    w.write(row, 9, i.amount)
                    w.write(row, 10, i.descript)
                    w.write(row, 11, i.parkinglot.name if i.parkinglot else '')
                    w.write(row, 12, i.get_status_display())
                    row += 1
                output = io.BytesIO()
                e.save(output)
                # 重新定位到开始
                output.seek(0)
                response = HttpResponse(
                    content_type='application/vnd.ms-excel')
                response[
                    'Content-Disposition'] = 'attachment;filename=company.xls'
                response.write(output.getvalue())
                return response

        elif action == 'select':
            name = request.POST.get('busi_name', '')
            ass_name = request.POST.get('associate_name', '')
            if name:
                name = int(name)
                companys = companys.filter(id=name).all()
                ctx['b'] = name
            if ass_name:
                companys = companys.filter(owner__icontains=ass_name).all()
                ctx['a'] = ass_name

    ctx['objects'] = ctx['company'] = companys
    ctx['parkinglot'] = ParkingLot.objects.filter(status=0)

    return (ctx, 'company.html')
Exemplo n.º 6
0
def groundsensor(request):
    '''地感管理'''

    ctx = {}

    groundsensors = GroundSensor.objects.select_related('parkinglot','gate').all()

    if request.method == 'POST':
        action = request.POST.get('action', '')
        if action == 'add':
            r = GroundSensor()            
            _save_attr_(r, request)

            parkinglot_id = request.POST.get('parkinglot_id', '')
            if parkinglot_id:
                p = ParkingLot.objects.filter(id=parkinglot_id).first()
                if p: 
                    r.parkinglot = p
                    r.save()

            gate_id = request.POST.get('gate_id', '')
            if gate_id:
                p = Gate.objects.filter(id=gate_id).first()
                if p: 
                    r.gate = p
                    r.save()

        elif action == 'update':
            id = request.POST.get('id', '')

            r = GroundSensor.objects.filter(id=id).first()
            _save_attr_(r, request)
            parkinglot_id = request.POST.get('parkinglot_id', '')
            if parkinglot_id:
                p = ParkingLot.objects.filter(id=parkinglot_id).first()
                if p: 
                    r.parkinglot = p
                    r.save()
            gate_id = request.POST.get('gate_id', '')
            if gate_id:
                p = Gate.objects.filter(id=gate_id).first()
                if p: 
                    r.gate = p
                    r.save()
        elif action == 'search':
            ctx['brand'] = brand = request.POST.get('brand', '')
            ctx['manufacturer'] = manufacturer = request.POST.get('manufacturer', '')
           
            gate_id = request.POST.get('gate_id', '')
            parkinglot_id = request.POST.get('parkinglot_id', '')

            if parkinglot_id:
                ctx['parkinglot_id'] = int(parkinglot_id)
                groundSensors = groundsensors.filter(parkinglot_id=int(parkinglot_id))
                ctx['gates'] = Gate.objects.filter(parkinglot_id=int(parkinglot_id))
            if gate_id:
                ctx['gate_id'] = int(gate_id)
                groundsensors = groundsensors.filter(gate_id=int(gate_id))
            if brand:
                groundsensors = groundsensors.filter(brand=brand)
            if manufacturer:
                groundsensors = groundsensors.filter(manufacturer=manufacturer)
                
        elif action == 'delete':
            ids = request.POST.getlist('ids', '')
            GroundSensor.objects.filter(id__in=ids).delete()

        elif action == 'validate':
            number = request.POST.get('number', '')
            id = request.POST.get('id', '')

            r = GroundSensor.objects.filter(number=number.strip())
            if r.exists():
                if id:
                    if r.first().id != int(id):
                        return JsonResponse({'valid': False})
                else:
                    return JsonResponse({'valid': False})

            return JsonResponse({'valid': True})

        elif action == 'get_gate':
            id = request.POST.get('id', '')
            gates = Gate.objects.filter(parkinglot_id=id)
            gates = [{'name': _.name, 'id':_.id} for _ in gates]
            return JsonResponse({'success':True, 'result': gates})

    ctx['objects'] = groundsensors.order_by('-buy_time')
    ctx['parkinglots'] = ParkingLot.objects.filter(status=0).all()
    print(ctx['parkinglots'])
    all_gates = {}

    gates = Gate.objects.select_related('parkinglot').filter(parkinglot__status=0).order_by('parkinglot')
    for i in gates:
        if i.parkinglot.id in all_gates:
            all_gates[i.parkinglot.id].append({'gate_id': i.id, 'gate_name': i.monitor})
        else:
            all_gates[i.parkinglot.id] = [{'gate_id': i.id, 'gate_name': i.monitor}]
    ctx['all_gates'] = all_gates

    return (ctx, 'groundsensor.html')
Exemplo n.º 7
0
def place(request):

    ctx = {}
    place = Place.objects.filter(status=0).all()
    zone = Zone.objects.filter(status=0).all()

    if request.method == 'POST':
        action = request.POST.get('action', '')
        if action == 'add':

            r = Place()

            park = request.POST.get('parkinglot')
            if park:
                r.parkinglot = ParkingLot.objects.filter(id=int(park)).first()
                r.save()

            zon = request.POST.get('zone')
            if zon:
                r.zone = Zone.objects.filter(id=int(zon)).first()
                r.save()

            _save_attr_(r, request)
        elif action == 'update':
            id = request.POST.get('id', '')
            r = Place.objects.filter(id=id).first()

            park = request.POST.get('parkinglot')
            if park:
                r.parkinglot = ParkingLot.objects.filter(id=int(park)).first()
                r.save()

            zon = request.POST.get('zone')
            if zon:
                r.zone = Zone.objects.filter(id=int(zon)).first()
                r.save()

            _save_attr_(r, request)

        elif action == 'delete':
            ids = request.POST.getlist('ids', '')
            u = Place.objects.filter(id__in=ids).all()
            for item in u:
                item.status = -1
                item.save()

        elif action == 'select':
            use_type = request.POST.get('use_type')
            car_type = request.POST.get('car_type')
            park = request.POST.get('parkinglot')
            zon = request.POST.get('zone')

            if use_type != '':
                place = place.filter(use_type=use_type)
            if car_type != '':
                place = place.filter(car_type=car_type)
            if park != '':
                place = place.filter(parkinglot__id=park)
            if zon != '':
                place = place.filter(zone__id=zon)

            ctx['u'] = use_type
            ctx['c'] = car_type
            ctx['p'] = park

        elif action == 'getZone':

            id = request.POST.get('id')
            zones = zone.filter(parkinglot__id=id).all()
            tmp = []

            for i in zones:
                d = {'id': i.id, 'name': i.zone_name}
                tmp.append(d)

            return JsonResponse({'data': tmp})

    ctx['zones'] = zone
    ctx['parkinglots'] = ParkingLot.objects.filter(status=0).all()
    ctx['place'] = ctx['objects'] = place

    return (ctx, 'place.html')
Exemplo n.º 8
0
def worker(request):
    ''' 车场员工 '''

    ctx = {}

    workers = Worker.objects.all().filter(is_delete=0)

    if request.method == 'POST':
        action = request.POST.get('action', '')
        if action == 'add':
            r = Worker()
            _save_attr_(r, request)

            parkinglot_id = request.POST.get('parkinglot_id', '')
            if parkinglot_id:
                p = ParkingLot.objects.filter(id=parkinglot_id).first()
                if p:
                    r.parkinglot = p
                    r.save()

        elif action == 'update':
            id = request.POST.get('id', '')

            r = Worker.objects.filter(id=id).first()
            _save_attr_(r, request)
            parkinglot_id = request.POST.get('parkinglot_id', '')
            if parkinglot_id:
                p = ParkingLot.objects.filter(id=parkinglot_id).first()
                if p:
                    r.parkinglot = p
                    r.save()

        elif action == 'search':
            ctx['name'] = name = request.POST.get('name', '')
            ctx['number'] = number = request.POST.get('number', '')
            forbidden = request.POST.get('forbidden', '')
            parkinglot = request.POST.get('parkinglot', '')

            if forbidden:
                ctx['forbidden'] = int(forbidden)
                workers = workers.filter(forbidden=int(forbidden))
            if parkinglot:
                ctx['parkinglot'] = int(parkinglot)
                workers = workers.filter(parkinglot_id=int(parkinglot))
            if number:
                workers = workers.filter(number__contains=number)
            if name:
                workers = workers.filter(name__contains=name)

        elif action == 'delete':
            ids = request.POST.getlist('ids', '')
            Worker.objects.filter(id__in=ids).update(is_delete=1)

        elif action == 'forbidden':
            id = request.POST.get('id', '')
            Worker.objects.filter(id=id).update(forbidden=1)
            return JsonResponse({'success': True})

        elif action == 'awaken':
            id = request.POST.get('id', '')
            Worker.objects.filter(id=id).update(forbidden=0)
            return JsonResponse({'success': True})

        elif action == 'validate':
            number = request.POST.get('number', '')
            id = request.POST.get('id', '')

            r = Worker.objects.filter(number=number.strip())
            if r.exists():
                if id:
                    if r.first().id != int(id):
                        return JsonResponse({'valid': False})
                else:
                    return JsonResponse({'valid': False})

            return JsonResponse({'valid': True})

    ctx['objects'] = workers
    ctx['parkinglots'] = ParkingLot.objects.filter(status=0).all()

    return (ctx, 'worker.html')
Exemplo n.º 9
0
def card_type(request):
    '''卡片类型设置'''
    import json

    def chec(str):
        if str.replace('.5', ':30') == str:
            return str + ':00'
        else:
            return '2'

    def tim():
        def rm_invalid(t):
            if t % 1 == 0:
                return int(t)
            return t

        list = []
        list1 = []
        for i in range(24):
            k = rm_invalid((i) / 2)
            k1 = rm_invalid((i + 24) / 2)
            c = {'k': k, 'v': chec(str(k))}
            c1 = {'k': k1, 'v': chec(str(k1))}

            list.append(c)
            list1.append(c1)

        return list, list1

    def cut_time(time):
        tim_list = []
        guide = []
        start = 0
        for i, j in enumerate(time):
            # print(i)
            if j == ',':
                tip = False
                tim_list.append(time[start:i])
                start = i + 1

        tim_list.append(time[start:])

        return get_start_and_end(tim_list)

    def get_start_and_end(list):
        def get(time):
            for i, j in enumerate(time):
                if j == '-':
                    return time[0:i], time[i + 2:]

        tmp = []
        for m in list:
            start, end = get(m)
            c = {"start": start, "end": end}
            tmp.append(c)
        return tmp

    def save_time(obj, request):
        fields = obj._meta.fields

        for field in fields:
            field_name = field.name
            if str(type(
                    field)) == "<class 'django.db.models.fields.TextField'>":
                value = request.POST.get(field_name, '')
                if value.strip() != '':
                    obj.__setattr__(field_name, cut_time(value))
        obj.save()

    def to_dobule(str):
        t = json.loads(str.replace("'", '"')) if str else ''

        return t

    def decode_str(obj):
        tmp = []
        for i in obj:
            c = {
                'work': to_dobule(i.work),
                'relax': to_dobule(i.relax),
                'free': to_dobule(i.free),
                'free_tu': to_dobule(i.free_tu),
                'free_we': to_dobule(i.free_we),
                'free_th': to_dobule(i.free_th),
                'free_fr': to_dobule(i.free_fr),
                'free_sa': to_dobule(i.free_sa),
                'free_su': to_dobule(i.free_su),
                'id': i.id,
                'diff_type': i.diff_type,
                'name': i.name
            }
            tmp.append(c)
        return tmp

    ctx = {}
    cardtype = CardType.objects.filter(status=0).all()
    t = 2
    if request.method == 'POST':
        action = request.POST.get('action', '')
        if action == 'add':
            r = CardType()
            _save_attr_(r, request)
            save_time(r, request)
            t = int(request.POST.get('diff_type'))

        elif action == 'update':
            id = request.POST.get('id', '')
            r = CardType.objects.filter(id=id).first()
            _save_attr_(r, request)
            save_time(r, request)
            t = int(request.POST.get('diff'))

        elif action == 'delete':
            ids = request.POST.getlist('ids', '')
            t = request.POST.get('type')
            u = CardType.objects.filter(id__in=ids).all()

            for item in u:
                item.status = -1
                item.save()
        elif action == 'select':
            t = request.POST.get('type')

        elif action == 'export':
            w, e = export_excel(cardtype[0], u'卡片类型管理')
            row = 1
            s = ''
            for i in cardtype:
                w.write(row, 0, i.name)
                w.write(row, 1, i.work)
                w.write(row, 2, i.relax)
                w.write(row, 3, i.get_diff_type_display())
                w.write(row, 4, i.free)
                w.write(row, 5, i.free_tu)
                w.write(row, 6, i.free_we)
                w.write(row, 7, i.free_th)
                w.write(row, 8, i.free_fr)
                w.write(row, 9, i.free_sa)
                w.write(row, 10, i.free_su)
                row += 1
            output = io.BytesIO()
            e.save(output)
            # 重新定位到开始
            output.seek(0)
            response = HttpResponse(content_type='application/vnd.ms-excel')
            response[
                'Content-Disposition'] = 'attachment;filename=cardtype.xls'
            response.write(output.getvalue())
            return response

    if t != 2:
        cardtype = cardtype.filter(diff_type=int(t))
        ctx['type'] = int(t)

    ctx['num'], ctx['num1'] = tim()
    ctx['parkinglots'] = ParkingLot.objects.filter(status=0).all()
    ctx['cardtype'] = ctx['objects'] = decode_str(cardtype)
    return (ctx, 'card_type.html')
Exemplo n.º 10
0
def card(request):
    '''卡片管理'''

    ctx = {}
    cards = Card.objects.filter(status=0).all()

    def correct_obj(request, r):
        # owner_id = request.POST.get('owner', '')
        card_id = request.POST.get('my_card', '')
        suit = request.POST.getlist('suit', [])
        # if owner_id:
        #     p = AdminUser.objects.filter(id=owner_id).first()
        #     if p:
        #         r.owner = p

        if card_id:
            p = CardType.objects.filter(id=card_id).first()
            if p:
                r.my_card = p
        r.save()
        if suit:
            for i in suit:
                if i not in r.suit.all():
                    r.suit.add(ParkingLot.objects.filter(id=int(i)).first())
            r.save()

    if request.method == 'POST':
        action = request.POST.get('action', '')
        if action == 'add':
            r = Card()
            correct_obj(request, r)
            _save_attr_(r, request)

        elif action == 'update':
            id = request.POST.get('id', '')
            r = Card.objects.filter(id=id).first()
            correct_obj(request, r)
            _save_attr_(r, request)

        elif action == 'delete':
            ids = request.POST.getlist('ids', '')
            u = Card.objects.filter(id__in=ids).all()
            for item in u:
                item.status = -1
                item.save()

        elif action == 'validate':
            owner = request.POST.get('owner', '')
            id = request.POST.get('id', '')
            if id:
                r = Card.objects.filter(owner=owner.strip()).exclude(
                    id=int(id), status=0)
            else:
                r = Card.objects.filter(owner=owner.strip(), status=0)

            if r.exists():
                return JsonResponse({'valid': False})

            return JsonResponse({'valid': True})

        elif action == 'export':
            w, e = export_excel(cards[0], u'开卡管理')
            row = 1
            s = ''
            for i in cards:
                w.write(row, 0, i.owner)
                w.write(row, 1, i.my_card.name)
                w.write(row, 2, i.valid_start)
                w.write(row, 3, i.valid_end)
                for j in i.suit.all():
                    s += j.name + '  '
                w.write(row, 4, s)
                row += 1
            output = io.BytesIO()
            e.save(output)
            # 重新定位到开始
            output.seek(0)
            response = HttpResponse(content_type='application/vnd.ms-excel')
            response['Content-Disposition'] = 'attachment;filename=card.xls'
            response.write(output.getvalue())
            return response

    ctx['users'] = AdminUser.objects.all()
    ctx['cardtypes'] = CardType.objects.filter(status=0).all()
    ctx['cards'] = ctx['objects'] = cards
    ctx['parkinglot'] = ParkingLot.objects.all()
    return (ctx, 'card.html')
Exemplo n.º 11
0
def coupon(request):
    '''优惠券管理'''
    def code(id):
        from meta.qrcode import make_qrcode
        import datetime

        url = 'http://parking.metatype.cn/business/ticket/' + str(id) + '/'
        now = datetime.datetime.now()
        name = str(now.year) + '' + str(
            now.month) + '' + str('now.day') + '' + str(now.hour) + '' + str(
                now.minute) + '' + str(now.second)
        return make_qrcode(url, name + '.png')

    ctx = {}
    objects = TicketRecord.objects.select_related('parkinglot', 'company',
                                                  'coupons').all()

    if request.method == 'POST':
        action = request.POST.get('action', '')
        if action == 'add':
            id = request.POST.get('id', '')
            if id:
                r = TicketRecord.objects.filter(id=id).first()
            else:
                r = TicketRecord()

            _save_attr_(r, request)

            company_id = request.POST.get('company_id', '')
            parkinglot_id = request.POST.get('parkinglot_id', '')
            if parkinglot_id:
                r.parkinglot_id = int(parkinglot_id)
            if company_id:
                r.company_id = int(company_id)

            ticket_type = request.POST.get('ticket_type', '')
            ticket_id = request.POST.get('ticket_id', '')
            if ticket_type and ticket_id:
                r.coupons_id = int(ticket_id)
            r.coucode = code(r.id)
            r.qrrandom = ran()
            r.save()

        elif action == 'update':
            id = request.POST.get('id', '')

            r = Camera.objects.filter(id=id).first()
            _save_attr_(r, request)
            parkinglot_id = request.POST.get('parkinglot_id', '')
            if parkinglot_id:
                p = ParkingLot.objects.filter(id=parkinglot_id).first()
                if p:
                    r.parkinglot = p
                    r.save()
            gate_id = request.POST.get('gate_id', '')
            if gate_id:
                p = Gate.objects.filter(id=gate_id).first()
                if p:
                    r.gate = p
                    r.save()
        elif action == 'search':
            ticket_type = request.POST.get('ticket_type', '')
            ticket_id = request.POST.get('ticket_id', '')

            company_id = request.POST.get('company_id', '')
            parkinglot_id = request.POST.get('parkinglot_id', '')

            if parkinglot_id:
                ctx['parkinglot_id'] = int(parkinglot_id)
                objects = objects.filter(parkinglot_id=int(parkinglot_id))
                # ctx['gates'] = objects.filter(parkinglot_id=int(parkinglot_id))
            if company_id:
                ctx['company_id'] = int(company_id)
                objects = objects.filter(company_id=int(company_id))
            if ticket_type:
                ctx['ticket_type'] = int(ticket_type)
                objects = objects.filter(ticket_type=int(ticket_type))

                ctx['tickets'] = Coupons.objects.filter(is_delete=0,
                                                        type=int(ticket_type))

            if ticket_id:
                ctx['ticket_id'] = int(ticket_id)
                objects = objects.filter(coupons_id=int(ticket_id))

        elif action == 'delete':
            ids = request.POST.getlist('ids', '')
            TicketRecord.objects.filter(
                id__in=ids).delete()  #update(is_delete=1)

    ctx['objects'] = objects.order_by('-buy_time')
    ctx['parkinglots'] = ParkingLot.objects.filter(status=0)
    c = Company.objects.select_related('parkinglot').filter(status=1)
    companies = {}

    for i in c:
        if i.parkinglot.id in companies:
            companies[i.parkinglot.id].append({'id': i.id, 'name': i.name})
        else:
            companies[i.parkinglot.id] = [{'id': i.id, 'name': i.name}]
    print(c)
    ctx['all_companies'] = companies

    all_tickets = {}
    all_tickets['0'] = [{
        'id': _.id,
        'name': _.name
    } for _ in Coupons.objects.filter(is_delete=0, type=0).order_by(
        '-update_time')]
    all_tickets['1'] = [{
        'id': _.id,
        'name': _.name
    } for _ in Coupons.objects.filter(is_delete=0, type=1).order_by(
        '-update_time')]
    all_tickets['2'] = [{
        'id': _.id,
        'name': _.name
    } for _ in Coupons.objects.filter(is_delete=0, type=2).order_by(
        '-update_time')]
    all_tickets['3'] = [{
        'id': _.id,
        'name': _.name
    } for _ in Coupons.objects.filter(is_delete=0, type=3).order_by(
        '-update_time')]

    ctx['all_tickets'] = all_tickets

    return (ctx, 'coupon.html')