Files @ c6b329f0cff0
Branch filter:

Location: website/www/conservancy/apps/assignment/models.py

bsturmfels
Fix validation.
from __future__ import unicode_literals

import uuid

from django.core.validators import URLValidator, ValidationError
from django.db import models
from django_countries.fields import CountryField


def validate_mutiple_urls(value):
    """Map the URLValidator() over text containing multiple URLs."""
    candidate_urls = [c.strip() for c in value.split()]
    invalid_urls = []
    # TODO: Improve this https://docs.djangoproject.com/en/3.2/ref/forms/validation/#raising-multiple-errors
    validator = URLValidator()
    for url in candidate_urls:
        try:
            validator(url)
        except ValidationError:
            invalid_urls.append(url)
    print(invalid_urls)
    if invalid_urls:
        raise ValidationError('These don\'t seem to be complete URLs:\n{}'.format('\n'.join(invalid_urls)))


class Assignment(models.Model):
    """A copyright assignment to Conservancy."""

    uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    full_name = models.CharField(max_length=255)
    email = models.EmailField('Email address (to contact you if we have questions)')
    country_of_residence = CountryField()
    repositories = models.TextField(
        'Code repositories contributed to that you\'d like to assign',
        help_text='List of URLs, one per line',
        validators=[validate_mutiple_urls],
    )
    all_emails = models.TextField(
        'All email addresses and/or names used by you to contribute to the above',
    )
    period_begins = models.DateField(
        'Assignment period begins',
    )
    period_end_type = models.CharField(
        'Time period to assign',
        max_length=50,
        choices=[
            ('all future contributions', 'all future contributions'),
            ('a specific past date', 'a specific past date'),
        ],
    )
    period_ends = models.DateField(
        'Assignment period ends (if applicable)',
        blank=True,
        null=True,
    )
    attestation_of_copyright = models.BooleanField(
        'I agree to be bound by the terms of the Copyright Assignment Agreement above, and that I own the copyright in the works defined above',
    )