Skip to content

Commit

Permalink
19 write database models (#20)
Browse files Browse the repository at this point in the history
* add main app in settings

* create models and migrations

* change privacy enum type to string instead of integer

* add logs models

* add comment votes model

* add logs models and management

* add pycache to gitignore
  • Loading branch information
ylked authored Mar 6, 2024
1 parent 7d10f68 commit 90574e3
Show file tree
Hide file tree
Showing 23 changed files with 1,472 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
api/db.sqlite3

frontend/.vscode/

api/logs.sqlite3
__pycache__
20 changes: 20 additions & 0 deletions api/neurona/DatabaseRouter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

class DatabaseRouter:

def db_for_read(self, model, **hints):
if model._meta.app_label == 'neuronaLogs':
return 'logs'
return 'default'

def db_for_write(self, model, **hints):
if model._meta.app_label == 'neuronaLogs':
return 'logs'
return 'default'

def allow_relation(self, obj1, obj2, **hints):
return True

def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label == 'neuronaLogs':
return db == 'logs'
return None
14 changes: 8 additions & 6 deletions api/neurona/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/

Expand All @@ -27,7 +26,6 @@

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
Expand All @@ -38,6 +36,8 @@
"django.contrib.messages",
"django.contrib.staticfiles",
"rest_framework",
"neuronaApp",
"neuronaLogs"
]

MIDDLEWARE = [
Expand Down Expand Up @@ -70,17 +70,21 @@

WSGI_APPLICATION = "neurona.wsgi.application"


# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases

DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
},
"logs": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "logs.sqlite3",
},
}

DATABASE_ROUTERS = ["neurona.DatabaseRouter.DatabaseRouter"]

# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators
Expand All @@ -100,7 +104,6 @@
},
]


# Internationalization
# https://docs.djangoproject.com/en/5.0/topics/i18n/

Expand All @@ -112,7 +115,6 @@

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.0/howto/static-files/

Expand Down
49 changes: 49 additions & 0 deletions api/neuronaApp/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Generated by Django 5.0.2 on 2024-02-24 19:39

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Challenges',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('challenge', models.CharField(max_length=100)),
('expires_at', models.DateTimeField()),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
],
),
migrations.CreateModel(
name='User',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('username', models.CharField(max_length=100, unique=True)),
('email', models.EmailField(max_length=100, unique=True)),
('display_name', models.CharField(max_length=100)),
('passkey_user_id', models.CharField(max_length=100)),
('about', models.TextField(blank=True, max_length=2000)),
('image_url', models.URLField(null=True)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
],
),
migrations.CreateModel(
name='RecoveryCodes',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('code', models.CharField(max_length=100)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='neuronaApp.user')),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# Generated by Django 5.0.2 on 2024-02-24 20:28

import django.db.models.deletion
import neuronaApp.models.spaces_models
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('neuronaApp', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='Spaces',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('about', models.TextField(blank=True, max_length=2000)),
('image_url', models.URLField(null=True)),
('privacy', models.IntegerField(choices=[(neuronaApp.models.spaces_models.Privacy['PUBLIC'], 0), (neuronaApp.models.spaces_models.Privacy['PROTECTED'], 1), (neuronaApp.models.spaces_models.Privacy['RESTRICTED'], 2), (neuronaApp.models.spaces_models.Privacy['PRIVATE'], 3)])),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
],
),
migrations.AlterField(
model_name='recoverycodes',
name='user',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='recovery_codes', to='neuronaApp.user'),
),
migrations.CreateModel(
name='ApiKeys',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('key', models.CharField(max_length=256)),
('expires_at', models.DateTimeField()),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='api_keys', to='neuronaApp.user')),
],
),
migrations.CreateModel(
name='Comments',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('content', models.TextField(max_length=1000)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='neuronaApp.user')),
],
),
migrations.CreateModel(
name='CommentsImages',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('image_url', models.URLField()),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('comment', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='images', to='neuronaApp.comments')),
],
),
migrations.CreateModel(
name='Posts',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=100)),
('content', models.TextField(max_length=1000)),
('is_archived', models.BooleanField(default=False)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='posts', to='neuronaApp.user')),
('space', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='posts', to='neuronaApp.spaces')),
],
),
migrations.AddField(
model_name='comments',
name='post',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='neuronaApp.posts'),
),
migrations.CreateModel(
name='PostsImages',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('image_url', models.URLField()),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='images', to='neuronaApp.posts')),
],
),
migrations.CreateModel(
name='PublicKeys',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('key', models.CharField(max_length=5000)),
('credential_id', models.CharField(max_length=100)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='public_keys', to='neuronaApp.user')),
],
),
migrations.CreateModel(
name='SpacesAccessRequests',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('space', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='access_requests', to='neuronaApp.spaces')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='access_requests', to='neuronaApp.user')),
],
),
migrations.CreateModel(
name='SpacesAdmins',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('space', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='admins', to='neuronaApp.spaces')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='admin_of', to='neuronaApp.user')),
],
),
migrations.CreateModel(
name='SpacesInvitations',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('space', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='invitations', to='neuronaApp.spaces')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='invitations', to='neuronaApp.user')),
],
),
migrations.CreateModel(
name='SpacesMembers',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('space', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='members', to='neuronaApp.spaces')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='spaces', to='neuronaApp.user')),
],
),
migrations.CreateModel(
name='Tags',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('space', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='tags', to='neuronaApp.spaces')),
],
),
migrations.AddField(
model_name='posts',
name='tag',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='posts', to='neuronaApp.tags'),
),
migrations.CreateModel(
name='Votes',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('is_upvote', models.BooleanField()),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='votes', to='neuronaApp.posts')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='votes', to='neuronaApp.user')),
],
),
]
19 changes: 19 additions & 0 deletions api/neuronaApp/migrations/0003_alter_spaces_privacy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 5.0.2 on 2024-02-27 21:28

import neuronaApp.models.spaces_models
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('neuronaApp', '0002_spaces_alter_recoverycodes_user_apikeys_comments_and_more'),
]

operations = [
migrations.AlterField(
model_name='spaces',
name='privacy',
field=models.CharField(choices=[(neuronaApp.models.spaces_models.Privacy['PUBLIC'], 'public'), (neuronaApp.models.spaces_models.Privacy['PROTECTED'], 'protected'), (neuronaApp.models.spaces_models.Privacy['RESTRICTED'], 'restricted'), (neuronaApp.models.spaces_models.Privacy['PRIVATE'], 'private')], max_length=100),
),
]
25 changes: 25 additions & 0 deletions api/neuronaApp/migrations/0004_commentsvotes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 5.0.2 on 2024-02-28 08:40

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('neuronaApp', '0003_alter_spaces_privacy'),
]

operations = [
migrations.CreateModel(
name='CommentsVotes',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('is_upvote', models.BooleanField()),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('comment', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='votes', to='neuronaApp.comments')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments_votes', to='neuronaApp.user')),
],
),
]
3 changes: 0 additions & 3 deletions api/neuronaApp/models.py

This file was deleted.

5 changes: 5 additions & 0 deletions api/neuronaApp/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .user_models import User, Challenges, RecoveryCodes, PublicKeys, ApiKeys
from .posts_models import Posts, PostsImages, Comments, CommentsImages, Votes
from .spaces_models import Spaces, Tags, SpacesMembers, SpacesAdmins, SpacesAccessRequests, SpacesInvitations

from neuronaLogs.models.logs_managing import *
Loading

0 comments on commit 90574e3

Please sign in to comment.