Ejemplo n.º 1
0
    def update_card(self, staged_card: StagedCard) -> Card:
        """
        Updates or creates the Card object for the given StagedCard
        :param staged_card: The staging information for this card
        :return: The updated or created Card
        """
        try:
            if staged_card.is_token:
                card = Card.objects.get(name=staged_card.get_name(),
                                        scryfall_oracle_id=staged_card.get_scryfall_oracle_id(),
                                        is_token=True,
                                        side=staged_card.get_side())
            else:
                card = Card.objects.get(name=staged_card.get_name(), is_token=False)
                if not self.force_update and staged_card.get_name() in self.updated_cards:
                    logger.info('%s has already been updated', card)
                    self.increment_ignores('Card')
                    return card

            logger.info('Updating existing card %s', card)
            self.increment_updated('Card')
        except Card.DoesNotExist:
            card = Card(name=staged_card.get_name())
            logger.info('Creating new card %s', card)
            self.increment_created('Card')

        self.updated_cards.append(staged_card.get_name())

        card.cost = staged_card.get_mana_cost()
        card.cmc = staged_card.get_cmc()
        card.colour_flags = staged_card.get_colour()
        card.colour_identity_flags = staged_card.get_colour_identity()
        card.colour_count = staged_card.get_colour_count()
        card.colour_sort_key = staged_card.get_colour_sort_key()
        card.colour_weight = staged_card.get_colour_weight()

        card.power = staged_card.get_power()
        card.toughness = staged_card.get_toughness()
        card.num_power = staged_card.get_num_power()
        card.num_toughness = staged_card.get_num_toughness()
        card.loyalty = staged_card.get_loyalty()
        card.num_loyalty = staged_card.get_num_loyalty()

        card.type = staged_card.get_types()
        card.subtype = staged_card.get_subtypes()
        card.original_type = staged_card.get_original_type()

        card.rules_text = staged_card.get_rules_text()
        card.original_text = staged_card.get_original_text()
        card.layout = staged_card.get_layout()
        card.side = staged_card.get_side()
        card.scryfall_oracle_id = staged_card.get_scryfall_oracle_id()
        card.is_reserved = staged_card.is_reserved()
        card.is_token = staged_card.is_token

        card.full_clean()
        card.save()
        return card
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
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")
Ejemplo n.º 4
0
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")