2022-05-13 05:11:35 +02:00
|
|
|
import re
|
|
|
|
|
2022-05-13 03:04:13 +02:00
|
|
|
from django import forms
|
|
|
|
from . import models
|
|
|
|
|
2022-05-13 05:11:35 +02:00
|
|
|
|
|
|
|
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 ScratchURLField(forms.URLField):
|
|
|
|
def clean(self, value):
|
|
|
|
url = super().clean(value)
|
|
|
|
scratch_id = get_scratch_id_from_url(url)
|
|
|
|
return scratch_id
|
|
|
|
|
|
|
|
|
2022-05-13 03:04:13 +02:00
|
|
|
class ScratchProjectAddForm(forms.ModelForm):
|
2022-05-13 05:11:35 +02:00
|
|
|
url = ScratchURLField(label='Lien Scratch du projet (URL)')
|
2022-05-13 03:04:13 +02:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
fields = ('name', 'author_name', 'url')
|
|
|
|
model = models.ScratchProject
|