Skip to content

Commit

Permalink
created model for attendance, events and blogs
Browse files Browse the repository at this point in the history
  • Loading branch information
walosha committed Jan 17, 2023
1 parent a415c7e commit 0310ae9
Show file tree
Hide file tree
Showing 42 changed files with 435 additions and 24 deletions.
2 changes: 1 addition & 1 deletion account/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from rest_framework_simplejwt.views import TokenObtainPairView
from rest_framework import viewsets, status, permissions
from rest_framework import viewsets, status
from rest_framework.decorators import action
from .serializers import CreateUserSerializer, PasswordChangeSerializer, CustomObtainTokenPairSerializer, ListUserSerializer
from rest_framework.response import Response
Expand Down
Empty file added attendance/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions attendance/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.contrib import admin

from .models import Attendance

admin.site.register(Attendance)
6 changes: 6 additions & 0 deletions attendance/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class AttendanceConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'attendance'
31 changes: 31 additions & 0 deletions attendance/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 4.0.8 on 2023-01-16 22:39

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


class Migration(migrations.Migration):

initial = True

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

operations = [
migrations.CreateModel(
name='Attendance',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('title', models.CharField(max_length=100)),
('comment', models.CharField(blank=True, max_length=256)),
('eventId', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='event.event')),
],
options={
'ordering': ('-created_at',),
},
),
]
17 changes: 17 additions & 0 deletions attendance/migrations/0002_remove_attendance_title.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.0.8 on 2023-01-16 22:42

from django.db import migrations


class Migration(migrations.Migration):

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

operations = [
migrations.RemoveField(
model_name='attendance',
name='title',
),
]
Empty file.
17 changes: 17 additions & 0 deletions attendance/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.db import models
from event.models import Event
from core.models import AuditableModel

# Create your models here.


class Attendance (AuditableModel):
eventId = models.ForeignKey(
Event, on_delete=models.SET_NULL, null=True)
comment = models.CharField(max_length=256, blank=True)

class Meta:
ordering = ('-created_at',)

def __str__(self) -> str:
return self.id
3 changes: 3 additions & 0 deletions attendance/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions attendance/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
Empty file added blog/__init__.py
Empty file.
18 changes: 18 additions & 0 deletions blog/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.contrib import admin
from .models import Post, Comment

# Register your models here.


@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
list_display = ["id", 'author', 'postId', 'created_at', 'active']
list_filter = ['active', 'created_at']
search_fields = ['author', "postId", "active", 'body']


@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ["id", "author", 'author', 'created_at', 'status']
list_filter = ['status', 'created_at']
search_fields = ['author', "status", 'body']
6 changes: 6 additions & 0 deletions blog/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class BlogConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'blog'
7 changes: 7 additions & 0 deletions blog/manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.db import models
# from .models import Post


class PublishedManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(status=Post.Status.PUBLISHED)
59 changes: 59 additions & 0 deletions blog/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Generated by Django 4.0.8 on 2023-01-16 23:58

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
import uuid


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='Post',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('title', models.CharField(max_length=250)),
('slug', models.SlugField(max_length=250)),
('body', models.TextField()),
('publish', models.DateTimeField(default=django.utils.timezone.now)),
('status', models.CharField(choices=[('DF', 'Draft'), ('PB', 'Published')], default='DF', max_length=2)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='blog_posts', to=settings.AUTH_USER_MODEL)),
],
options={
'ordering': ['-publish'],
},
),
migrations.CreateModel(
name='Comment',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('body', models.TextField()),
('active', models.BooleanField(default=True)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to=settings.AUTH_USER_MODEL)),
('postId', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='blog.post')),
],
options={
'ordering': ['created_at'],
},
),
migrations.AddIndex(
model_name='post',
index=models.Index(fields=['-publish'], name='blog_post_publish_bb7600_idx'),
),
migrations.AddIndex(
model_name='comment',
index=models.Index(fields=['created_at'], name='blog_commen_created_4e025c_idx'),
),
]
18 changes: 18 additions & 0 deletions blog/migrations/0002_alter_post_slug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.0.8 on 2023-01-17 00:06

from django.db import migrations, models


class Migration(migrations.Migration):

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

operations = [
migrations.AlterField(
model_name='post',
name='slug',
field=models.SlugField(blank=True, max_length=200, null=True),
),
]
Empty file added blog/migrations/__init__.py
Empty file.
64 changes: 64 additions & 0 deletions blog/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import time
from django.db import models
from core.models import AuditableModel
from django.utils import timezone
from account.models import CustomUser

# from .manager import PublishedManager


class PublishedManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(status=Post.Status.PUBLISHED)


class Post(AuditableModel):
class Status(models.TextChoices):
DRAFT = 'DF', 'Draft'
PUBLISHED = 'PB', 'Published'
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=200, null=True, blank=True)
author = models.ForeignKey(CustomUser,
on_delete=models.CASCADE,
related_name='blog_posts')
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
status = models.CharField(max_length=2,
choices=Status.choices,
default=Status.DRAFT)
objects = models.Manager() # The default manager.
published = PublishedManager() # Our custom manager.

class Meta:
ordering = ['-publish']
indexes = [
models.Index(fields=['-publish']),
]

def __str__(self):
return self.title

def save(self, *args, **kwargs):
self.slug = str(f'{self.author.firstname} {self.author.lastname}').replace(
" ", "-").strip().lower() + '-' + str(int(time.time()))
return super().save(*args, **kwargs)


class Comment(AuditableModel):
postId = models.ForeignKey(Post,
on_delete=models.CASCADE,
related_name='comments')
author = models.ForeignKey(CustomUser,
on_delete=models.CASCADE,
related_name='comments')
body = models.TextField()
active = models.BooleanField(default=True)

class Meta:
ordering = ['created_at']
indexes = [
models.Index(fields=['created_at']),
]

def __str__(self):
return f'Comment by {self.author} on {self.postId}'
3 changes: 3 additions & 0 deletions blog/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions blog/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
11 changes: 11 additions & 0 deletions core/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import uuid
from django.db import models


class AuditableModel(models.Model):
id = models.UUIDField(primary_key=True, editable=False, default=uuid.uuid4)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

class Meta:
abstract = True
51 changes: 38 additions & 13 deletions core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""

import os
from pathlib import Path
from datetime import timedelta
import dj_database_url
from decouple import config

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand All @@ -26,8 +28,21 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []
ALLOWED_HOSTS = [
"127.0.0.1", "0.0.0.0", "localhost", "api", "chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop"]
INTERNAL_IPS = ["127.0.0.1",
'chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop']
if DEBUG:
import os # only if you haven't already imported this
import socket # only if you haven't already imported this

hostname, _, ips = socket.gethostbyname_ex(socket.gethostname())
INTERNAL_IPS = [ip[:-1] + '1' for ip in ips] + ['127.0.0.1', '10.0.2.2']

USE_X_FORWARDED_HOST = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOW_ALL_ORIGINS = True

# Application definition

Expand All @@ -38,10 +53,15 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders',
'rest_framework',
'rest_framework.authtoken',
'rest_framework_simplejwt.token_blacklist',
"account"
'import_export',
"account",
"attendance",
"event",
"blog"

]
# AUTH_USER_MODEL = "users.CustomUser"
Expand All @@ -52,6 +72,8 @@
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
Expand All @@ -78,17 +100,20 @@
WSGI_APPLICATION = 'core.wsgi.application'


# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases
# DATABASES = {
# 'default': dj_database_url.config(default=config('DATABASE_URL'))
# }

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
"default": {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '5432',
}
}


# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators

Expand Down Expand Up @@ -137,9 +162,9 @@
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication'
],
"DEFAULT_PERMISSION_CLASSES": [
"rest_framework.permissions.IsAuthenticatedOrReadOnly"
]
# "DEFAULT_PERMISSION_CLASSES": [
# "rest_framework.permissions.IsAuthenticatedOrReadOnly"
# ]

}

Expand Down
Loading

0 comments on commit 0310ae9

Please sign in to comment.