Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Austen django 02 submission #333

Merged
merged 10 commits into from
Mar 7, 2022
22 changes: 22 additions & 0 deletions Code/Austen/django-02/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"workbench.colorCustomizations": {
"activityBar.activeBackground": "#1f6fd0",
"activityBar.activeBorder": "#ee90bb",
"activityBar.background": "#1f6fd0",
"activityBar.foreground": "#e7e7e7",
"activityBar.inactiveForeground": "#e7e7e799",
"activityBarBadge.background": "#ee90bb",
"activityBarBadge.foreground": "#15202b",
"sash.hoverBorder": "#1f6fd0",
"statusBar.background": "#1857a4",
"statusBar.foreground": "#e7e7e7",
"statusBarItem.hoverBackground": "#1f6fd0",
"statusBarItem.remoteBackground": "#1857a4",
"statusBarItem.remoteForeground": "#e7e7e7",
"titleBar.activeBackground": "#1857a4",
"titleBar.activeForeground": "#e7e7e7",
"titleBar.inactiveBackground": "#1857a499",
"titleBar.inactiveForeground": "#e7e7e799"
},
"peacock.color": "#1857a4"
}
Empty file.
6 changes: 6 additions & 0 deletions Code/Austen/django-02/BLOGS/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.contrib import admin
from .models import *
# Register your models here.

admin.site.register(Blog)
admin.site.register(Post)
6 changes: 6 additions & 0 deletions Code/Austen/django-02/BLOGS/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class BlogsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'BLOGS'
27 changes: 27 additions & 0 deletions Code/Austen/django-02/BLOGS/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from django.forms import *
from . import models
from datetime import datetime
class post:
class new(ModelForm):
class Meta:
model = models.Post
fields = ['title', 'content', 'public', 'edited']
widgets = {'content': Textarea(attrs={'cols': 25, 'rows': 5}), 'edited': HiddenInput()}

def save(self, blog, commit=True):
new_post = super(post.new, self).save(commit=False)
title = self.cleaned_data['title']
content = self.cleaned_data['content']
public = self.cleaned_data['public']
if commit:
new_post = models.Post.objects.create(blog=blog, title=title, content=content, public=public)
return new_post
def update(self, selected_post, commit=True):
updated_post = models.Post.objects.get(title=selected_post.title)
updated_post.title = self.cleaned_data['title']
updated_post.content = self.cleaned_data['content']
updated_post.public = self.cleaned_data['public']
updated_post.edited = datetime.now()
if commit:
updated_post.save()
return updated_post
33 changes: 33 additions & 0 deletions Code/Austen/django-02/BLOGS/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 4.0.2 on 2022-02-25 05:18

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),
]

operations = [
migrations.CreateModel(
name='Blog',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='Post',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=24)),
('content', models.CharField(max_length=100)),
('blog', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='BLOGS.blog')),
],
),
]
18 changes: 18 additions & 0 deletions Code/Austen/django-02/BLOGS/migrations/0002_blog_posts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.0.2 on 2022-02-27 19:49

from django.db import migrations, models


class Migration(migrations.Migration):

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

operations = [
migrations.AddField(
model_name='blog',
name='posts',
field=models.ManyToManyField(blank=True, related_name='blogs', to='BLOGS.Post'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 4.0.2 on 2022-03-05 22:50

from django.db import migrations, models
import django.utils.timezone


class Migration(migrations.Migration):

dependencies = [
('BLOGS', '0002_blog_posts'),
]

operations = [
migrations.AddField(
model_name='post',
name='created',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='post',
name='edited',
field=models.DateTimeField(auto_now=True),
),
migrations.AddField(
model_name='post',
name='public',
field=models.BooleanField(default=True),
),
]
Empty file.
24 changes: 24 additions & 0 deletions Code/Austen/django-02/BLOGS/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.db.models import *
from django.contrib.auth import get_user_model
from datetime import datetime

# Create your models here.


class Post(Model):
blog = ForeignKey('Blog', on_delete=CASCADE)
title = CharField(max_length=24)
content = CharField(max_length=100)
created = DateTimeField(auto_now_add=True)
edited = DateTimeField(default=datetime.now())
public = BooleanField(default=True)

def __str__(self):
return f'{self.title}'

class Blog(Model):
user = OneToOneField(get_user_model(), on_delete=CASCADE)
posts = ManyToManyField(Post, related_name='blogs', blank=True)

def __str__(self):
return f'{self.user}\'s blog'
3 changes: 3 additions & 0 deletions Code/Austen/django-02/BLOGS/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.
11 changes: 11 additions & 0 deletions Code/Austen/django-02/BLOGS/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.urls import path
from . import views as view

app_name = 'BLOG'
urlpatterns = [
path('browse/', view.blog.browse, name='browse'),
path('<str:username>/new/', view.post.new, name='new'),
path('<str:username>/posts/', view.blog.index, name='index'),
path('<str:username>/posts/<str:post_title>/', view.post.view, name='view'),
path('<str:username>/posts/<str:post_title>/edit', view.post.update, name='edit_post'),
]
66 changes: 66 additions & 0 deletions Code/Austen/django-02/BLOGS/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from django.contrib.auth import get_user_model
from django.shortcuts import render, redirect, get_object_or_404
from django.urls import reverse
from . import forms
from . import models as model

# Create your views here.

class post:
def view(REQUEST, username, post_title):
user = get_object_or_404(get_user_model(), username=username)
blog = model.Blog.objects.get(user=user)
selected = model.Post.objects.get(title=post_title)
context = {
'posts': model.Post.objects.filter(blog=blog),
'selected': selected,
'selected_user': user
}
print(context)
return render(REQUEST, 'blog.html', context)
def new(REQUEST, username):
context = {
'name': 'new post',
'form': forms.post.new(),
'url': 'BLOG:new',
'username': username
}
if REQUEST.POST:
form = forms.post.new(REQUEST.POST, instance=REQUEST.user)
if form.is_valid():
blog = model.Blog.objects.get(user=REQUEST.user)
new_post = form.save(blog=blog)

return redirect(reverse('BLOG:index', kwargs={'username': REQUEST.user.username}))
return render(REQUEST, 'form.html', context)
def update(REQUEST, username, post_title):
selected_post = model.Post.objects.get(title=post_title)
context = {
'name': 'edit post',
'form': forms.post.new(initial={'title': selected_post.title, 'content': selected_post.content}),
'url': 'BLOG:edit_post',
'username': username,
'selected_post': selected_post
}
if REQUEST.POST:
data = REQUEST.POST
form = forms.post.new(data)
if form.is_valid():
form.update(selected_post)
return redirect(reverse('BLOG:browse'))
return render(REQUEST, 'form.html', context)
class blog:
def index(REQUEST, username):
user = get_object_or_404(get_user_model(), username=username)
blog = model.Blog.objects.get(user=user)
context = {
'posts': model.Post.objects.filter(blog=blog),
'selected_user': user
}

return render(REQUEST, 'blog.html', context)
def browse(REQUEST):
context = {
'blogs': model.Blog.objects.all()
}
return render(REQUEST, 'index.html', context)
Empty file.
3 changes: 3 additions & 0 deletions Code/Austen/django-02/HOME/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions Code/Austen/django-02/HOME/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class HomeConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'HOME'
Empty file.
3 changes: 3 additions & 0 deletions Code/Austen/django-02/HOME/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions Code/Austen/django-02/HOME/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.
6 changes: 6 additions & 0 deletions Code/Austen/django-02/HOME/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.urls import path
from . import views as view
app_name = 'HOME'
urlpatterns = [
path('', view.home, name='index')
]
6 changes: 6 additions & 0 deletions Code/Austen/django-02/HOME/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.shortcuts import render

# Create your views here.

def home(REQUEST):
return render(REQUEST, 'index.html')
Empty file.
3 changes: 3 additions & 0 deletions Code/Austen/django-02/USERS/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions Code/Austen/django-02/USERS/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class UsersConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'USERS'
33 changes: 33 additions & 0 deletions Code/Austen/django-02/USERS/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from django.forms import *
from django.contrib.auth import get_user_model
from BLOGS.models import Blog
class user:
class login(ModelForm):
class Meta:
model = get_user_model()
fields = ['username', 'password']
widgets = {'password': PasswordInput()}

class register(ModelForm):
class Meta:
model = get_user_model()
fields = ['username', 'email', 'password']
def save(self, commit=True):
new_user = super(user.register, self).save(commit=False)
new_user.username = self.cleaned_data['username']
new_user.set_password(self.cleaned_data['password'])
if commit:
new_user.save()
user_blog = Blog.objects.create(user=new_user)
return new_user

class update:
class email(ModelForm):
class Meta:
model = get_user_model()
fields = ['email']

class password(ModelForm):
class Meta:
model = get_user_model()
fields = ['password']
Empty file.
3 changes: 3 additions & 0 deletions Code/Austen/django-02/USERS/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions Code/Austen/django-02/USERS/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.
13 changes: 13 additions & 0 deletions Code/Austen/django-02/USERS/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.urls import path
from . import views as view

app_name = 'USER'
urlpatterns = [
path('login', view.login.form, name='login'),
path('auth', view.login.auth, name='auth'),
path('register', view.login.register, name='register'),
path('logout', view.login.logout, name='logout'),
path('view', view.profile.view, name='view'),
path('update/email', view.profile.update, name='update_email'),
path('update/password', view.login.update, name='update_password'),
]
Loading