diff --git a/Code/Jose/Django/lab02/blog/__init__.py b/Code/Jose/Django/lab02/blog/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/Jose/Django/lab02/blog/asgi.py b/Code/Jose/Django/lab02/blog/asgi.py new file mode 100644 index 00000000..ae299fdb --- /dev/null +++ b/Code/Jose/Django/lab02/blog/asgi.py @@ -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() diff --git a/Code/Jose/Django/lab02/blog/settings.py b/Code/Jose/Django/lab02/blog/settings.py new file mode 100644 index 00000000..a87badb0 --- /dev/null +++ b/Code/Jose/Django/lab02/blog/settings.py @@ -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' diff --git a/Code/Jose/Django/lab02/blog/urls.py b/Code/Jose/Django/lab02/blog/urls.py new file mode 100644 index 00000000..e78ba70d --- /dev/null +++ b/Code/Jose/Django/lab02/blog/urls.py @@ -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')), +] \ No newline at end of file diff --git a/Code/Jose/Django/lab02/blog/wsgi.py b/Code/Jose/Django/lab02/blog/wsgi.py new file mode 100644 index 00000000..0e0b14d9 --- /dev/null +++ b/Code/Jose/Django/lab02/blog/wsgi.py @@ -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() diff --git a/Code/Jose/Django/lab02/blog_app/__init__.py b/Code/Jose/Django/lab02/blog_app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/Jose/Django/lab02/blog_app/admin.py b/Code/Jose/Django/lab02/blog_app/admin.py new file mode 100644 index 00000000..1765c7c1 --- /dev/null +++ b/Code/Jose/Django/lab02/blog_app/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from .models import BlogPost +# Register your models here. + +admin.site.register(BlogPost) \ No newline at end of file diff --git a/Code/Jose/Django/lab02/blog_app/apps.py b/Code/Jose/Django/lab02/blog_app/apps.py new file mode 100644 index 00000000..90d811d3 --- /dev/null +++ b/Code/Jose/Django/lab02/blog_app/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class BlogAppConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'blog_app' diff --git a/Code/Jose/Django/lab02/blog_app/forms.py b/Code/Jose/Django/lab02/blog_app/forms.py new file mode 100644 index 00000000..e802dfba --- /dev/null +++ b/Code/Jose/Django/lab02/blog_app/forms.py @@ -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 \ No newline at end of file diff --git a/Code/Jose/Django/lab02/blog_app/migrations/0001_initial.py b/Code/Jose/Django/lab02/blog_app/migrations/0001_initial.py new file mode 100644 index 00000000..56a67ee8 --- /dev/null +++ b/Code/Jose/Django/lab02/blog_app/migrations/0001_initial.py @@ -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)), + ], + ), + ] diff --git a/Code/Jose/Django/lab02/blog_app/migrations/0002_initial.py b/Code/Jose/Django/lab02/blog_app/migrations/0002_initial.py new file mode 100644 index 00000000..fe001785 --- /dev/null +++ b/Code/Jose/Django/lab02/blog_app/migrations/0002_initial.py @@ -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), + ), + ] diff --git a/Code/Jose/Django/lab02/blog_app/migrations/__init__.py b/Code/Jose/Django/lab02/blog_app/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/Code/Jose/Django/lab02/blog_app/models.py b/Code/Jose/Django/lab02/blog_app/models.py new file mode 100644 index 00000000..b1cf5adf --- /dev/null +++ b/Code/Jose/Django/lab02/blog_app/models.py @@ -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}' \ No newline at end of file diff --git a/Code/Jose/Django/lab02/blog_app/tests.py b/Code/Jose/Django/lab02/blog_app/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/Code/Jose/Django/lab02/blog_app/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/Code/Jose/Django/lab02/blog_app/urls.py b/Code/Jose/Django/lab02/blog_app/urls.py new file mode 100644 index 00000000..cb630f98 --- /dev/null +++ b/Code/Jose/Django/lab02/blog_app/urls.py @@ -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'), +] \ No newline at end of file diff --git a/Code/Jose/Django/lab02/blog_app/views.py b/Code/Jose/Django/lab02/blog_app/views.py new file mode 100644 index 00000000..3e90e6d8 --- /dev/null +++ b/Code/Jose/Django/lab02/blog_app/views.py @@ -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")) \ No newline at end of file diff --git a/Code/Jose/Django/lab02/manage.py b/Code/Jose/Django/lab02/manage.py new file mode 100644 index 00000000..9505045b --- /dev/null +++ b/Code/Jose/Django/lab02/manage.py @@ -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() diff --git a/Code/Jose/Django/lab02/static/index.css b/Code/Jose/Django/lab02/static/index.css new file mode 100644 index 00000000..e69de29b diff --git a/Code/Jose/Django/lab02/templates/base.html b/Code/Jose/Django/lab02/templates/base.html new file mode 100644 index 00000000..3b8c6298 --- /dev/null +++ b/Code/Jose/Django/lab02/templates/base.html @@ -0,0 +1,41 @@ +{% load static %} + + + +
+ + + + + +{{blog.body}}
+| First: | ++ {% if user.first_name %} {{user.first_name}} {% else %} Not Provided + {% endif %} + | +|
|---|---|---|
| Last: | ++ {% if user.last_name %} {{user.last_name}} {% else %} Not Provided + {% endif %} + | +|
| Joined: | +{{user.date_joined|date}} | +|