def rest_new_card(request): response = {} if request.method == 'PUT': user = UserCustom.objects.get(user_id__exact=request.user.pk) card_new_data = json.loads(request.body.decode()) card = Card() card.org = user.org card.reg_date = datetime.now() card.changes_date = datetime.now() card.holder_name = card_new_data['holder_name'] card.code = card_new_data['code'] card.holder_phone = card_new_data['holder_phone'] card.sex = card_new_data['sex'] card.type = card_new_data['type'] try: card.save() response['status'] = 'success' response['message'] = 'Новая карта успешно заведена!' return JsonResponse(response, safe=False) except IntegrityError as err: response['status'] = 'error' response['message'] = 'Карта с таким кодом уже существует!' return JsonResponse(response, safe=False)
if nrow == 0: continue row = sheet.row_values(nrow) name = row[0] name = re.sub(r'[0-9]+', '', name) name = name.strip() name = name.title() card.holder_name = name code = row[1] card.code = str(code) reg_date = row[2] try: card.reg_date = datetime.strptime(reg_date, '%d.%m.%Y') except: card.reg_date = None sex = row[3] if sex == 'муж': card.sex = 'm' elif sex == 'жен': card.sex = 'f' else: card.sex = 'm' fav_date = row[4] try: card.fav_date = datetime.strptime(fav_date, '%d.%m.%Y') except:
def maintenance(request): response = {} if request.method == "POST": post = request.POST user = UserCustom.objects.get(user_id__exact=request.user.pk) if "cmd" in post: if post["cmd"] == "save": # сохранение карты if "data" in post: data = json.loads(post["data"]) try: form = CardForm(data) if form.is_valid(): try: #if "collision" in post: #if post['collision'] card = Card.objects.get( code__exact=form.cleaned_data['code'], org_id__exact=user.org.pk) except ObjectDoesNotExist as e: card = Card() card.code = form.cleaned_data['code'] card.holder_name = form.cleaned_data['holder_name'] card.holder_phone = form.cleaned_data[ 'holder_phone'] card.bonus = form.cleaned_data['bonus'] card.discount = form.cleaned_data['discount'] card.accumulation = form.cleaned_data[ 'accumulation'] card.type = form.cleaned_data['type'] card.changes_date = datetime.now().date() if not card.reg_date: card.reg_date = datetime.now().date() card.last_transaction_date = datetime.now( ).date() card.org = user.org card.save() response = {"result": "ok"} return HttpResponse( json.dumps(response), content_type="application/json") except: response = {"result": "error"} return HttpResponse(json.dumps(response), content_type="application/json") if post["cmd"] == "get": # получение данных по карте if "data" in post: data = json.loads(post["data"]) try: card = Card.objects.filter( code__exact=data["code"], org_id__exact=user.org.pk).get() date_func = lambda a: '' if a is None else a.strftime( '%Y-%m-%d') data = { "code": card.code, "holder_name": card.holder_name, "holder_phone": card.holder_phone, "accumulation": card.accumulation, "bonus": card.bonus, "discount": card.discount, "type": card.type, "reg_date": date_func(card.reg_date), "changes_date": date_func(card.changes_date), "last_transaction_date": date_func(card.last_transaction_date) } response = {"result": "ok", "data": data} response = json.dumps(response) return HttpResponse(response, content_type="application/json") except Exception as err: response = {"result": "error", "msg": err} return HttpResponse(json.dumps(response), content_type="application/json") if post["cmd"] == "delete": # получение данных по карте if "data" in post: data = json.loads(post["data"]) try: for code in data: card = Card.objects.filter( code__exact=code, org_id__exact=user.org.pk).get() card.deleted = 'y' card.save() response = {"result": "ok", "data": data} response = json.dumps(response) return HttpResponse(response, content_type="application/json") except Exception as err: response = {"result": "error", "msg": err} return HttpResponse(json.dumps(response), content_type="application/json") if post["cmd"] == "restore": # получение данных по карте if "data" in post: data = json.loads(post["data"]) try: for code in data: card = Card.objects.filter( code__exact=code, org_id__exact=user.org.pk).get() card.deleted = 'n' card.save() response = {"result": "ok", "data": data} response = json.dumps(response) return HttpResponse(response, content_type="application/json") except Exception as err: response = {"result": "error", "msg": err} return HttpResponse(json.dumps(response), content_type="application/json")
def mass_add(request): if request.method == "POST": data = request.POST user = UserCustom.objects.get(user_id__exact=request.user.pk) try: form = MassCardForm(data) if form.is_valid(): start = form.cleaned_data['code_start'] end = form.cleaned_data['code_end'] length = form.cleaned_data['code_length'] pool = [str(x).zfill(length) for x in range(start, end + 1, 1)] for code in pool: try: card = Card.objects.get(code__exact=code, org_id__exact=user.org.pk) exist = True except: card = Card() exist = False card.org = user.org if not exist: card.code = code card.org = user.org card.type = form.cleaned_data['type'] card.discount = form.cleaned_data['discount'] card.bonus = form.cleaned_data['bonus'] card.accumulation = form.cleaned_data['accumulation'] card.reg_date = datetime.now().date() card.last_transaction_date = datetime.now().date() else: if form.cleaned_data['doubles'] == 'rewrite': card.type = form.cleaned_data['type'] card.discount = form.cleaned_data['discount'] card.bonus = form.cleaned_data['bonus'] card.accumulation = form.cleaned_data[ 'accumulation'] card.fio = '' card.deleted = 'n' card.reg_date = datetime.now().date() card.last_transaction_date = datetime.now().date() card.changes_date = None elif form.cleaned_data['doubles'] == 'append': card.type = form.cleaned_data['type'] card.discount = form.cleaned_data['discount'] card.bonus = form.cleaned_data['bonus'] card.accumulation = form.cleaned_data[ 'accumulation'] card.deleted = 'n' card.reg_date = datetime.now().date() card.last_transaction_date = datetime.now().date() card.changes_date = None elif form.cleaned_data['doubles'] == 'ignore': continue card.save() return HttpResponseRedirect('/cards/') except Exception as e: response = {"result": e} return HttpResponse(response, content_type="application/json") response = {"result": "error"} return HttpResponse(json.dumps(response), content_type="application/json")