Database
initialize_db(app)
The initialize_db function is called by the application factory. It initializes our database and sets up a custom JSON encoder for our models.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
app |
Access the application instance itself, and is used to get access to the |
required |
Returns:
Type | Description |
---|---|
A flask application object |
Source code in database/db.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
Collection
Bases: db.Document
This class is a MongoDB document that represents a collection of items
Source code in database/models.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
Snippet
Bases: db.Document
The Snippet
class inherits from the db.Document
class, which is a MongoEngine class that allows us to save
documents to MongoDB
Source code in database/models.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
|
__repr__()
The repr function is what tells Python how to represent an object of our class as a string. The default implementation for this function is to return the instance address, which in this case would be: <main.Post object at 0x7f8c9d6b0a90>
Parameters:
Name | Type | Description | Default |
---|---|---|---|
self |
Refer to the object that is calling the function |
required |
Returns:
Type | Description |
---|---|
The title of the object |
Source code in database/models.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
like_count()
The like_count function returns the number of likes that a post has. It takes in self as an argument, which is the instance of Post that called it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
self |
Refer to the object that is calling the method |
required |
Returns:
Type | Description |
---|---|
The length of the likedby list |
Source code in database/models.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
User
Bases: db.Document
The User class is a subclass of the db.Document class, which is a subclass of the db.Model class
Source code in database/models.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
|
check_password(password)
The check_password function takes in a password and the hash of the password and checks if they are equal. If they are, it returns true. Otherwise, it returns false.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
self |
Reference the instance of the object that is being created |
required | |
password |
Check the password entered by the user against the hashed password stored in the database |
required |
Returns:
Type | Description |
---|---|
True or false depending on whether the password_hash matches the password |
Source code in database/models.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
|
all_languages()
The all_languages function returns a list of all the languages that have been used in any snippet. It does this by querying the Snippet collection and then iterating through each document to check if the language field is already in the list of all_languages. If it isn't, it adds it to the list.
Returns:
Type | Description |
---|---|
A list of all the languages in the database |
Source code in database/constants.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
all_tags()
The all_tags function returns a list of all the tags in the database. It does this by iterating through each snippet and appending any new tag to the list of all_tags. It then returns that list.
Returns:
Type | Description |
---|---|
A list of all the tags in the database |
Source code in database/constants.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|