def find_discountable_entity( input, ) -> Union[models.Training, models.TicketType, GenericError]: if 'ticket_type_id' in input and 'training_id' in input: return GenericError( 'Только один из параметров ticket_type_id и training_id должен быть выбран' ) if input.get('ticket_type_id'): ticket_type_id = input['ticket_type_id'] try: entity = models.TicketType.objects.get(uuid=ticket_type_id) except models.TicketType.DoesNotExist: return GenericError('Тип билета не найден') elif input.get('training_id'): training_id = input['training_id'] try: entity = models.Training.objects.get(pk=training_id) except models.Training.DoesNotExist: return GenericError('Тренинг не найден') else: return GenericError( 'Один из параметров ticket_type_id и training_id должен быть выбран' ) return entity
def resolve(self, _, info, input): if not info.context.user.is_authenticated: return GenericError("Пользователь не авторизован") try: comment = self.model.objects.get(id=input['id']) except self.model.DoesNotExist: return GenericError("Комментарий не найден") if info.context.user.pk != comment.author.pk: return GenericError("Нельзя удалять чужие комментарии") comment.delete() return self.Ok()
def resolve(self, _, info, input): # TODO - move to model (shift, _) = models.Shift.objects.get_or_create( date=input['date'], shift=input['shift'], ) shift.is_night = input.get('is_night', False) if 'watchman_id' in input: try: shift.watchman = models.Watchman.objects.get( pk=input['watchman_id']) except models.Watchman.DoesNotExist: return GenericError("Watchman does not exist") else: shift.watchman = None try: shift.full_clean() except ValidationError as e: return BoxedError(e) shift.save() def on_commit(): # can't broadcast empty dict, channels-redis complains channels.watchmen_updates_group.broadcast({'updated': True}) transaction.on_commit(on_commit) return shift
def resolve(self, _, info, input): if not info.context.user.is_authenticated: return GenericError("Пользователь не авторизован") try: comment = self.model.objects.get(id=input['id']) except self.models.DoesNotExist: return GenericError("Комментарий не найден") if info.context.user.pk != comment.author.pk: return GenericError("Нельзя редактировать чужие комментарии") comment.text = input['text'] comment.full_clean() comment.save() return comment
def resolve(self, _, info): digest = models.WeeklyDigest.objects.current_digest() try: digest.send_mailchimp() except PublicError as e: return GenericError(e.message) return digest
def resolve(self, _, info, input): try: return models.Chat.objects.create_by_invite_link(input['invite_link']) except models.Chat.objects.BadInviteLinkFormatError: return GenericError( "invite_link должен быть в форме https://t.me/joinchat/xxxxx" ) except ValidationError as e: return BoxedError(e)
def resolve(self, _, info, input): id = input['id'] try: payment = models.Payment.objects.get(pk=id) except models.Payment.DoesNotExist: return GenericError("Платёж не найден") payment.cancel() return payment
def resolve(self, obj, info, input): try: return self.smart_resolve(obj, info, input) except Exception as e: if isinstance(e, PublicError): return GenericError(e.message) if isinstance(e, ValidationError): return BoxedError(e) raise Exception("Internal error")
def resolve(self, _, info, input): maybe_entity = find_discountable_entity(input) if isinstance(maybe_entity, GenericError): return maybe_entity entity = maybe_entity try: with transaction.atomic(): promocode = models.Promocode.objects.create( code=input['code'], discount=input['discount'], uses_max=input.get('uses_max'), ) entity.promocodes.add(promocode) entity.save() except ValidationError as e: return BoxedError(e) except django.db.utils.IntegrityError as e: if e.args[0] == 1062: return GenericError("Промокод уже существует") raise e return promocode
def resolve(self, _, info, input): ticket_type_id = input['ticket_type_id'] try: ticket_type = models.TicketType.objects.get(uuid=ticket_type_id) except models.TicketType.DoesNotExist: return GenericError('Тип билета не найден') try: order = models.Order.objects.create_order( ticket_type=ticket_type, email=input['email'], first_name=input.get('first_name', ''), last_name=input.get('last_name', ''), city=input.get('city', ''), promocode=input.get('promocode', ''), payer_email=input.get('payer', {}).get('email', ''), payer_first_name=input.get('payer', {}).get('first_name', ''), payer_last_name=input.get('payer', {}).get('last_name', ''), ) order.full_clean() # extra precaution, make sure that order is ok except ValidationError as e: return BoxedError(e) return order
def resolve(self, _, info, input): page = self.model.objects.import_page(input['page_id']) if not page: return GenericError( "Страница не найдена или не должна быть импортирована") return page