Ejemplo n.º 1
0
 def partial_update(self, request, *args, **kwargs):
     """PATCH requests fall here.
     Here I'm working from the idea that the entry already exists,
     so I just find it and update the counters. Only if it doesn't exist,
     I create a new one and try to save it. If it can't save because
     there's no member in the database, I create that member."""
     try:
         data = request.data
         try:
             # Find the existing message entry
             nwords = Nwords.objects.get(
                 guild_id=Guild(guild_id=data["guild_id"]),
                 user_id=User(user_id=data["user_id"]),
                 period=data["period"])
         except ObjectDoesNotExist:
             # Entry not found - create one!
             nwords = Nwords(guild_id=Guild(guild_id=data["guild_id"]),
                             user_id=User(user_id=data["user_id"]),
                             period=data["period"])
         # Update counters
         nwords.nigger += data["nigger"]
         nwords.n***a += data["n***a"]
         try:
             # Submit changes
             nwords.save()
         except IntegrityError:
             # If there's no member - create one!
             user = User(user_id=data["user_id"])
             user.save()
             nwords.save()
         serializer = self.get_serializer(nwords)
         return Response(serializer.data)
     except Exception as e:
         print(e)
Ejemplo n.º 2
0
 def partial_update(self, request, *args, **kwargs):
     """PATCH requests fall here.
     Here I'm working from the idea that the entry already exists,
     so I just find it and update the counters. Only if it doesn't exist,
     I create a new one and try to save it. If it can't save because
     there's no member in the database, I create that member."""
     data = request.data
     try:
         # Find the existing message entry
         emotes = Emotes.objects.get(
             guild_id=Guild(guild_id=data["guild_id"]),
             user_id=User(user_id=data["user_id"]),
             emote=data["emote"],
             period=data["period"])
     except ObjectDoesNotExist as e:
         # Entry not found - create one!
         emotes = Emotes(guild_id=Guild(guild_id=data["guild_id"]),
                         user_id=User(user_id=data["user_id"]),
                         emote=data["emote"],
                         period=data["period"])
     # Update counters
     emotes.count += data["count"]
     try:
         # Submit changes
         emotes.save()
     except IntegrityError as e:
         # If there's no member - create one!
         if "user_id" in str(e.__cause__):
             user = User(user_id=data["user_id"])
             user.save()
         else:
             raise e
         emotes.save()
     serializer = self.get_serializer(emotes)
     return Response(serializer.data)
Ejemplo n.º 3
0
def get_or_init_activity(data):
    try:
        # Find the existing activity entry
        activity = Activity.objects.get(
            guild_id=Guild(guild_id=data["guild_id"]),
            user_id=User(user_id=data.get("user_id", data.get("giver_id"))),
            period=date.today())
    except ObjectDoesNotExist:
        # Entry not found - create one!
        activity = Activity(
            guild_id=Guild(guild_id=data["guild_id"]),
            user_id=User(user_id=data.get("user_id", data.get("giver_id"))),
            period=date.today())
    return activity
Ejemplo n.º 4
0
 def partial_update(self, request, *args, **kwargs):
     """PATCH requests fall here.
     Here I'm working from the idea that the entry already exists,
     so I just find it and update the counters. Only if it doesn't exist,
     I create a new one and try to save it. If it can't save because
     there's no member in the database, I create that member."""
     try:
         data = request.data
         try:
             # Find the existing message entry
             messages = Messages.objects.get(
                 guild_id=Guild(guild_id=data["guild_id"]),
                 channel_id=data["channel_id"],
                 user_id=User(user_id=data["user_id"]),
                 period=data["period"]
             )
         except ObjectDoesNotExist:
             # Entry not found - create one!
             messages = Messages(
                 guild_id=Guild(guild_id=data["guild_id"]),
                 channel_id=data["channel_id"],
                 user_id=User(user_id=data["user_id"]),
                 period=data["period"]
             )
         # Update counters
         messages.postcount += data["postcount"]
         messages.attachments += data["attachments"]
         messages.words += data["words"]
         try:
             # Submit changes
             messages.save()
         except IntegrityError:
             # If there's no member - create one!
             author = User(user_id=data["user_id"])
             author.save()
             messages.save()
         serializer = self.get_serializer(messages)
         activity_functions.add_message_activity(
             data, Guild.objects.get(guild_id=data["guild_id"])
         )
         return Response(serializer.data)
     except Exception as e:
         print(e)
Ejemplo n.º 5
0
 def partial_update(self, request, guild_id, *args, **kwargs):
     """PATCH requests fall here.
     Here I'm working from the idea that the entry already exists,
     so I just find it and update. Only if it doesn't exist,
     I create a new one and save it."""
     data = request.data
     try:
         # Find the existing guild entry
         guild = Guild.objects.get(guild_id=guild_id)
     except ObjectDoesNotExist:
         # Entry not found - create one!
         guild = Guild(guild_id=guild_id)
     # Update kwargs
     for key, value in data.items():
         print(key, value)
         if value == "reset": value = None
         setattr(guild, key, value)
     # Submit changes
     guild.save()
     serializer = self.get_serializer(guild)
     return Response(serializer.data)
Ejemplo n.º 6
0
 def partial_update(self, request, *args, **kwargs):
     """PATCH requests fall here.
     Here I'm working from the idea that the entry already exists,
     so I just find it and update the counters. Only if it doesn't exist,
     I create a new one and try to save it. If it can't save because
     there's no member in the database, I create that member."""
     data = request.data
     try:
         # Find the existing message entry
         voice = Voice.objects.get(
             guild_id=Guild(guild_id=data["guild_id"]),
             channel_id=data["channel_id"],
             user_id=User(user_id=data["user_id"]),
             members=data["members"],
             period=data["period"])
     except ObjectDoesNotExist as e:
         # Entry not found - create one!
         voice = Voice(guild_id=Guild(guild_id=data["guild_id"]),
                       channel_id=data["channel_id"],
                       user_id=User(user_id=data["user_id"]),
                       members=data["members"],
                       period=data["period"])
     # Update counters
     voice.duration += data["duration"]
     try:
         # Submit changes
         voice.save()
     except IntegrityError as e:
         # If there's no member - create one!
         if "user_id" in str(e.__cause__):
             user = User(user_id=data["user_id"])
             user.save()
         else:
             raise e
         voice.save()
     serializer = self.get_serializer(voice)
     activity_functions.add_voice_activity(
         data, Guild.objects.get(guild_id=data["guild_id"]))
     return Response(serializer.data)