Skip to content

Commit 7437a23

Browse files
Matthew HolmesMatthew Holmes
authored andcommitted
adding chirpy notes
1 parent add58a3 commit 7437a23

File tree

16 files changed

+349
-0
lines changed

16 files changed

+349
-0
lines changed

Code/matthew/django/lectures/05_chirpy/chirpy_app/__init__.py

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.contrib import admin
2+
from . models import Cheep
3+
# Register your models here.
4+
5+
admin.site.register(Cheep)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class ChirpyAppConfig(AppConfig):
5+
default_auto_field = 'django.db.models.BigAutoField'
6+
name = 'chirpy_app'
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Generated by Django 4.0.3 on 2022-03-30 02:33
2+
3+
from django.conf import settings
4+
from django.db import migrations, models
5+
import django.db.models.deletion
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
initial = True
11+
12+
dependencies = [
13+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
14+
]
15+
16+
operations = [
17+
migrations.CreateModel(
18+
name='Cheep',
19+
fields=[
20+
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
21+
('chirp', models.CharField(max_length=125)),
22+
('date_published', models.DateTimeField(auto_now=True)),
23+
('deleted', models.BooleanField(default=False)),
24+
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
25+
],
26+
),
27+
]

Code/matthew/django/lectures/05_chirpy/chirpy_app/migrations/__init__.py

Whitespace-only changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from django.contrib.auth.models import User
2+
from django.db import models
3+
4+
5+
# For importing ~User objects~ (username, password, email, first_name, last_name)
6+
7+
8+
# Create your models here.
9+
10+
class Cheep(models.Model):
11+
chirp = models.CharField(max_length=125)
12+
# auto_now gives timestamp based off settings time_zone
13+
date_published = models.DateTimeField(auto_now=True)
14+
# on_delete=CASCADE - deletes all user data and associated information
15+
user = models.ForeignKey(User, on_delete=models.CASCADE)
16+
deleted = models.BooleanField(default=False)
17+
18+
def __str__(self):
19+
return f'{self.user}: {self.date_published} -- {self.chirp}'
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Chirp</title>
8+
</head>
9+
<body>
10+
<h1>Welcome to Class Kiwi Chirpy</h1>
11+
<form action="{% url 'save' %}" method="POST">
12+
{% csrf_token %} {{form}}
13+
<button>Cheep!</button>
14+
<!-- -->
15+
</form>
16+
{% for cheep in cheeps %}
17+
<ul>
18+
{% comment %} Front end deletion {% endcomment %}
19+
{% if cheep.deleted == False %} {{cheep}} {% endif %}
20+
</ul>
21+
22+
{% endfor %}
23+
24+
<!-- -->
25+
</body>
26+
</html>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
3+
"""chirpy_app URL Configuration
4+
5+
Function views
6+
1. Add an import: from my_app import views
7+
2. Add a URL to urlpatterns: path('', views.home, name='home')
8+
Class-based views
9+
1. Add an import: from other_app.views import Home
10+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
11+
Including another URLconf
12+
1. Import the include() function: from django.urls import include, path
13+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
14+
"""
15+
16+
from django.urls import path
17+
from . import views
18+
19+
20+
urlpatterns = [
21+
path('', views.index, name='index'),
22+
path('save/', views.save_cheep, name='save')
23+
]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
from django.shortcuts import render
3+
from django import forms
4+
from django.http import HttpResponseRedirect
5+
from django.urls import reverse
6+
from .models import Cheep
7+
# Create your views here.
8+
9+
10+
class NewCheepForm(forms.Form):
11+
text1 = forms.CharField(label='Cheep your thoughts here', widget=forms.TextInput(
12+
attrs={'placeholder': 'Cheep away'}), max_length=120)
13+
14+
15+
def index(request):
16+
cheeps = Cheep.objects.all().order_by('-date_published')
17+
# Backend deletion
18+
# cheeps = Cheep.objects.filter(deleted=False).order_by('-date_published')
19+
20+
return render(request, 'chirpy_app/index.html', {
21+
'form': NewCheepForm(),
22+
'cheeps': cheeps
23+
})
24+
25+
26+
def save_cheep(request):
27+
if request.method == 'POST':
28+
form = NewCheepForm(request.POST)
29+
if form.is_valid():
30+
text = form.cleaned_data['text1']
31+
user = request.user
32+
# Linking Cheep() in models.py and automatically updating chirp and user
33+
34+
cheep = Cheep() # Ex: cheep.date_published
35+
cheep.chirp = text
36+
cheep.user = user
37+
cheep.save()
38+
39+
return HttpResponseRedirect(reverse('index'))

0 commit comments

Comments
 (0)