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

Create a blog Django app #20

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# VS Code
.vscode

Comment on lines +1 to +3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few problems here:

  1. Ignoring VS Code is usually more involved and includes allow-listing certain config files that are supposed to be checked into Git: https://www.toptal.com/developers/gitignore/api/visualstudiocode
  2. Editor-specific ignores are considered out of the scope in individual repos and are typically recommended to be managed through user-global exclusions: https://sebastiandedeyne.com/setting-up-a-global-gitignore-file
  3. Finally, changing Git configurations that are not directly related to the topic of the PR, make it non-atomic. So they must be excluded from the patch and submitted as separate PRs should the need be.

### Django ###
*.log
*.pot
Expand Down
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
exclude: '^blog/migrations/'
repos:
- repo: https://github.com/asottile/add-trailing-comma
rev: v2.4.0
Expand Down
1 change: 1 addition & 0 deletions blog/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""A Django app for blog."""
1 change: 1 addition & 0 deletions blog/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Django admin panel for the blog app."""
10 changes: 10 additions & 0 deletions blog/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Configuration file of the blog app."""

from django.apps import AppConfig


class BlogConfig(AppConfig):
"""A class for the blog app configuration."""

default_auto_field = 'django.db.models.BigAutoField'
name = 'blog'
29 changes: 29 additions & 0 deletions blog/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 3.2.18 on 2023-08-22 09:14

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


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=200)),
('text', models.TextField()),
('created_date', models.DateTimeField(default=django.utils.timezone.now)),
('published_date', models.DateTimeField(blank=True, null=True)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
Empty file added blog/migrations/__init__.py
Empty file.
26 changes: 26 additions & 0 deletions blog/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Models for the blog app."""

from django.conf import settings
from django.db import models
from django.utils import timezone


class Post(models.Model):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Martolivna I want you to write tests for this model.

Create a tests/test_models.py file, import this model there, follow https://docs.djangoproject.com/en/4.2/topics/testing/overview/ to declare a test case class and make dedicated methods for every separate thing/aspect you're testing.
Generally, those tests would make an instance of this class and then, call its methods + check their return values or side effects.

Make sure to call every method you declared in this class.

Bonus: you can also integrate coverage reporting like so https://adamj.eu/tech/2019/04/30/getting-a-django-application-to-100-percent-coverage/.

"""A class to represent a post."""
webknjaz marked this conversation as resolved.
Show resolved Hide resolved

author = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
Martolivna marked this conversation as resolved.
Show resolved Hide resolved
)
title = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)

def publish(self):
"""Save the post entry."""
webknjaz marked this conversation as resolved.
Show resolved Hide resolved
self.published_date = timezone.now
self.save()

def __str__(self):
"""Return the post title."""
webknjaz marked this conversation as resolved.
Show resolved Hide resolved
return str(self.title)
1 change: 1 addition & 0 deletions blog/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Tests for the blog app."""
1 change: 1 addition & 0 deletions blog/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Views of the blog app."""
3 changes: 2 additions & 1 deletion mysite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['127.0.0.1']
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']


# Application definition
Expand All @@ -32,6 +32,7 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
]

MIDDLEWARE = [
Expand Down