Skip to content

Commit

Permalink
Merge pull request #30 from codewitgabi/django-support-dev
Browse files Browse the repository at this point in the history
Added postgresql support for django and changed default launch template
  • Loading branch information
JC-Coder authored Oct 10, 2023
2 parents ba05b99 + 7c7e1c6 commit a2bf5c6
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 24 deletions.
6 changes: 4 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ async function startProject() {
initDB = await promptInitDatabase();

if (initDB) {
database = await promptDatabase();
database = await promptDatabase(framework);

orm = await promptOrm(database);
if (jsBackendStacks.includes(framework)) {
orm = await promptOrm(database);
}
}

await createBackendProject(projectName, framework, database, orm);
Expand Down
2 changes: 1 addition & 1 deletion src/test/django_
Submodule django_ updated from 0480fa to e7a0d0
49 changes: 36 additions & 13 deletions src/utils/create-backend-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import { DJANGO_WSGI } from "../../templates/backend/django/base/wsgi.js";
import { DJANGO_ASGI } from "../../templates/backend/django/base/asgi.js";
import { DJANGO_SETTINGS } from "../../templates/backend/django/base/settings.js";
import { DJANGO_ENV_VARIABLES } from "../../templates/backend/django/base/env.js";
import {
DJANGO_POSTGRES_SETUP,
DJANGO_SQLITE_SETUP,
} from "../../templates/backend/django/base/database.js";

// third-party imports

Expand Down Expand Up @@ -283,15 +287,42 @@ export async function createBackendProject(

writeToFile(`${destinationPath}/${projectName}/asgi.py`, DJANGO_ASGI);

// uses sqlite by default for now till support for postgresql is added.

/*
if (database) {
if (database && database !== "sqlite3") {
switch (database) {
case "postgresql":
updateFileContent(
`${destinationPath}/${projectName}/settings.py`,
DJANGO_SETTINGS,
{
projectName,
DATABASE_IMPORT: "import dj_database_url",
DATABASE_SETUP: DJANGO_POSTGRES_SETUP,
},
);

updateFileContent(`${destinationPath}/.env`, DJANGO_ENV_VARIABLES, {
SECRET_KEY: crypto.randomUUID().split("-").join(""),
DATABASE_ENV:
"DATABASE_URL=postgres://username:password@localhost:5432",
});
break;
}
} else {
updateFileContent(
`${destinationPath}/${projectName}/settings.py`,
DJANGO_SETTINGS,
{
projectName,
DATABASE_IMPORT: "",
DATABASE_SETUP: DJANGO_SQLITE_SETUP,
},
);

updateFileContent(`${destinationPath}/.env`, DJANGO_ENV_VARIABLES, {
SECRET_KEY: crypto.randomUUID().split("-").join(""),
DATABASE_ENV: "",
});
}
*/

// add updates to django starter files

Expand Down Expand Up @@ -319,14 +350,6 @@ export async function createBackendProject(
},
);

updateFileContent(
`${destinationPath}/${projectName}/settings.py`,
DJANGO_SETTINGS,
{
projectName,
},
);

if (shell.which("git")) {
// initialize git for the final source

Expand Down
3 changes: 2 additions & 1 deletion src/utils/prompts.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ export async function promptBackendFramework() {
}

export async function promptDatabase(framework) {
const choices = framework === "django" ? ["SQLite3"] : ["MongoDB"];
const choices =
framework === "django" ? ["SQLite3", "PostgreSQL"] : ["MongoDB"];

const ans = await inquirer.prompt([
{
Expand Down
14 changes: 14 additions & 0 deletions templates/backend/django/base/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const DJANGO_POSTGRES_SETUP = `{
'default': dj_database_url.parse(
os.environ.get("DATABASE_URL"),
conn_max_age=600,
conn_health_checks=True,
)
}`;

export const DJANGO_SQLITE_SETUP = `{
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}`;
2 changes: 2 additions & 0 deletions templates/backend/django/base/env.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const DJANGO_ENV_VARIABLES = `DEBUG=True
SECRET_KEY={{SECRET_KEY}}
{{DATABASE_ENV}}
`;
12 changes: 6 additions & 6 deletions templates/backend/django/base/settings.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const DJANGO_SETTINGS = `from pathlib import Path
from decouple import config
import os
{{DATABASE_IMPORT}}
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand Down Expand Up @@ -61,12 +62,7 @@ WSGI_APPLICATION = '{{projectName}}.wsgi.application'
# Database
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
DATABASES = {{DATABASE_SETUP}}
# Password validation
Expand Down Expand Up @@ -108,4 +104,8 @@ STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# Uncomment these when going into production environment.
# CSRF_COOKIE_SECURE = True
`;
22 changes: 22 additions & 0 deletions templates/backend/django/django-temp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<div align="center">
<h1>Django + Startease</h1>
</div>

## Project Setup

```bash
cd <project>
```

```bash
pip install -r requirements.txt
```

Once the dependencies are installed, configure your environment variables in the `.env` file especially if you're using __PostgreSQL__ database.

```bash
python manage.py runserver
```

Nice!!! your django server is now listening on [localhost](http://localhost:8000)

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions templates/backend/django/django-temp/static/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@
box-sizing: border-box;
}

.container {
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
color-scheme: only dark;
background: #242424;
color: #f2f2f2;
padding: 2em;
}

h1 {
font-size: 1.5rem;
}

img {
height: 100px;
width: 100%;
object-fit: contain;
mix-blend-mode: revert;
}
28 changes: 27 additions & 1 deletion templates/backend/django/django-temp/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,33 @@
<title>Hello Django</title>
</head>
<body>
<h1>Hello Django!</h1>
<div class="container">
<a href="https://github.com/JC-Coder/startease">
<svg
version="1.0"
xmlns="http://www.w3.org/2000/svg"
width="125pt"
height="150pt"
viewBox="0 0 640 1280"
preserveAspectRatio="xMidYMid meet"
>
<g
transform="translate(0.000000,1280.000000) scale(0.100000,-0.100000)"
fill="#ff5722"
stroke="none"
>
<path
d="M4780 12433 c-56 -169 -211 -639 -345 -1043 -307 -926 -541 -1638 -1055 -3200 -138 -421 -260 -790 -270 -820 -33 -98 -478 -1454 -484 -1475 -3 -11 -7 -24 -8 -28 -2 -4 169 -8 380 -7 363 0 383 -1 378 -17 -8 -31 -1071 -3933 -1155 -4243 -44 -162 -139 -513 -211 -780 -72 -267 -133 -491 -135 -497 -2 -7 -1 -13 3 -13 4 1 66 141 139 313 73 172 207 488 299 702 91 215 237 557 324 760 87 204 231 541 320 750 89 209 292 684 450 1055 158 371 360 845 449 1053 89 207 233 547 322 755 89 207 196 459 239 560 l78 182 -439 0 c-241 0 -439 3 -439 6 0 4 12 63 26 133 14 69 70 347 124 616 55 270 140 693 190 940 50 248 131 648 180 890 49 242 170 843 270 1335 99 492 221 1093 270 1335 49 242 116 575 149 739 34 165 59 301 57 303 -2 3 -50 -134 -106 -304z"
/>
</g>
</svg>
</a>

<a href="">
<img src="{% static 'assets/django.png' %}" />
</a>
</div>

<script src="{% static 'js/index.js' %}"></script>
</body>
</html>

0 comments on commit a2bf5c6

Please sign in to comment.