This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tables.py
55 lines (48 loc) · 1.85 KB
/
tables.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
# Overlord library
from core.library import models, uuid, JsonResponse, api, console
class TestModel(models.Model):
"""
This is a test model used to demonstrate Overlord's Universal API
and should not be deployed to production
It contains two columns within it's database table `uuid` & `data` which
demonstrates how you can create a unique uuid field for your models
Also contains a single function which allows you add a new row with
a unique `uuid` and return a success response upon completion
"""
uuid = models.CharField(
null=False,
blank=False,
unique=True,
default=uuid,
max_length=36,
primary_key=True
)
data = models.TextField(
null=False,
blank=False,
default="no data provided"
)
@staticmethod
def add_test_data(req, *args, **kwargs) -> JsonResponse:
"""
This function adds a row to your database table with a piece of `data`
which also has a universal unique identifier (uuid) attached to it
If the row is created successfully the api will respond with "success response"
If the row fails to be created the api will respond with an "error response"
An error response via the `api.error` function will only provide insightful
data if the server is running in DEBUG mode, otherwise the message will read
"[ERROR] 500 Internal Server" so that users do not get in-sight into your
production server
"""
try:
# Get the test data from the request body
test_data = api.get_body(req)
# Print the received test_data to console
console.out(f"\n> Received Test Data\n\n{test_data}\n")
# Create the test data in a db table row
TestModel.objects.update_or_create(data=test_data)
# Return a success response
return api.success()
except Exception as db_error:
# Return an error response
return api.error(db_error)