FilterList model and endpoints#371
Conversation
This is the model which we will use for items that are either blacklisted or whitelisted. #305
The test_utils_account.py tests were never running, because the folder they were in had no __init__.py file. The test_models.py file was failing because it had an outdated import of the ModelReprMixin, which has moved to a new file. #305
|
An It makes more sense to enforce all records of a certain type to all be whitelist or all be a blacklist. Each type should have a default state (i.e. allowed or denied), and if an item of that type is present in the DB, then assume the opposite state. It can be up to the client to decide what that default state is. If you really need to have it in the DB, then it should be stored in a separate table as a mapping between type and allowed/denied rather than stored as a field for individual items in the list. |
Some types need both a blacklist and a whitelist, for example the Guild Invite IDs. Here the idea is that some guild invites will be blacklisted and will never be permitted, some will be whitelisted and will always be permitted, and everything else will be permitted based on certain criteria (whether they are Partnered or Verified, in this case). The UniqueConstraint makes it impossible for a blacklist and a whitelist for the same item to simultaneously exist, so every item is always exclusively on one of the two lists. |
I was handling this in a Django vanilla kind of way, which was causing the constraint to return a 500 instead of a 400. This changes the approach to use the DRF way, and makes it return 400. It doesn't actually change the way anything behaves, other than returning the right status code.
This really should've been handled automatically by DRF, and in the future, it will be. But for now, we need to have constraints both on the serializer (to get status code 400), and on the model (to prevent direct database constraint violations). See encode/django-rest-framework#7173
SebastiaanZ
left a comment
There was a problem hiding this comment.
Alright, that's quite a lot of commits and files, but it looks solid. I've also ran some manual tests locally by polling the endpoints; looks good. One requested change to prevent us from using a wrong default value somewhere.
Co-authored-by: Sebastiaan Zeeff <33516116+SebastiaanZ@users.noreply.github.com>
SebastiaanZ
left a comment
There was a problem hiding this comment.
Nice work. Adding in the rework/fix/kaizen of the social account tests made it a slightly less focused PR, but I doubt fixing that would have otherwise been a major priority. Better to get it done.
|
Now that we're gonna merge this pretty soon, I'll write a migration to get all the data ready. |
This will populate the table with domain names, guild invites, filter tokens and file formats.
Now that we have a migration that adds data, we can no longer have tests that operate on the assumption that the database is going to be empty. So, we're now clearing that table before these tests run.
|
Is this now ready for merge? |
|
Best to wait until the bot PR is ready too, in case a review there requires changes here. |
This pull request introduces a new model, FilterList, and the prerequisite viewset, URL endpoint, migrations, serializer and tests.
The intention of this model is to store things that we will either whitelist or blacklist. For example, we'll be storing file formats, guild invite IDs, and other things that are currently in the bot config file, but that we want to be able to modify during runtime.
Eventually, we may set up automation where this table is synchronized against stuff like the Awesome Discord Programming List.
Review style: commit-by-commit.
Closes #305.
Routes
GET /bot/filter-lists
Returns all filterlist items in the database.
Response format
Status codes
GET /bot/filter-lists/<id:int>
Returns a specific FilterList item from the database.
Response format
Status codes
GET /bot/filter-lists/get-types
Returns a list of valid list types that can be used in POST requests.
Response format
Status codes
POST /bot/filter-lists
Adds a single FilterList item to the database.
Request body
Status codes
DELETE /bot/filter-lists/<id:int>
Deletes the FilterList item with the given
id.Status codes
iddoes not exist