Merge branch 'development' into login

This commit is contained in:
Kristofers Solo 2023-03-27 21:06:08 +03:00
commit 8df44c7ee9
11 changed files with 116 additions and 10 deletions

View File

@ -1,3 +0,0 @@
from django.contrib import admin
# Register your models here.

View File

@ -0,0 +1,9 @@
from django import forms
from .models import Project
class ProjectForm(forms.ModelForm):
class Meta:
model = Project
fields = ["title", "description"]

View File

@ -0,0 +1,27 @@
# Generated by Django 4.1 on 2023-03-27 17:46
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='Project',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=255)),
('description', models.TextField()),
('create_date', models.DateTimeField(auto_now_add=True)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]

View File

@ -1,3 +1,15 @@
from django.contrib.auth.models import User
from django.db import models from django.db import models
# Create your models here.
class Project(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=255)
description = models.TextField()
create_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.repo_name
def get_absolute_url(self):
return f"/projects/{self.id}"

View File

@ -1,3 +0,0 @@
from django.test import TestCase
# Create your tests here.

View File

@ -0,0 +1,8 @@
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("add", views.add_project, name="add-project"),
]

View File

@ -1,3 +1,25 @@
from django.shortcuts import render from django.contrib.auth.decorators import login_required, permission_required
from django.shortcuts import redirect, render
# Create your views here. from .forms import ProjectForm
from .models import Project
def index(request):
projects = Project.objects.all()
return render(request, "fossdb/index.html", {"title": "FOSSDB", "projects": projects})
@login_required(login_url="account/login/")
@permission_required("fossdb.add_post)", login_url="account/login/", raise_exception=True)
def add_project(request):
if request.method == "POST":
project = ProjectForm(request.POST)
if project.is_valid():
post = project.save(commit=False)
post.author = request.user
post.save()
return redirect("")
else:
project = ProjectForm()
return render(request, "main/create_project.html", {"title": "Add project", "form": project})

View File

@ -36,6 +36,7 @@ ALLOWED_HOSTS = config["ALLOWED_HOSTS"]
# Application definition # Application definition
INSTALLED_APPS = [ INSTALLED_APPS = [
"fossdb",
"account", "account",
"django.contrib.admin", "django.contrib.admin",
"django.contrib.auth", "django.contrib.auth",
@ -86,7 +87,7 @@ DATABASES = {
"USER": config["DATABASE"]["USER"], "USER": config["DATABASE"]["USER"],
"PASSWORD": config["DATABASE"]["PASSWORD"], "PASSWORD": config["DATABASE"]["PASSWORD"],
"HOST": config["DATABASE"]["HOST"], "HOST": config["DATABASE"]["HOST"],
"PORT": config["DATABASE"]["PORT"] "PORT": config["DATABASE"]["PORT"],
} }
} }

View File

@ -13,11 +13,16 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path 1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path("blog/", include("blog.urls")) 2. Add a URL to urlpatterns: path("blog/", include("blog.urls"))
""" """
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin from django.contrib import admin
from django.urls import include, path from django.urls import include, path
urlpatterns = [ urlpatterns = [
path("", include("fossdb.urls")),
path("admin/", admin.site.urls), path("admin/", admin.site.urls),
path("", include("django.contrib.auth.urls")), path("", include("django.contrib.auth.urls")),
path("account/", include("account.urls")), path("account/", include("account.urls")),
] ]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

View File

@ -0,0 +1,13 @@
{% extends "layout.html" %}
{% load static %}
{% block title %}{{ title }}{% endblock %}
{% block meta %}{% endblock %}
{% block content %}
{% for project in projects %}
<b>@{{ project.author.username }}</b>
<h5>{{ project.title }}</h5>
<p>{{ project.description }}</p>
{% empty %}
<p>No projects yet (</p>
{% endfor %}
{% endblock %}

15
templates/layout.html Normal file
View File

@ -0,0 +1,15 @@
{% load static %}
<!DOCTYPE html>
<html lang="en" data-color-mode="dark">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>
{% block title %}{% endblock %}
</title>
{% block meta %}{% endblock %}
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>