From cef8b24d83ef14b0c914185ce3ae2533b628dd14 Mon Sep 17 00:00:00 2001 From: Michael Gecht Date: Fri, 29 Dec 2017 16:22:47 +0100 Subject: [PATCH] Update old Python 2 formatting to Python 3 formatting. * Also add new migration for Django 2 --- .../contrib/sites/migrations/0001_initial.py | 2 +- .../migrations/0004_auto_20171020_0922.py | 2 +- geomat/stein/admin.py | 2 +- .../migrations/0007_auto_20160615_2017.py | 103 +++++++++++------- .../migrations/0035_auto_20171020_0922.py | 2 +- geomat/stein/tests/test_rest.py | 13 ++- geomat/stein/views.py | 45 ++++++-- geomat/users/migrations/0001_initial.py | 90 +++++++++++---- .../migrations/0004_auto_20171020_0922.py | 2 +- .../migrations/0005_auto_20171229_1614.py | 18 +++ 10 files changed, 196 insertions(+), 83 deletions(-) create mode 100644 geomat/users/migrations/0005_auto_20171229_1614.py diff --git a/geomat/contrib/sites/migrations/0001_initial.py b/geomat/contrib/sites/migrations/0001_initial.py index 91c0f89..db75ca9 100644 --- a/geomat/contrib/sites/migrations/0001_initial.py +++ b/geomat/contrib/sites/migrations/0001_initial.py @@ -25,7 +25,7 @@ class Migration(migrations.Migration): 'ordering': ('domain',), }, managers=[ - (b'objects', django.contrib.sites.models.SiteManager()), + ('objects', django.contrib.sites.models.SiteManager()), ], ), ] diff --git a/geomat/contrib/sites/migrations/0004_auto_20171020_0922.py b/geomat/contrib/sites/migrations/0004_auto_20171020_0922.py index 1e1870d..210c415 100644 --- a/geomat/contrib/sites/migrations/0004_auto_20171020_0922.py +++ b/geomat/contrib/sites/migrations/0004_auto_20171020_0922.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # Generated by Django 1.11.6 on 2017-10-20 09:22 -from __future__ import unicode_literals + import django.contrib.sites.models from django.db import migrations diff --git a/geomat/stein/admin.py b/geomat/stein/admin.py index 388f4b4..d0633e4 100644 --- a/geomat/stein/admin.py +++ b/geomat/stein/admin.py @@ -12,7 +12,7 @@ MineralType, Photograph, QuizAnswer, - QuizQuestion + QuizQuestion, ) diff --git a/geomat/stein/migrations/0007_auto_20160615_2017.py b/geomat/stein/migrations/0007_auto_20160615_2017.py index 00ec635..dea9448 100644 --- a/geomat/stein/migrations/0007_auto_20160615_2017.py +++ b/geomat/stein/migrations/0007_auto_20160615_2017.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- # Generated by Django 1.9.5 on 2016-06-15 20:17 - - from django.db import migrations, models @@ -14,86 +12,109 @@ class Migration(migrations.Migration): migrations.AddField( model_name='mineraltype', name='cleavage2', - field=models.CharField(blank=True, choices=[(b'PE', 'Perfect'), (b'LP', 'Less perfect'), (b'GO', 'Good'), - (b'DI', 'Distinct'), (b'ID', 'Indistinct'), (b'NO', 'None')], - max_length=2, verbose_name='cleavage 2'), - ), + field=models.CharField( + blank=True, + choices=[('PE', 'Perfect'), ('LP', 'Less perfect'), + ('GO', 'Good'), ('DI', 'Distinct'), + ('ID', 'Indistinct'), ('NO', 'None')], + max_length=2, + verbose_name='cleavage 2'), ), migrations.AddField( model_name='mineraltype', name='density', - field=models.CharField(default=0, max_length=20, verbose_name='density'), - ), + field=models.CharField( + default=0, max_length=20, verbose_name='density'), ), migrations.AddField( model_name='mineraltype', name='fracture2', - field=models.CharField(blank=True, choices=[(b'CF', 'Conchoidal'), (b'EF', 'Earthy'), (b'HF', 'Hackly'), - (b'SF', 'Splintery'), (b'UF', 'Uneven')], max_length=2, - verbose_name='fracture 2'), - ), + field=models.CharField( + blank=True, + choices=[('CF', 'Conchoidal'), ('EF', 'Earthy'), + ('HF', 'Hackly'), ('SF', 'Splintery'), ('UF', + 'Uneven')], + max_length=2, + verbose_name='fracture 2'), ), migrations.AddField( model_name='mineraltype', name='lustre2', - field=models.CharField(blank=True, choices=[(b'AM', 'Adamantine'), (b'DL', 'Dull'), (b'GR', 'Greasy'), - (b'MT', 'Metallic'), (b'PY', 'Pearly'), (b'SL', 'Silky'), - (b'SM', 'Submetallic'), (b'VT', 'Vitreous'), (b'WY', 'Waxy')], - max_length=2, verbose_name='lustre 2'), - ), + field=models.CharField( + blank=True, + choices=[('AM', 'Adamantine'), ('DL', 'Dull'), ('GR', + 'Greasy'), + ('MT', 'Metallic'), ('PY', 'Pearly'), ('SL', 'Silky'), + ('SM', 'Submetallic'), ('VT', 'Vitreous'), ('WY', + 'Waxy')], + max_length=2, + verbose_name='lustre 2'), ), migrations.AddField( model_name='mineraltype', name='other', - field=models.CharField(blank=True, max_length=100, verbose_name='other'), - ), + field=models.CharField( + blank=True, max_length=100, verbose_name='other'), ), migrations.AddField( model_name='mineraltype', name='resource_mindat', - field=models.CharField(blank=True, max_length=100, verbose_name='MinDat ID'), - ), + field=models.CharField( + blank=True, max_length=100, verbose_name='MinDat ID'), ), migrations.AddField( model_name='mineraltype', name='resource_mineralienatlas', - field=models.CharField(blank=True, max_length=100, verbose_name='MineralienAtlas ID'), + field=models.CharField( + blank=True, max_length=100, verbose_name='MineralienAtlas ID'), ), migrations.AddField( model_name='mineraltype', name='systematics', field=models.CharField( - choices=[(b'EL', 'Elements'), (b'SF', 'Sulfides & Sulfosalts'), (b'HG', 'Halogenides'), - (b'OH', 'Oxides and Hydroxides'), (b'CN', 'Carbonates and Nitrates'), (b'BR', 'Borates'), - (b'SL', 'Sulfates'), (b'PV', 'Phosphates, Arsenates & Vanadates'), - (b'SG', 'Silicates & Germanates'), (b'OC', 'Organic Compounds')], default=b'EL', max_length=2, - verbose_name='systematics'), - ), + choices=[('EL', 'Elements'), ('SF', 'Sulfides & Sulfosalts'), + ('HG', 'Halogenides'), ('OH', + 'Oxides and Hydroxides'), + ('CN', 'Carbonates and Nitrates'), ('BR', 'Borates'), + ('SL', + 'Sulfates'), ('PV', + 'Phosphates, Arsenates & Vanadates'), + ('SG', + 'Silicates & Germanates'), ('OC', + 'Organic Compounds')], + default='EL', + max_length=2, + verbose_name='systematics'), ), migrations.AlterField( model_name='handpiece', name='current_location', - field=models.CharField(blank=True, max_length=200, verbose_name='current location'), + field=models.CharField( + blank=True, max_length=200, verbose_name='current location'), ), migrations.AlterField( model_name='handpiece', name='finding_place', - field=models.CharField(blank=True, max_length=200, verbose_name='place of discovery'), + field=models.CharField( + blank=True, max_length=200, verbose_name='place of discovery'), ), migrations.AlterField( model_name='mineraltype', name='crystal_system', - field=models.CharField(blank=True, - choices=[(b'TC', 'Triclinic'), (b'MC', 'Monoclinic'), (b'OR', 'Orthorhombic'), - (b'TT', 'Tetragonal'), (b'TR', 'Trigonal'), (b'HG', 'Hexagonal'), - (b'CB', 'Cubic')], max_length=2, verbose_name='crystal system'), - ), + field=models.CharField( + blank=True, + choices=[('TC', 'Triclinic'), ('MC', 'Monoclinic'), + ('OR', 'Orthorhombic'), ('TT', 'Tetragonal'), + ('TR', 'Trigonal'), ('HG', 'Hexagonal'), ('C', + 'Cubic')], + max_length=2, + verbose_name='crystal system'), ), migrations.AlterField( model_name='mineraltype', name='minerals', - field=models.CharField(blank=True, max_length=100, verbose_name='minerals'), - ), + field=models.CharField( + blank=True, max_length=100, verbose_name='minerals'), ), migrations.AlterField( model_name='mineraltype', name='trivial_name', - field=models.CharField(blank=True, max_length=100, verbose_name='trivial name'), - ), + field=models.CharField( + blank=True, max_length=100, verbose_name='trivial name'), ), migrations.AlterField( model_name='mineraltype', name='variety', - field=models.CharField(blank=True, max_length=100, verbose_name='variety'), - ), + field=models.CharField( + blank=True, max_length=100, verbose_name='variety'), ), ] diff --git a/geomat/stein/migrations/0035_auto_20171020_0922.py b/geomat/stein/migrations/0035_auto_20171020_0922.py index 2d533d0..a9d0bfa 100644 --- a/geomat/stein/migrations/0035_auto_20171020_0922.py +++ b/geomat/stein/migrations/0035_auto_20171020_0922.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # Generated by Django 1.11.6 on 2017-10-20 09:22 -from __future__ import unicode_literals + import django.contrib.postgres.fields from django.db import migrations, models diff --git a/geomat/stein/tests/test_rest.py b/geomat/stein/tests/test_rest.py index 8c2c292..340170b 100644 --- a/geomat/stein/tests/test_rest.py +++ b/geomat/stein/tests/test_rest.py @@ -9,7 +9,13 @@ from rest_framework import status from rest_framework.test import APIClient -from geomat.stein.models import Classification, CrystalSystem, Handpiece, MineralType, Photograph +from geomat.stein.models import ( + Classification, + CrystalSystem, + Handpiece, + MineralType, + Photograph, +) from geomat.stein.serializers import StdImageField # Helper functions @@ -17,10 +23,7 @@ def convert(input): if isinstance(input, dict): - return { - convert(key): convert(value) - for key, value in list(input.items()) - } + return {convert(key): convert(value) for key, value in input.items()} elif isinstance(input, list): return [convert(element) for element in input] elif isinstance(input, str): diff --git a/geomat/stein/views.py b/geomat/stein/views.py index 749041b..6601cdc 100644 --- a/geomat/stein/views.py +++ b/geomat/stein/views.py @@ -17,7 +17,7 @@ MineralType, Photograph, QuizAnswer, - QuizQuestion + QuizQuestion, ) from geomat.stein.serializers import ( ClassificationSerializer, @@ -28,7 +28,7 @@ MineralTypeSerializer, PhotographSerializer, QuizAnswerFullSerializer, - QuizQuestionFullSerializer + QuizQuestionFullSerializer, ) @@ -66,28 +66,30 @@ class ListFilterAPIView(generics.ListAPIView): """ A View which creates a filters dict and returns a List of objects matching alle given Filters. View only for Retrieving Data.""" varchar_fields = ( - ) # Tupel containing all modelfileds which are varcharfields - int_fields = () # Tupel containing all modelfileds which are integerfields + ) # Tupel containing all modelfileds which are varcharfields + int_fields = ( + ) # Tupel containing all modelfileds which are integerfields model_fields = ( - ) # Tupel containing all modelfileds which are Model relation fields those are also ints + ) # Tupel containing all modelfileds which are Model relation fields those are also ints def get_filters(self): """ Method which creates the filters dict. Matches the given Parameters from the request with the allowed lookup_fields, and only maps non-empty fields. """ filters = {} - for field in self.lookup_field: # goes on for every field we defined as lookup_field + for field in self.lookup_field: # goes on for every field we defined as lookup_field if self.request.GET.get(field, None): - if field in self.varchar_fields: # we do not want the filter to contain "empty" fields + if field in self.varchar_fields: # we do not want the filter to contain "empty" fields filters[field] = ast.literal_eval( self.request.GET.get(field, None)) - elif field in self.int_fields: # Modelreferences are searched by pk + elif field in self.int_fields: # Modelreferences are searched by pk filters[field] = int(self.request.GET.get(field, None)) elif field in self.model_fields: filters[field] = int(self.request.GET.get(field, None)) else: filters[field] = self.request.GET.get( - field, None) # get the value of the field from request + field, + None) # get the value of the field from request return filters @@ -241,8 +243,29 @@ class FilterPhotographList(ListFilterAPIView): # API View for the Mineraltype Profiles class MineraltypeProfiles(generics.ListAPIView): queryset = MineralType.objects.all() - serializer_class = MineralProfilesSerializer - name = 'mineraltype-profiles' + serializer_class = MineralTypeSerializer + name = 'mineral-profiles' + + def get(self, request, *args, **kwargs): + + categories = MineralType.MINERAL_CATEGORIES + data = {} + + for cat in categories: + cat_string = cat[0] + classification = Classification.objects.filter( + mineral_type__systematics=cat_string).all() + human_string = cat[1] + data[str(human_string)] = {} + for clas in classification: + minerals = MineralType.objects.filter( + systematics=cat_string, classification=clas).all() + + data[str(human_string)][ + clas.classification_name] = MineralTypeSerializer( + minerals, many=True).data + + return Response(data=data) # Api View for the Glossary diff --git a/geomat/users/migrations/0001_initial.py b/geomat/users/migrations/0001_initial.py index d49f6f7..76c0a76 100644 --- a/geomat/users/migrations/0001_initial.py +++ b/geomat/users/migrations/0001_initial.py @@ -1,10 +1,8 @@ # -*- coding: utf-8 -*- - - -from django.db import models, migrations -import django.utils.timezone import django.contrib.auth.models import django.core.validators +import django.utils.timezone +from django.db import migrations, models class Migration(migrations.Migration): @@ -17,20 +15,71 @@ class Migration(migrations.Migration): migrations.CreateModel( name='User', fields=[ - ('id', models.AutoField(primary_key=True, verbose_name='ID', serialize=False, auto_created=True)), - ('password', models.CharField(max_length=128, verbose_name='password')), - ('last_login', models.DateTimeField(null=True, verbose_name='last login', blank=True)), - ('is_superuser', models.BooleanField(help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status', default=False)), - ('username', models.CharField(max_length=30, validators=[django.core.validators.RegexValidator('^[\\w.@+-]+$', 'Enter a valid username. This value may contain only letters, numbers and @/./+/-/_ characters.', 'invalid')], verbose_name='username', error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.', unique=True)), - ('first_name', models.CharField(max_length=30, verbose_name='first name', blank=True)), - ('last_name', models.CharField(max_length=30, verbose_name='last name', blank=True)), - ('email', models.EmailField(max_length=254, verbose_name='email address', blank=True)), - ('is_staff', models.BooleanField(help_text='Designates whether the user can log into this admin site.', verbose_name='staff status', default=False)), - ('is_active', models.BooleanField(help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active', default=True)), - ('date_joined', models.DateTimeField(verbose_name='date joined', default=django.utils.timezone.now)), - ('groups', models.ManyToManyField(related_name='user_set', blank=True, verbose_name='groups', to='auth.Group', help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_query_name='user')), - ('user_permissions', models.ManyToManyField(related_name='user_set', blank=True, verbose_name='user permissions', to='auth.Permission', help_text='Specific permissions for this user.', related_query_name='user')), - ('name', models.CharField(max_length=255, verbose_name='Name of User', blank=True)), + ('id', models.AutoField( + primary_key=True, + verbose_name='ID', + serialize=False, + auto_created=True)), + ('password', models.CharField( + max_length=128, verbose_name='password')), + ('last_login', models.DateTimeField( + null=True, verbose_name='last login', blank=True)), + ('is_superuser', models.BooleanField( + help_text= + 'Designates that this user has all permissions without explicitly assigning them.', + verbose_name='superuser status', + default=False)), + ('username', models.CharField( + max_length=30, + validators=[ + django.core.validators.RegexValidator( + '^[\\w.@+-]+$', + 'Enter a valid username. This value may contain only letters, numbers and @/./+/-/_ characters.', + 'invalid') + ], + verbose_name='username', + error_messages={ + 'unique': 'A user with that username already exists.' + }, + help_text= + 'Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.', + unique=True)), + ('first_name', models.CharField( + max_length=30, verbose_name='first name', blank=True)), + ('last_name', models.CharField( + max_length=30, verbose_name='last name', blank=True)), + ('email', models.EmailField( + max_length=254, verbose_name='email address', blank=True)), + ('is_staff', models.BooleanField( + help_text= + 'Designates whether the user can log into this admin site.', + verbose_name='staff status', + default=False)), + ('is_active', models.BooleanField( + help_text= + 'Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', + verbose_name='active', + default=True)), + ('date_joined', models.DateTimeField( + verbose_name='date joined', + default=django.utils.timezone.now)), + ('groups', models.ManyToManyField( + related_name='user_set', + blank=True, + verbose_name='groups', + to='auth.Group', + help_text= + 'The groups this user belongs to. A user will get all permissions granted to each of their groups.', + related_query_name='user')), + ('user_permissions', models.ManyToManyField( + related_name='user_set', + blank=True, + verbose_name='user permissions', + to='auth.Permission', + help_text='Specific permissions for this user.', + related_query_name='user')), + ('name', models.CharField( + max_length=255, verbose_name='Name of User', blank=True)), ], options={ 'verbose_name': 'user', @@ -38,7 +87,6 @@ class Migration(migrations.Migration): 'verbose_name_plural': 'users', }, managers=[ - (b'objects', django.contrib.auth.models.UserManager()), - ], - ), + ('objects', django.contrib.auth.models.UserManager()), + ], ), ] diff --git a/geomat/users/migrations/0004_auto_20171020_0922.py b/geomat/users/migrations/0004_auto_20171020_0922.py index 9b55eed..b693779 100644 --- a/geomat/users/migrations/0004_auto_20171020_0922.py +++ b/geomat/users/migrations/0004_auto_20171020_0922.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # Generated by Django 1.11.6 on 2017-10-20 09:22 -from __future__ import unicode_literals + import django.contrib.auth.models import django.contrib.auth.validators diff --git a/geomat/users/migrations/0005_auto_20171229_1614.py b/geomat/users/migrations/0005_auto_20171229_1614.py new file mode 100644 index 0000000..e3632c3 --- /dev/null +++ b/geomat/users/migrations/0005_auto_20171229_1614.py @@ -0,0 +1,18 @@ +# Generated by Django 2.0 on 2017-12-29 15:14 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0004_auto_20171020_0922'), + ] + + operations = [ + migrations.AlterField( + model_name='user', + name='last_name', + field=models.CharField(blank=True, max_length=150, verbose_name='last name'), + ), + ]