Skip to content

Commit

Permalink
Merge pull request #1126 from jefmoura/441-update-public-customform
Browse files Browse the repository at this point in the history
Update the public options of customform
  • Loading branch information
Rafael Muñoz Cárdenas authored Aug 7, 2018
2 parents a8a183b + a75c479 commit 54c0ad9
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 10 deletions.
1 change: 1 addition & 0 deletions factories/formlibrary_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ class Meta:

name = 'Custom Form A'
organization = SubFactory(Organization)
public = {'org': False, 'url': False}
19 changes: 19 additions & 0 deletions formlibrary/migrations/0012_remove_customform_public.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.3 on 2018-08-06 15:08
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('formlibrary', '0011_set_customform_public'),
]

operations = [
migrations.RemoveField(
model_name='customform',
name='public',
),
]
26 changes: 26 additions & 0 deletions formlibrary/migrations/0013_auto_20180807_0436.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.3 on 2018-08-07 11:36
from __future__ import unicode_literals

import django.contrib.postgres.fields.jsonb
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('formlibrary', '0012_remove_customform_public'),
]

operations = [
migrations.AddField(
model_name='customform',
name='public',
field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, help_text='Public information with the structure:{org: (bool), url: (bool)}', null=True),
),
migrations.AlterField(
model_name='customform',
name='fields',
field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, null=True),
),
]
22 changes: 22 additions & 0 deletions formlibrary/migrations/0014_set_customform_public.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.3 on 2018-08-07 14:02
from __future__ import unicode_literals

from django.db import migrations
from ..models import CustomForm


def set_form_public(apps, schema_editor):
CustomForm.objects.filter(is_public=True).update(
public={'org': True, 'url': False})


class Migration(migrations.Migration):

dependencies = [
('formlibrary', '0013_auto_20180807_0436'),
]

operations = [
migrations.RunPython(set_form_public),
]
39 changes: 29 additions & 10 deletions formlibrary/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from __future__ import unicode_literals

import json
import uuid

from django.db import models
from django.contrib import admin
from django.contrib.postgres.fields import JSONField
from django.core.exceptions import ValidationError
from django.utils import timezone
from voluptuous import Schema, Required

from workflow.models import WorkflowLevel1, SiteProfile, WorkflowLevel2, Office, AdminLevelOne, Organization
from indicators.models import DisaggregationValue, Indicator
Expand Down Expand Up @@ -193,14 +196,14 @@ class CustomFormFieldAdmin(admin.ModelAdmin):


class CustomForm(models.Model):
PUBLIC_ALL = 'all'
PUBLIC_URL = 'url'

PUBLIC_CHOICES = (
(PUBLIC_ALL, 'ALL'),
(PUBLIC_URL, 'URL'),
)

PUBLIC_ALL = 'all' # deprecated
PUBLIC_URL = 'url' # deprecated

PUBLIC_CHOICES = ( # deprecated
(PUBLIC_ALL, 'ALL'), # deprecated
(PUBLIC_URL, 'URL'), # deprecated
) # deprecated
STATUS_ARCHIVED = 'archived'
STATUS_NEW = 'new'
STATUS_PUBLISHED = 'published'
Expand All @@ -217,11 +220,11 @@ class CustomForm(models.Model):
form_uuid = models.CharField(max_length=255, verbose_name='CustomForm UUID', default=uuid.uuid4, unique=True)
description = models.TextField(null=True, blank=True)
validations = models.CharField(max_length=500, null=True, blank=True)
fields = JSONField(null=True)
fields = JSONField(blank=True, null=True)
is_public = models.BooleanField(default=0)
default_global = models.BooleanField(default=0)
organization = models.ForeignKey(Organization, default=1)
public = models.CharField(choices=PUBLIC_CHOICES, max_length=5, blank=True, null=True)
public = JSONField(blank=True, null=True, help_text="Public information with the structure:{org: (bool), url: (bool)}")
workflowlevel1 = models.ForeignKey(WorkflowLevel1, blank=True, null=True)
silo_id = models.IntegerField(default=0)
status = models.CharField(blank=True, null=True, max_length=15, default=STATUS_NEW, choices=STATUS_CHOICES)
Expand All @@ -233,8 +236,24 @@ class CustomForm(models.Model):
class Meta:
ordering = ('name',)

def _validate_public(self, public):
schema = Schema({
Required('org'): bool,
Required('url'): bool
})
return schema(public)

def clean_fields(self, exclude=None):
super(CustomForm, self).clean_fields(exclude=exclude)
if not self.is_template and self.public is not None:
try:
self.public = self._validate_public(self.public)
except Exception as e:
raise ValidationError(e)

# on save add create date or update edit date
def save(self, *args, **kwargs):
self.full_clean()
if self.create_date == None:
self.create_date = timezone.now()
self.edit_date = timezone.now()
Expand Down
Empty file added formlibrary/tests/__init__.py
Empty file.
54 changes: 54 additions & 0 deletions formlibrary/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
from django.core.exceptions import ValidationError
from django.test import TestCase, tag

import factories
from formlibrary.models import CustomForm


@tag('pkg')
class CustomFormTest(TestCase):
def setUp(self):
self.organization = factories.Organization()
self.user = factories.User()

def test_save_without_public_info(self):
custom_form = CustomForm(
organization=self.organization,
name="Humanitec's Survey",
fields="{}",
public={}
)

self.assertRaises(ValidationError, custom_form.save)

def test_save_without_public_org_info(self):
custom_form = CustomForm(
organization=self.organization,
name="Humanitec's Survey",
fields="{}",
public={'url': True}
)

self.assertRaises(ValidationError, custom_form.save)

def test_save_without_public_url_info(self):
custom_form = CustomForm(
organization=self.organization,
name="Humanitec's Survey",
fields="{}",
public={'org': True}
)

self.assertRaises(ValidationError, custom_form.save)

def test_save_with_public_info(self):
custom_form = CustomForm.objects.create(
organization=self.organization,
name="Humanitec's Survey",
fields="{}",
public={'org': True, 'url': True}
)

self.assertEqual(custom_form.name, "Humanitec's Survey")
self.assertEqual(custom_form.public, {'org': True, 'url': True})

0 comments on commit 54c0ad9

Please sign in to comment.