Skip to content

Commit c3fe458

Browse files
committed
Init DB
0 parents  commit c3fe458

33 files changed

+333
-0
lines changed

blog/__init__.py

Whitespace-only changes.
147 Bytes
Binary file not shown.

blog/__pycache__/admin.cpython-39.pyc

339 Bytes
Binary file not shown.

blog/__pycache__/apps.cpython-39.pyc

420 Bytes
Binary file not shown.
1.17 KB
Binary file not shown.

blog/__pycache__/tests.cpython-39.pyc

188 Bytes
Binary file not shown.

blog/admin.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from django.contrib import admin
2+
3+
from .models.mongo_models import Comment
4+
from .models.postgres_models import Post
5+
6+
admin.site.register(Post)
7+
admin.site.register(Comment)
8+

blog/apps.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class BlogConfig(AppConfig):
5+
default_auto_field = 'django.db.models.BigAutoField'
6+
name = 'blog'

blog/db_routers.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class NonRelRouter:
2+
"""
3+
A router to control if database should use
4+
primary database or non-relational one.
5+
"""
6+
7+
nonrel_models = {'comment'}
8+
9+
def db_for_read(self, model, **_hints):
10+
if model._meta.model_name in self.nonrel_models:
11+
return 'nonrel'
12+
return 'default'
13+
14+
def db_for_write(self, model, **_hints):
15+
if model._meta.model_name in self.nonrel_models:
16+
return 'nonrel'
17+
return 'default'
18+
19+
def allow_relation(self, obj1, obj2, **_hints):
20+
if obj1._meta.model_name in self.nonrel_models or obj2._meta.model_name in self.nonrel_models:
21+
return True
22+
return None
23+
24+
def allow_migrate(self, _db, _app_label, model_name=None, **_hints):
25+
if _db == 'nonrel' or model_name in self.nonrel_models:
26+
return False
27+
return True

blog/migrations/0001_initial.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Generated by Django 4.1 on 2022-10-18 13:39
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
initial = True
10+
11+
dependencies = [
12+
]
13+
14+
operations = [
15+
migrations.CreateModel(
16+
name='Post',
17+
fields=[
18+
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
19+
('title', models.CharField(max_length=100)),
20+
('content', models.TextField()),
21+
('created_at', models.DateTimeField(auto_now_add=True)),
22+
('updated_at', models.DateTimeField(auto_now=True)),
23+
],
24+
),
25+
migrations.CreateModel(
26+
name='Comment',
27+
fields=[
28+
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
29+
('message', models.TextField()),
30+
('created_at', models.DateTimeField(auto_now_add=True)),
31+
('updated_at', models.DateTimeField(auto_now=True)),
32+
('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='blog.post')),
33+
],
34+
options={
35+
'ordering': ('-created_at',),
36+
},
37+
),
38+
]

blog/migrations/__init__.py

Whitespace-only changes.
Binary file not shown.
158 Bytes
Binary file not shown.

blog/models/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .postgres_models import *
2+
from .mongo_models import *
210 Bytes
Binary file not shown.
983 Bytes
Binary file not shown.
Binary file not shown.

blog/models/mongo_models.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from django.db import models
2+
from djongo import models as mongo_models
3+
4+
from .postgres_models import Post
5+
6+
7+
class Comment(models.Model):
8+
_id = mongo_models.ObjectIdField()
9+
post = models.ForeignKey(Post, on_delete=models.CASCADE)
10+
message = models.TextField()
11+
created_at = models.DateTimeField(auto_now_add=True)
12+
updated_at = models.DateTimeField(auto_now=True)
13+
14+
class Meta:
15+
_db = 'nonrel'
16+
ordering = ("-created_at", )
17+
18+
def __str__(self):
19+
return self.message

blog/models/postgres_models.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from django.db import models
2+
3+
class Post(models.Model):
4+
title = models.CharField(max_length=100)
5+
content = models.TextField()
6+
created_at = models.DateTimeField(auto_now_add=True)
7+
updated_at = models.DateTimeField(auto_now=True)
8+
9+
def __str__(self):
10+
return self.title

blog/tests.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

blog/views.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.shortcuts import render
2+
3+
# Create your views here.

db.sqlite3

132 KB
Binary file not shown.

manage.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
"""Django's command-line utility for administrative tasks."""
3+
import os
4+
import sys
5+
6+
7+
def main():
8+
"""Run administrative tasks."""
9+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'multidb.settings')
10+
try:
11+
from django.core.management import execute_from_command_line
12+
except ImportError as exc:
13+
raise ImportError(
14+
"Couldn't import Django. Are you sure it's installed and "
15+
"available on your PYTHONPATH environment variable? Did you "
16+
"forget to activate a virtual environment?"
17+
) from exc
18+
execute_from_command_line(sys.argv)
19+
20+
21+
if __name__ == '__main__':
22+
main()

multidb/__init__.py

Whitespace-only changes.
150 Bytes
Binary file not shown.
2.41 KB
Binary file not shown.
927 Bytes
Binary file not shown.
553 Bytes
Binary file not shown.

multidb/asgi.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
ASGI config for multidb project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'multidb.settings')
15+
16+
application = get_asgi_application()

multidb/settings.py

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
"""
2+
Django settings for multidb project.
3+
4+
Generated by 'django-admin startproject' using Django 4.1.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/4.1/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/4.1/ref/settings/
11+
"""
12+
13+
from pathlib import Path
14+
15+
# Build paths inside the project like this: BASE_DIR / 'subdir'.
16+
BASE_DIR = Path(__file__).resolve().parent.parent
17+
18+
19+
# Quick-start development settings - unsuitable for production
20+
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/
21+
22+
# SECURITY WARNING: keep the secret key used in production secret!
23+
SECRET_KEY = 'django-insecure-7(i@ivgfgs&7$18_soy$o%m^rlvt@3+&xx=05@_+ig+@zaxh-2'
24+
25+
# SECURITY WARNING: don't run with debug turned on in production!
26+
DEBUG = True
27+
28+
ALLOWED_HOSTS = []
29+
30+
31+
# Application definition
32+
33+
INSTALLED_APPS = [
34+
'django.contrib.admin',
35+
'django.contrib.auth',
36+
'django.contrib.contenttypes',
37+
'django.contrib.sessions',
38+
'django.contrib.messages',
39+
'django.contrib.staticfiles',
40+
'blog'
41+
]
42+
43+
MIDDLEWARE = [
44+
'django.middleware.security.SecurityMiddleware',
45+
'django.contrib.sessions.middleware.SessionMiddleware',
46+
'django.middleware.common.CommonMiddleware',
47+
'django.middleware.csrf.CsrfViewMiddleware',
48+
'django.contrib.auth.middleware.AuthenticationMiddleware',
49+
'django.contrib.messages.middleware.MessageMiddleware',
50+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
51+
]
52+
53+
ROOT_URLCONF = 'multidb.urls'
54+
55+
TEMPLATES = [
56+
{
57+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
58+
'DIRS': [],
59+
'APP_DIRS': True,
60+
'OPTIONS': {
61+
'context_processors': [
62+
'django.template.context_processors.debug',
63+
'django.template.context_processors.request',
64+
'django.contrib.auth.context_processors.auth',
65+
'django.contrib.messages.context_processors.messages',
66+
],
67+
},
68+
},
69+
]
70+
71+
WSGI_APPLICATION = 'multidb.wsgi.application'
72+
73+
74+
# Database
75+
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases
76+
77+
DATABASES = {
78+
'default': {
79+
'ENGINE': 'django.db.backends.sqlite3',
80+
'NAME': BASE_DIR / 'db.sqlite3',
81+
},
82+
"nonrel": {
83+
"ENGINE": "djongo",
84+
"NAME": "blog_mongo",
85+
"CLIENT": {
86+
"host": "localhost",
87+
"port": 27017,
88+
# "username": os.environ.get('MONGO_DB_USERNAME'),
89+
# "password": os.environ.get('MONGO_DB_PASSWORD'),
90+
},
91+
'TEST': {
92+
'MIRROR': 'default',
93+
},
94+
}
95+
}
96+
97+
DATABASE_ROUTERS = ['blog.db_routers.NonRelRouter', ]
98+
99+
# Password validation
100+
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators
101+
102+
AUTH_PASSWORD_VALIDATORS = [
103+
{
104+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
105+
},
106+
{
107+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
108+
},
109+
{
110+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
111+
},
112+
{
113+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
114+
},
115+
]
116+
117+
118+
# Internationalization
119+
# https://docs.djangoproject.com/en/4.1/topics/i18n/
120+
121+
LANGUAGE_CODE = 'en-us'
122+
123+
TIME_ZONE = 'UTC'
124+
125+
USE_I18N = True
126+
127+
USE_TZ = True
128+
129+
130+
# Static files (CSS, JavaScript, Images)
131+
# https://docs.djangoproject.com/en/4.1/howto/static-files/
132+
133+
STATIC_URL = 'static/'
134+
135+
# Default primary key field type
136+
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
137+
138+
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

multidb/urls.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""multidb URL Configuration
2+
3+
The `urlpatterns` list routes URLs to views. For more information please see:
4+
https://docs.djangoproject.com/en/4.1/topics/http/urls/
5+
Examples:
6+
Function views
7+
1. Add an import: from my_app import views
8+
2. Add a URL to urlpatterns: path('', views.home, name='home')
9+
Class-based views
10+
1. Add an import: from other_app.views import Home
11+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
12+
Including another URLconf
13+
1. Import the include() function: from django.urls import include, path
14+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
15+
"""
16+
from django.contrib import admin
17+
from django.urls import path
18+
19+
urlpatterns = [
20+
path('admin/', admin.site.urls),
21+
]

multidb/wsgi.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
WSGI config for multidb project.
3+
4+
It exposes the WSGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.wsgi import get_wsgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'multidb.settings')
15+
16+
application = get_wsgi_application()

requirements.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Django==4.1
2+
3+
pymongo==3.12.1
4+
djongo==1.3.6

0 commit comments

Comments
 (0)