Initial commit

This commit is contained in:
Antoine Martin 2022-05-13 03:04:13 +02:00
commit 7a053983a7
20 changed files with 333 additions and 0 deletions

0
scratch_show/__init__.py Normal file
View file

6
scratch_show/admin.py Normal file
View file

@ -0,0 +1,6 @@
from django.contrib import admin
from .models import Event, ScratchProject
admin.site.register(Event)
admin.site.register(ScratchProject)

6
scratch_show/apps.py Normal file
View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class ScratchShowConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'scratch_show'

9
scratch_show/forms.py Normal file
View file

@ -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

View file

@ -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')),
],
),
]

View file

19
scratch_show/models.py Normal file
View file

@ -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})

View file

@ -0,0 +1,4 @@
<h1>{{ object.name }}</h1>
<p>Créateur : {{ object.author_name }}</p>
<iframe src="https://scratch.mit.edu/projects/{{ object.project_id }}/embed" allowtransparency="true" width="485" height="402" frameborder="0" scrolling="no" allowfullscreen></iframe>

View file

@ -0,0 +1,4 @@
<form method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save">
</form>

3
scratch_show/tests.py Normal file
View file

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

9
scratch_show/urls.py Normal file
View file

@ -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/<int:pk>', views.ScratchProjectDetailView.as_view(), name='project-detail'),
]

34
scratch_show/views.py Normal file
View file

@ -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