Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
# Generate a test user for endpoint responses.
self.testuser = User.objects.create_user(
username='testuser', email='user@dbca.wa.gov.au.com', password='pass')
# Log in testuser by default.
self.client.login(username='testuser', password='pass')
# Generate some Record objects.
mixer.cycle(8).blend(Record, title=mixer.RANDOM)
def test_list_filter(self):
url = '/catalogue/api/records/'
params = {'format': 'json'}
resp = self.client.get(url, data=params)
unfiltered = json.loads(resp.content.decode('utf-8'))
records = Record.objects.all()
rec1, rec2 = records[0], records[1]
# Generate an Application
app = mixer.blend(Application, name='test')
app.records.add(rec1)
params = {'format': 'json', 'application__name': 'test'}
resp = self.client.get(url, data=params)
self.assertEqual(resp.status_code, 200)
self.assertContains(resp, rec1.title)
self.assertNotContains(resp, rec2.title)
# The filtered response will be shorter than the unfiltered one.
filtered = json.loads(resp.content.decode('utf-8'))
self.assertTrue(len(unfiltered) > len(filtered))
class StyleInline(admin.StackedInline):
model = models.Style
extra = 1
@admin.register(models.Style)
class StyleAdmin(admin.ModelAdmin):
list_display = ('name','record','format',)
def get_readonly_fields(self, request, obj=None):
if obj:
return self.readonly_fields + ('name',)
return self.readonly_fields
@admin.register(models.Record)
class RecordAdmin(admin.ModelAdmin):
list_display = ("id", "identifier", "title",)
inlines = [StyleInline,]
readonly_fields = ('publication_date','modified',)
def get_readonly_fields(self, request, obj=None):
if obj:
return self.readonly_fields + ('identifier',)
return self.readonly_fields
@admin.register(models.Organization)
class OrganizationAdmin(admin.ModelAdmin):
inlines = [CollaboratorInline,]
@property
def qml(self):
"""
The default sld style file
if not exist, return None
"""
return self.default_style("QML")
class Style(models.Model):
FORMAT_CHOICES = (
('SLD','SLD'),
('QML','QML'),
('LYR','LAYER')
)
record = models.ForeignKey(Record, related_name='styles',null=True)
name = models.CharField(max_length=255)
format = models.CharField(max_length=3, choices=FORMAT_CHOICES)
default = models.BooleanField(default=False)
content = models.FileField(upload_to='{0}/catalogue/styles'.format(settings.MEDIA_ROOT),blank=True, default='')
checksum = models.CharField(blank=True,max_length=255, editable=False)
def clean(self):
from django.core.exceptions import ValidationError
try:
duplicate = Style.objects.exclude(pk=self.pk).get(record=self.record,format=self.format,default=True)
if duplicate and self.default:
raise ValidationError('There can only be one default format style for each record')
except Style.DoesNotExist:
pass
def save(self, *args, **kwargs):
update_fields=None