Skip to content
Snippets Groups Projects
models.py 2.96 KiB
Newer Older
  • Learn to ignore specific revisions
  • George Hickman's avatar
    George Hickman committed
    import decimal
    
    
    from django.conf import settings
    
    from django.contrib.contenttypes.fields import GenericRelation
    
    George Hickman's avatar
    George Hickman committed
    from django.core.exceptions import ValidationError
    from django.core.validators import MinValueValidator
    
    from django.db import models
    from django.urls import reverse
    
    George Hickman's avatar
    George Hickman committed
    from django.utils.translation import ugettext as _
    
    
    
    class Project(models.Model):
    
        lead = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.SET_NULL, related_name='lead_projects')
    
        submission = models.OneToOneField("funds.ApplicationSubmission", on_delete=models.CASCADE)
    
        user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, related_name='owned_projects')
    
        title = models.TextField()
    
    George Hickman's avatar
    George Hickman committed
        contact_legal_name = models.TextField(_('Person or Organisation name'), default='')
        contact_email = models.TextField(_('Email'), default='')
        contact_address = models.TextField(_('Address'), default='')
        contact_phone = models.TextField(_('Phone'), default='')
        value = models.DecimalField(
            default=0,
            max_digits=10,
            decimal_places=2,
            validators=[MinValueValidator(decimal.Decimal('0.01'))],
        )
        proposed_start = models.DateTimeField(_('Proposed Start Date'), null=True)
        proposed_end = models.DateTimeField(_('Proposed End Date'), null=True)
    
        # tracks updates to the Projects fields via the Project Application Form.
        user_has_updated_details = models.BooleanField(default=False)
    
        activities = GenericRelation(
            'activity.Activity',
            content_type_field='source_content_type',
            object_id_field='source_object_id',
    
            related_query_name='project',
    
        def __str__(self):
    
            return self.title
    
    
        @classmethod
        def create_from_submission(cls, submission):
            """
            Create a Project from the given submission.
    
            Returns a new Project or the given ApplicationSubmissions existing
            Project.
            """
            # OneToOne relations on the targetted model cannot be accessed without
            # an exception when the relation doesn't exist (is None).  Since we
            # want to fail fast here, we can use hasattr instead.
            if hasattr(submission, 'project'):
                return submission.project
    
            return Project.objects.create(
                submission=submission,
    
                title=submission.title,
                user=submission.user,
    
                contact_email=submission.user.email,
                contact_legal_name=submission.user.full_name,
                contact_address=submission.form_data.get('address', ''),
                value=submission.form_data.get('value', 0),
    
    George Hickman's avatar
    George Hickman committed
        def clean(self):
            if self.proposed_start is None:
                return
    
            if self.proposed_end is None:
                return
    
            if self.proposed_start > self.proposed_end:
                raise ValidationError(_('Proposed End Date must be after Proposed Start Date'))
    
    
        def get_absolute_url(self):
            return reverse('apply:projects:detail', args=[self.id])