def buy_item(event: HTTPEvent): category = event.params['category'] try: release = int(event.params['release']) except ValueError: return NotFoundException( f"Unknown release {event.params['release']}, it should be an int") try: id_ = int(event.params['id']) except ValueError: raise NotFoundException( f"Unknown id {event.params['id']}, it should be an int") area = event.params['area'] if area not in VALID_AREAS: raise NotFoundException(f"Area {area} does not exist") amount = event.json.get('amount', 1) if type(amount) is not int: raise InvalidException(f"The amount to be bought must be an integer") if amount < 1: raise InvalidException(f"The amount must be one or more") try: result = BeneficiariesService.buy_item(event.authorizer, area, category, release, id_, amount) except BeneficiariesService.exceptions().ConditionalCheckFailedException: raise ForbiddenException( f"You don't have enough {area} score to buy this item") if not result: return NotFoundException(f"Item not found") return JSONResponse(result)
def get(cls, stage: str, area: str, line: int, sub_line: int): objectives = cls.get_stage_objectives(stage) if area not in objectives: raise NotFoundException(f"Area {area} not found") objectives = objectives[area] line -= 1 if line < 0 or line >= len(objectives): raise NotFoundException( f"Line {line + 1} on area {area} not found") objectives = objectives[line] sub_line -= 1 if sub_line < 0 or sub_line >= len(objectives): raise NotFoundException( f"Sub-line {line + 1}.{sub_line + 1} on area {area} not found") return objectives[sub_line]
def get_random(cls, category: RewardType, release: int, rarity: RewardRarity): reward = Reward.factory(category, rarity) if reward is not None: return reward release = int(release) if release < 1: raise InvalidException('Release must be positive and non-zero') top = abs(release * REWARDS_PER_RELEASE - 1) if rarity == RewardRarity.RARE: top = -top lowest = min(0, top) highest = max(0, top) random_point = random.randint(lowest, highest) index = cls.get_interface() result = index.query( category.name, (Operator.BETWEEN, lowest, random_point), attributes=['category', 'description', 'release-id', 'price'], limit=1) if len(result.items) == 0: result = index.query( category.name, (Operator.BETWEEN, random_point, highest), attributes=['category', 'description', 'release-id', 'price'], limit=1) if len(result.items) == 0: raise NotFoundException(f'No reward of type {category.name} found') return Reward.from_db_map(result.items[0])
def update_avatar(cls, user_sub: str, avatar: dict): interface = cls.get_interface() parts_ids = [part_id for part_id in set(avatar.values()) if part_id is not None] if len(parts_ids) > 0: logs = LogsService.batch_get( [LogKey(sub=user_sub, tag=join_key('REWARD', 'AVATAR', part_id)) for part_id in parts_ids], attributes=['data', 'tag']) if len(logs) != len(parts_ids): raise NotFoundException("An avatar part was not found") else: logs = [] avatar_parts = {int(split_key(log.tag)[-1]): Reward.from_api_map(log.data).to_api_map() for log in logs} new_avatar = { 'left_eye': avatar_parts.get(avatar.get('left_eye')), 'right_eye': avatar_parts.get(avatar.get('right_eye')), "mouth": avatar_parts.get(avatar.get('mouth')), "top": avatar_parts.get(avatar.get('top')), "bottom": avatar_parts.get(avatar.get('bottom')), "neckerchief": avatar_parts.get(avatar.get('neckerchief')) } interface.update(user_sub, updates={'avatar': new_avatar}) return new_avatar
def get_district(event: HTTPEvent): code = event.params["district"] response = DistrictModel.get({"code": code}) if response.item is None: raise NotFoundException(f"District '{code}' was not found") response.item = District.from_db(response.item).to_api_map() return JSONResponse(response.as_dict())
def get_active_task(cls, sub: str) -> Optional[Task]: from core.services.beneficiaries import BeneficiariesService beneficiary = BeneficiariesService.get(sub, ["target"]) target = beneficiary.target if target is None: raise NotFoundException('Task not found') return target
def get(cls, sub: str, stage: str, area: str, line: int, subline: int) -> Task: interface = cls.get_interface() item = interface.get(sub, join_key(stage, area, f"{line}.{subline}")).item if item is None: raise NotFoundException('Task not found') return Task.from_db_dict(item)
def join_group(event: HTTPEvent): district = event.params["district"] group = event.params["group"] code = event.json["code"] if not event.authorizer.is_beneficiary: if event.authorizer.is_scouter: raise ForbiddenException( 'Scouters accounts can\'t be migrated to a beneficiary account' ) UsersCognito.add_to_group(event.authorizer.username, "Beneficiaries") group_item = GroupsService.get(district, group, ["beneficiary_code"]).item if group_item is None: raise NotFoundException("Group not found") if group_item["beneficiary_code"] != code: raise ForbiddenException("Wrong code") BeneficiariesService.create(district, group, event.authorizer) return JSONResponse({"message": f"Joined group \"{group}\""})