- Read the guideline before start
Consider the following models:
User
model with the following fields:username
- user's usernamefirst_name
- user's first_namelast_name
- user's last_namebio
- any details such as age, country ot city
Chat
model:title
- chat titledescription
- a short chat descriptionusers
- many-to-many field, chat participants
Message
model:text
- message contentsent
- time when message was sentis_delivered
- boolean value,True
if message was delivered successfullyuser
- foreign key, points to the senderchat
- foreign key, points to the chat where the message was sent
Use the following command to load prepared data from fixture to test and debug your code:
python manage.py loaddata chat_data.json
Write some functions which should perform different queries on this domain:
-
get_messages_that_contain_word
- function should return list of messages that containsword
in its text. Use case-insensitive containment test. -
get_untitled_chats
- should return chats which title starts with"Untitled"
string. For example, chatsUntitled
,Untitled(1)
,Untitled(2)
should be considered as untitled. -
get_users_who_sent_messages_in_2015
- should return list of tuples withfirst_name
andlast_name
of users who sent messages in 2015. Usevalues_list
. -
get_actual_chats
- should return list of the actual chats. Chats is actual when it is a message in it that was sent later than 2020. -
get_messages_contain_authors_first_name
- should return a list ofMessages
which text contains author'sfirst_name
. -
get_users_who_sent_messages_starting_with_m_or_a
- should return a list of users who sent at least one message starts witha
orm
. Use case-insensitive test. -
get_delivered_or_admin_messages
- should return a list of messages that was delivered or sent by a user whose username starts with"admin"
prefix. -
get_count_messages_sent_by_first_name
- should return number of messages sent by all users with givenfirst_name
-
get_top_users_by_number_of_the_messages
- should return top-3 users by number of sent messages. They should have an additional fieldnum_messages
that should be equal to this number.
users = get_top_users_by_number_of_the_messages()
print(
users[0].username, # "user1"
users[0].num_messages # 7
)
Note: Use list()
to convert QuerySet
to the list
.
Also, write two more functions which should use select_related
and prefetch_related
methods to decrease the number of queries to the database.
get_last_5_messages_dicts
- should return list that contains five last messages. Each message should be represented as a dict with the following fields:"from"
- a username of the sender"text"
- text of the message
messages = get_last_5_messages_dicts()
print(message[3]) # {"from": "max", text: "Hello, mates!"}
get_chat_dicts
- should return a list of chats represented by dicts. Each dict should contain the following fields:"id"
- chat id"title"
- chat title"users"
- a list of the participants' names
chats = get_chat_dicts()
# print(chats[0])
# {
# "id": 1,
# "title": "My family",
# "users": ["mom", "dad", "me"]
# }