diff --git a/FOSSDB_web/apps/fossdb/admin.py b/FOSSDB_web/apps/fossdb/admin.py index 8c38f3f..e69de29 100644 --- a/FOSSDB_web/apps/fossdb/admin.py +++ b/FOSSDB_web/apps/fossdb/admin.py @@ -1,3 +0,0 @@ -from django.contrib import admin - -# Register your models here. diff --git a/FOSSDB_web/apps/fossdb/forms.py b/FOSSDB_web/apps/fossdb/forms.py new file mode 100644 index 0000000..9603689 --- /dev/null +++ b/FOSSDB_web/apps/fossdb/forms.py @@ -0,0 +1,9 @@ +from django import forms + +from .models import Project + + +class ProjectForm(forms.ModelForm): + class Meta: + model = Project + fields = ["title", "description"] diff --git a/FOSSDB_web/apps/fossdb/migrations/0001_initial.py b/FOSSDB_web/apps/fossdb/migrations/0001_initial.py new file mode 100644 index 0000000..def7053 --- /dev/null +++ b/FOSSDB_web/apps/fossdb/migrations/0001_initial.py @@ -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)), + ], + ), + ] diff --git a/FOSSDB_web/apps/fossdb/models.py b/FOSSDB_web/apps/fossdb/models.py index 71a8362..e61e920 100644 --- a/FOSSDB_web/apps/fossdb/models.py +++ b/FOSSDB_web/apps/fossdb/models.py @@ -1,3 +1,15 @@ +from django.contrib.auth.models import User 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}" diff --git a/FOSSDB_web/apps/fossdb/tests.py b/FOSSDB_web/apps/fossdb/tests.py index 7ce503c..e69de29 100644 --- a/FOSSDB_web/apps/fossdb/tests.py +++ b/FOSSDB_web/apps/fossdb/tests.py @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/FOSSDB_web/apps/fossdb/urls.py b/FOSSDB_web/apps/fossdb/urls.py new file mode 100644 index 0000000..11fbdee --- /dev/null +++ b/FOSSDB_web/apps/fossdb/urls.py @@ -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"), +] diff --git a/FOSSDB_web/apps/fossdb/views.py b/FOSSDB_web/apps/fossdb/views.py index 91ea44a..35c3d5a 100644 --- a/FOSSDB_web/apps/fossdb/views.py +++ b/FOSSDB_web/apps/fossdb/views.py @@ -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}) diff --git a/FOSSDB_web/settings.py b/FOSSDB_web/settings.py index e18612d..c9ddb15 100644 --- a/FOSSDB_web/settings.py +++ b/FOSSDB_web/settings.py @@ -36,6 +36,7 @@ ALLOWED_HOSTS = config["ALLOWED_HOSTS"] # Application definition INSTALLED_APPS = [ + "fossdb", "account", "django.contrib.admin", "django.contrib.auth", @@ -86,7 +87,7 @@ DATABASES = { "USER": config["DATABASE"]["USER"], "PASSWORD": config["DATABASE"]["PASSWORD"], "HOST": config["DATABASE"]["HOST"], - "PORT": config["DATABASE"]["PORT"] + "PORT": config["DATABASE"]["PORT"], } } diff --git a/FOSSDB_web/urls.py b/FOSSDB_web/urls.py index 4e84863..9aa8c44 100644 --- a/FOSSDB_web/urls.py +++ b/FOSSDB_web/urls.py @@ -13,11 +13,16 @@ Including another URLconf 1. Import the include() function: from django.urls import include, path 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.urls import include, path urlpatterns = [ + path("", include("fossdb.urls")), path("admin/", admin.site.urls), path("", include("django.contrib.auth.urls")), path("account/", include("account.urls")), ] + +urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) diff --git a/templates/fossdb/index.html b/templates/fossdb/index.html new file mode 100644 index 0000000..c7843d8 --- /dev/null +++ b/templates/fossdb/index.html @@ -0,0 +1,13 @@ +{% extends "layout.html" %} +{% load static %} +{% block title %}{{ title }}{% endblock %} +{% block meta %}{% endblock %} +{% block content %} + {% for project in projects %} + @{{ project.author.username }} +
{{ project.title }}
+

{{ project.description }}

+ {% empty %} +

No projects yet (

+ {% endfor %} +{% endblock %} diff --git a/templates/layout.html b/templates/layout.html new file mode 100644 index 0000000..08fe90d --- /dev/null +++ b/templates/layout.html @@ -0,0 +1,15 @@ +{% load static %} + + + + + + + {% block title %}{% endblock %} + + {% block meta %}{% endblock %} + + + {% block content %}{% endblock %} + +