Skip to content

Commit

Permalink
Started working on Module 2
Browse files Browse the repository at this point in the history
  • Loading branch information
yahyaaly151989 committed Aug 20, 2023
1 parent 1020e90 commit 51e3f20
Show file tree
Hide file tree
Showing 33 changed files with 450 additions and 0 deletions.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added Module02/mysite/blog/__pycache__/views.cpython-311.pyc
Binary file not shown.
13 changes: 13 additions & 0 deletions Module02/mysite/blog/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.contrib import admin
from .models import Post


@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ['title', 'slug', 'author', 'publish', 'status']
list_filter = ['status', 'created', 'publish', 'author']
search_fields = ['title', 'body']
prepopulated_fields = {'slug': ('title',)}
raw_id_fields = ['author']
date_hierarchy = 'publish'
ordering = ['status', 'publish']
6 changes: 6 additions & 0 deletions Module02/mysite/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'
36 changes: 36 additions & 0 deletions Module02/mysite/blog/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Generated by Django 4.2.4 on 2023-08-20 07:26

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


class Migration(migrations.Migration):

initial = True

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

operations = [
migrations.CreateModel(
name='Post',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=250)),
('slug', models.SlugField(max_length=250)),
('body', models.TextField()),
('publish', models.DateTimeField(default=django.utils.timezone.now)),
('created', models.DateTimeField(auto_now_add=True)),
('updated', models.DateTimeField(auto_now=True)),
('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'],
'indexes': [models.Index(fields=['-publish'], name='blog_post_publish_bb7600_idx')],
},
),
]
Empty file.
Binary file not shown.
Binary file not shown.
27 changes: 27 additions & 0 deletions Module02/mysite/blog/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User

class Post(models.Model):

class Status(models.TextChoices):
DRAFT = 'DF', 'Draft'
PUBLISHED = 'PB', 'Published'

title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250)
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blog_posts')
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=2, choices=Status.choices, default=Status.DRAFT)

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

def __str__(self):
return self.title
92 changes: 92 additions & 0 deletions Module02/mysite/blog/static/css/blog.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
body {
margin:0;
padding:0;
font-family:helvetica, sans-serif;
}

a {
color:#00abff;
text-decoration:none;
}

h1 {
font-weight:normal;
border-bottom:1px solid #bbb;
padding:0 0 10px 0;
}

h2 {
font-weight:normal;
margin:30px 0 0;
}

#content {
float:left;
width:60%;
padding:0 0 0 30px;
}

#sidebar {
float:right;
width:30%;
padding:10px;
background:#efefef;
height:100%;
}

p.date {
color:#ccc;
font-family: georgia, serif;
font-size: 12px;
font-style: italic;
}

/* pagination */
.pagination {
margin:40px 0;
font-weight:bold;
}

/* forms */
label {
float:left;
clear:both;
color:#333;
margin-bottom:4px;
}
input, textarea {
clear:both;
float:left;
margin:0 0 10px;
background:#ededed;
border:0;
padding:6px 10px;
font-size:12px;
}
input[type=submit] {
font-weight:bold;
background:#00abff;
color:#fff;
padding:10px 20px;
font-size:14px;
text-transform:uppercase;
}
.errorlist {
color:#cc0033;
float:left;
clear:both;
padding-left:10px;
}

/* comments */
.comment {
padding:10px;
}
.comment:nth-child(even) {
background:#efefef;
}
.comment .info {
font-weight:bold;
font-size:12px;
color:#666;
}
19 changes: 19 additions & 0 deletions Module02/mysite/blog/templates/blog/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% load static %}

<!DOCTYPE html>
<html>
<head>
<title>{% block title %}{% endblock %}</title>
<link href="{% static "css/blog.css" %}" rel="stylesheet">
</head>
<body>
<div id="content">
{% block content %}
{% endblock %}
</div>
<div id="sidebar">
<h2>My blog</h2>
<p>This is my blog.</p>
</div>
</body>
</html>
11 changes: 11 additions & 0 deletions Module02/mysite/blog/templates/blog/post/detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends "blog/base.html" %}

{% block title %}{{ post.title }}{% endblock %}

{% block content %}
<h1>{{ post.title }}</h1>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|linebreaks }}
{% endblock %}
19 changes: 19 additions & 0 deletions Module02/mysite/blog/templates/blog/post/list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% extends "blog/base.html" %}

{% block title %}My Blog{% endblock %}

{% block content %}
<h1>My Blog</h1>
{% for post in posts %}
<h2>
<a href="{% url 'blog:post_detail' post.id %}">
{{ post.title }}
</a>
</h2>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|truncatewords:30|linebreaks }}
<hr />
{% endfor %}
{% endblock %}
3 changes: 3 additions & 0 deletions Module02/mysite/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.
9 changes: 9 additions & 0 deletions Module02/mysite/blog/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.urls import path
from . import views

app_name = 'blog'

urlpatterns = [
path('', views.post_list, name='post_list'),
path('<int:id>/', views.post_detail, name='post_detail'),
]
10 changes: 10 additions & 0 deletions Module02/mysite/blog/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.shortcuts import render, get_object_or_404
from .models import Post

def post_list(request):
posts = Post.objects.all()
return render(request, 'blog/post/list.html', {'posts': posts})

def post_detail(request, id):
post = get_object_or_404(Post, id=id, status=Post.Status.PUBLISHED)
return render(request, 'blog/post/detail.html', {'post': post})
Binary file added Module02/mysite/db.sqlite3
Binary file not shown.
22 changes: 22 additions & 0 deletions Module02/mysite/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', 'mysite.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.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
16 changes: 16 additions & 0 deletions Module02/mysite/mysite/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for mysite 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.2/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

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

application = get_asgi_application()
Loading

0 comments on commit 51e3f20

Please sign in to comment.