commit 7a053983a7ffae62eafdad3c7ddf8f1f113afff3 Author: Antoine Martin Date: Fri May 13 03:04:13 2022 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e5eb3d1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/env/ +__pycache__/ +*.sqlite3 diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..f3e1dad --- /dev/null +++ b/manage.py @@ -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', 'scratch.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() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..58d1a9c --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +django>=4.0,<5.0 diff --git a/scratch/__init__.py b/scratch/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/scratch/asgi.py b/scratch/asgi.py new file mode 100644 index 0000000..0db2c10 --- /dev/null +++ b/scratch/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for scratch 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.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'scratch.settings') + +application = get_asgi_application() diff --git a/scratch/settings.py b/scratch/settings.py new file mode 100644 index 0000000..1f811d3 --- /dev/null +++ b/scratch/settings.py @@ -0,0 +1,124 @@ +""" +Django settings for scratch project. + +Generated by 'django-admin startproject' using Django 4.0.4. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.0/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-=pp_ny!1v^o7_mt15%ls=6y5i%isdz6nl@h9pz9d*%&_d&&2&2' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'scratch_show.apps.ScratchShowConfig', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'scratch.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'scratch.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/4.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.0/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/scratch/urls.py b/scratch/urls.py new file mode 100644 index 0000000..08c1215 --- /dev/null +++ b/scratch/urls.py @@ -0,0 +1,22 @@ +"""scratch URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +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.contrib import admin +from django.urls import include, path + +urlpatterns = [ + path('admin/', admin.site.urls), + path('', include('scratch_show.urls')) +] diff --git a/scratch/wsgi.py b/scratch/wsgi.py new file mode 100644 index 0000000..a0a7758 --- /dev/null +++ b/scratch/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for scratch project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'scratch.settings') + +application = get_wsgi_application() diff --git a/scratch_show/__init__.py b/scratch_show/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/scratch_show/admin.py b/scratch_show/admin.py new file mode 100644 index 0000000..9433c8f --- /dev/null +++ b/scratch_show/admin.py @@ -0,0 +1,6 @@ +from django.contrib import admin + +from .models import Event, ScratchProject + +admin.site.register(Event) +admin.site.register(ScratchProject) diff --git a/scratch_show/apps.py b/scratch_show/apps.py new file mode 100644 index 0000000..2578602 --- /dev/null +++ b/scratch_show/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ScratchShowConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'scratch_show' diff --git a/scratch_show/forms.py b/scratch_show/forms.py new file mode 100644 index 0000000..ec4ebd8 --- /dev/null +++ b/scratch_show/forms.py @@ -0,0 +1,9 @@ +from django import forms +from . import models + +class ScratchProjectAddForm(forms.ModelForm): + url = forms.URLField(label='Lien Scratch du projet (URL)') + + class Meta: + fields = ('name', 'author_name', 'url') + model = models.ScratchProject diff --git a/scratch_show/migrations/0001_initial.py b/scratch_show/migrations/0001_initial.py new file mode 100644 index 0000000..79f9a75 --- /dev/null +++ b/scratch_show/migrations/0001_initial.py @@ -0,0 +1,35 @@ +# Generated by Django 4.0.4 on 2022-05-13 00:55 + +import datetime +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Event', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=128, verbose_name="Nom de l'évènement")), + ('date', models.DateField(default=datetime.date.today)), + ('accept_projects', models.BooleanField(default=True)), + ], + ), + migrations.CreateModel( + name='ScratchProject', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=128, verbose_name='Nom du projet')), + ('author_name', models.CharField(max_length=128, verbose_name='Auteur')), + ('project_id', models.PositiveBigIntegerField(unique=True, verbose_name='Scratch project id')), + ('event', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='scratch_show.event')), + ], + ), + ] diff --git a/scratch_show/migrations/__init__.py b/scratch_show/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/scratch_show/models.py b/scratch_show/models.py new file mode 100644 index 0000000..c0edc55 --- /dev/null +++ b/scratch_show/models.py @@ -0,0 +1,19 @@ +from datetime import date + +from django.db import models +from django.urls import reverse + +class Event(models.Model): + name = models.CharField("Nom de l'évènement", max_length=128) + date = models.DateField(default=date.today) + accept_projects = models.BooleanField(default=True) + + +class ScratchProject(models.Model): + name = models.CharField('Nom du projet', max_length=128) + author_name = models.CharField('Auteur', max_length=128) + project_id = models.PositiveBigIntegerField('Scratch project id', unique=True) + event = models.ForeignKey(Event, on_delete=models.CASCADE) + + def get_absolute_url(self): + return reverse('scratch_show:project-detail', kwargs={'pk' : self.pk}) diff --git a/scratch_show/templates/scratch_show/scratchproject_detail.html b/scratch_show/templates/scratch_show/scratchproject_detail.html new file mode 100644 index 0000000..42e1b31 --- /dev/null +++ b/scratch_show/templates/scratch_show/scratchproject_detail.html @@ -0,0 +1,4 @@ +

{{ object.name }}

+

Créateur : {{ object.author_name }}

+ + diff --git a/scratch_show/templates/scratch_show/scratchproject_form.html b/scratch_show/templates/scratch_show/scratchproject_form.html new file mode 100644 index 0000000..256405a --- /dev/null +++ b/scratch_show/templates/scratch_show/scratchproject_form.html @@ -0,0 +1,4 @@ +
{% csrf_token %} + {{ form.as_p }} + +
diff --git a/scratch_show/tests.py b/scratch_show/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/scratch_show/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/scratch_show/urls.py b/scratch_show/urls.py new file mode 100644 index 0000000..f6304ab --- /dev/null +++ b/scratch_show/urls.py @@ -0,0 +1,9 @@ +from django.urls import path +from . import views + +app_name = 'scratch_show' + +urlpatterns = [ + path('add', views.ScratchProjectAddView.as_view(), name='project-add'), + path('project/', views.ScratchProjectDetailView.as_view(), name='project-detail'), +] diff --git a/scratch_show/views.py b/scratch_show/views.py new file mode 100644 index 0000000..db76a11 --- /dev/null +++ b/scratch_show/views.py @@ -0,0 +1,34 @@ +import re + +from django.shortcuts import get_object_or_404 +from django.utils import timezone +from django.views.generic.edit import CreateView +from django.views.generic.detail import DetailView + +from . import forms, models + + +SCRATCH_ID_REGEX = re.compile(r'https://scratch\.mit\.edu/projects/([0-9]+)') + +def get_scratch_id_from_url(url): + match = SCRATCH_ID_REGEX.search(url) + return int(match.group(1)) + + +class ScratchProjectAddView(CreateView): + form_class = forms.ScratchProjectAddForm + template_name = 'scratch_show/scratchproject_form.html' + + def dispatch(self, request, *args, **kwargs): + self.event = get_object_or_404(models.Event, date=timezone.now().date(), accept_projects=True) + return super().dispatch(request, *args, **kwargs) + + def form_valid(self, form): + url = form.cleaned_data['url'] + form.instance.project_id = get_scratch_id_from_url(url) + form.instance.event = self.event + return super().form_valid(form) + + +class ScratchProjectDetailView(DetailView): + model = models.ScratchProject