Skip to content

Commit

Permalink
Add owned_by in missing places
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver-ni committed Dec 2, 2021
1 parent 88d0ed3 commit 6100bbc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
4 changes: 3 additions & 1 deletion cogs/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ class Pokemon(PokemonBase, Document):
class Meta:
strict = False

owned_by = fields.StringField(required=True)


class EmbeddedPokemon(PokemonBase, EmbeddedDocument):
class Meta:
Expand Down Expand Up @@ -583,7 +585,7 @@ async def update_pokemon(self, pokemon, update):
pokemon = pokemon._id
if isinstance(pokemon, dict) and "_id" in pokemon:
pokemon = pokemon["_id"]
return await self.db.pokemon.update_one({"_id": pokemon}, update)
return await self.db.pokemon.update_one({"_id": pokemon, "owned_by": "user"}, update)

async def fetch_pokemon(self, member: discord.Member, idx: int):
if isinstance(idx, ObjectId):
Expand Down
16 changes: 10 additions & 6 deletions cogs/pokemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ async def nickall(self, ctx, **flags):
pokemon = self.bot.mongo.fetch_pokemon_list(ctx.author, aggregations)

await self.bot.mongo.db.pokemon.update_many(
{"_id": {"$in": [x.id async for x in pokemon]}},
{"_id": {"$in": [x.id async for x in pokemon]}, "owned_by": "user"},
{"$set": {"nickname": nicknameall}},
)

Expand Down Expand Up @@ -348,7 +348,7 @@ async def favoriteall(self, ctx, **flags):
return await ctx.send("Aborted.")

await self.bot.mongo.db.pokemon.update_many(
{"_id": {"$in": [x.id async for x in pokemon]}},
{"_id": {"$in": [x.id async for x in pokemon]}, "owned_by": "user"},
{"$set": {"favorite": True}},
)

Expand Down Expand Up @@ -435,7 +435,7 @@ async def unfavoriteall(self, ctx, **flags):
return await ctx.send("Aborted.")

await self.bot.mongo.db.pokemon.update_many(
{"_id": {"$in": [x.id async for x in pokemon]}},
{"_id": {"$in": [x.id async for x in pokemon]}, "owned_by": "user"},
{"$set": {"favorite": False}},
)

Expand Down Expand Up @@ -756,7 +756,8 @@ async def release(self, ctx, args: commands.Greedy[converters.PokemonConverter])
# confirmed, release

result = await self.bot.mongo.db.pokemon.update_many(
{"_id": {"$in": list(ids)}, "owner_id": {"$ne": None}}, {"$set": {"owner_id": None}}
{"_id": {"$in": list(ids)}, "owned_by": "user"},
{"$set": {"owned_by": "released"}},
)
await self.bot.mongo.update_member(
ctx.author,
Expand Down Expand Up @@ -856,8 +857,11 @@ async def releaseall(self, ctx, **flags):
pokemon = self.bot.mongo.fetch_pokemon_list(ctx.author, aggregations)

result = await self.bot.mongo.db.pokemon.update_many(
{"_id": {"$in": [x.id async for x in pokemon]}, "owner_id": {"$ne": None}},
{"$set": {"owner_id": None}},
{
"_id": {"$in": [x.id async for x in pokemon]},
"owned_by": "user",
},
{"$set": {"owned_by": "released"}},
)

await self.bot.mongo.update_member(
Expand Down
4 changes: 4 additions & 0 deletions migrations/2021-12-01-pokemon_listing_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
client = MongoClient(config.DATABASE_URI)
db = client[config.DATABASE_NAME]

print("Part 1...")

db.pokemon.update_many({"owned_by": {"$exists": False}}, {"$set": {"owned_by": "user"}})

print("Part 2...")

db.listing.aggregate(
[
{
Expand Down

0 comments on commit 6100bbc

Please sign in to comment.