Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
16 changes: 16 additions & 0 deletions Code/Jose/Django/lab02/blog/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for blog project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'blog.settings')

application = get_asgi_application()
133 changes: 133 additions & 0 deletions Code/Jose/Django/lab02/blog/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
"""
Django settings for blog project.

Generated by 'django-admin startproject' using Django 4.0.3.

For more information on this file, see
https://docs.djangoproject.com/en/4.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""

from pathlib import Path

# 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/4.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-v6boh*mnoyyms%oay5n+%@xnt9_c09+delf+u==35$_xdzx(io'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog_app',
'users_app'
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'blog.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'blog.wsgi.application'


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

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


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

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'America/Los_Angeles'

USE_I18N = True

USE_TZ = True


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

STATIC_URL = 'static/'
STATICFILES_DIRS = [str(BASE_DIR.joinpath('static'))]

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# redefine the default auth user model from our users app
AUTH_USER_MODEL = 'users_app.User'

# tell django where to redirect if not logged in
# when a view function with @login_required is visited
LOGIN_URL = '/users/login'
23 changes: 23 additions & 0 deletions Code/Jose/Django/lab02/blog/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""blog URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('users_app.urls')),
path('blog/', include('blog_app.urls')),
]
16 changes: 16 additions & 0 deletions Code/Jose/Django/lab02/blog/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for blog project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'blog.settings')

application = get_wsgi_application()
Empty file.
5 changes: 5 additions & 0 deletions Code/Jose/Django/lab02/blog_app/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.contrib import admin
from .models import BlogPost
# Register your models here.

admin.site.register(BlogPost)
6 changes: 6 additions & 0 deletions Code/Jose/Django/lab02/blog_app/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class BlogAppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'blog_app'
19 changes: 19 additions & 0 deletions Code/Jose/Django/lab02/blog_app/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django import forms
from .models import BlogPost

class BlogForm(forms.ModelForm):
class Meta:
model = BlogPost


fields = [
'title',
'body'
]


widgets = {
'title': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Enter title'}),
'body': forms.Textarea(attrs={'class': 'form-control', 'placeholder': 'Enter text'})
}
# Create blog attached attributes
25 changes: 25 additions & 0 deletions Code/Jose/Django/lab02/blog_app/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.0.3 on 2022-03-10 11:15

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='BlogPost',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=200)),
('body', models.TextField()),
('public', models.BooleanField(blank=True, null=True)),
('date_created', models.DateTimeField(auto_now_add=True)),
('date_edited', models.DateTimeField(auto_now_add=True)),
],
),
]
23 changes: 23 additions & 0 deletions Code/Jose/Django/lab02/blog_app/migrations/0002_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.0.3 on 2022-03-10 11:15

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


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('blog_app', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='blogpost',
name='user',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='user', to=settings.AUTH_USER_MODEL),
),
]
Empty file.
15 changes: 15 additions & 0 deletions Code/Jose/Django/lab02/blog_app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.db import models
from users_app.models import User

# Create your models here.

class BlogPost(models.Model):
title = models.CharField(max_length=200)
body = models.TextField()
user = models.ForeignKey(User(), on_delete=models.CASCADE, related_name='user')
public = models.BooleanField(null=True, blank=True)
date_created = models.DateTimeField(auto_now_add=True)
date_edited = models.DateTimeField(auto_now_add=True)

def __str__(self):
return f'{self.title}'
3 changes: 3 additions & 0 deletions Code/Jose/Django/lab02/blog_app/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.
10 changes: 10 additions & 0 deletions Code/Jose/Django/lab02/blog_app/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.urls import path
from . import views

# Create your views here.

app_name = 'blog_app'
urlpatterns = [
path('home/', views.home, name='home'),
path('create/', views.create, name='create'),
]
28 changes: 28 additions & 0 deletions Code/Jose/Django/lab02/blog_app/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from django.shortcuts import redirect, render
from django.urls import reverse
from django.contrib.auth.decorators import login_required
from .models import BlogPost
from .forms import BlogForm
# Create your views here.


def home(request):
blogs = BlogPost.objects.all().order_by("-date_created")
context = {"blogs": blogs}
return render(request, "home.html", context)


@login_required
def create(request):
if request.method == "GET":
form = BlogForm()
return render(request, "create.html", {"form": form})

elif request.method == "POST":
form = BlogForm(request.POST)
if form.is_valid():
new_blog = form.save(commit=False)
new_blog.user = request.user
new_blog.save()

return redirect(reverse("blog_app:home"))
22 changes: 22 additions & 0 deletions Code/Jose/Django/lab02/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'blog.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == '__main__':
main()
Empty file.
Loading