🐻 Full-text Search & File Upload 🐻
Posted on Sep 26th, 2022
Today’s Topics
- Implementing search, including Postgres full-text search
🎯 Project due on Thursday afternoon
Please include a README in your project repo. The README should:
- be written in Markdown
- include your production application’s URL
- have instructions for getting your application running locally, so that any developer could pull it down and run it. If your setup makes any assumptions about the environment (e.g., you have Python 3.10.2 installed locally), you should state them in the README.
Your README should include clear documentation for your API’s available endpoints.
👉 If your project meets minimum requirements today, HUZZAH! 🎉 That is awesome. You should be working on at least one additional or spicy feature.
👉 If your project does not yet meet minimum requirements, you should aim for meeting them by the end of the day Wednesday.
Requirements for QuestionBox, Back End
Note: Depending on how you’ve constructed your API, you might have separate endpoints for all of the below, or some of the functionality might be combined in a single endpoint (for instance, if you nested answers in the question detail endpoint, like questions/4/answers
). What matters is that you have endpoints that your front-end team can use to perform all the necessary actions they need and that are documented in your README.
⚠️ Be sure to test that you have implemented permissions-checking correctly for these endpoints. For example, your API should not allow a user who is not the question-asker to mark an answer as accepted.
- token login, logout, and register (i.e., create a new user)
- list all questions
- list answers to a specific question
- show details about a specific question
- create a question (if authenticated)
- create an answer to a question (if authenticated and if you are not the question asker)
- mark an answer as “accepted” (if you are the question asker)
- list of own questions for an authenticated user
- list of own answers for an authenticated user
- star/favorite questions or answers
- unstar/unfavorite questions or answers
- search questions (possibly both questions and answers)
- A README with endpoints documented
Requirements for Social Cards, Back End
⚠️ Be sure to test that you have implemented permissions-checking correctly for these endpoints. For example, your API should not allow a user who is not the creator of a card to update a card.
- token login, logout, and register (i.e., create a new user)
- list all cards
- list all cards you’ve created (you, the authenticated user)
- list all cards created by a user that you follow
- show details of one card
- create a new card (if authenticated)
- update a card (if authenticated and it’s your card)
- delete a card (if authenticated and it’s your card)
- follow another user (if authenticated)
- unfollow another user (if authenticated)
- list all the users you follow
- A README with endpoints documented
Handling requests that include attached files
Using Insomnia
- Select the right HTTP method for your endpoint.
- Choose binary file attachment from the body menu (where you normally put the body of a request)
-
Set headers on the Headers tab (this example assumes an image file in jpeg format, named
profile-photo.jpg
):Content-Type: image/jpeg Content-Disposition: attachment; filename=profile-photo.jpg
For more information on the values for Content-Type
:
CORS for file upload
Assuming you are using django-cors-headers
, you’ll need to add the following to settings.py
to allow the request headers necessary for file attachments:
# in settings.py
from corsheaders.defaults import default_headers
CORS_ALLOW_HEADERS = list(default_headers) + [
'content-disposition',
]
📖 Read | 📺 Watch | 🎧 Listen
File Upload
- Django File (and Image) Uploads Tutorial -> A good and very recent post from Will Vincent; he does not include all the necessary info to make file uploads work in production but otherwise it’s a good overview.
- 📖 File Upload with DRF
- 🎧 + 📖 Success with Static Files
- 📖 What is Amazon S3? -> Skim this – this is Amazon’s documentation and it gets really in-depth.
- 📺 Introduction to S3 -> Also from Amazon
Search
- 📖 Basic and Full-Text Search with Django and Postgres
- 📖 If you want A LOT more detail about full-text search in Postgres and Django, this blog piece has you covered
- 📖 Blog post with more on full-text search
- 📺 Search from the Ground Up -> Will Vincent talk at DjangoCon 2019 explaining search in detail
- 📺 Pretty Printed: How to Perform Full Text Searches in Django with Postgres
🔖 Resources
📁 Creating a README
- Make a README
- Awesome README way more here than you actually need but you might like this mega-list of resources.
@action
decorator in ViewSets
- DRF Docs: Marking extra actions for routing with the
@action
decorator - DRF Docs: Routing for extra actions
Filtering and search
- DRF - Filtering -> Includes how to filter your output based on GET parameters, which you will want to do for using search terms.
- Django Docs: full-text search & the search lookup
- Django Docs: SearchVector -> You’ll need this if you want to search against more than a single field
File uploads
- Django Docs: ImageField
- Django Docs: FileField
- Pillow: Python Imaging Library
django-imagekit
-> If you want to resize images when they are uploaded, or do any kind of image processing, you will need this. Don’t add it unless you know you need it.
- How to Set Up Amazon S3
django-storages
- DRF
FileUploadParser
Without this you will get errors about unsupported media types
👾 Code & Notes
- Example Recipes API
- Code from class: Djoser and testing POST endpoints
- Notes on Django Queries These are the same notes you may have seen at the beginning of the Phase. I’m including them here for easy reference, as they show examples of queries and filters that might come in handy for search endpoints.