Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cria app Institution #42

Merged
merged 11 commits into from
Oct 16, 2022
6 changes: 3 additions & 3 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,15 @@
"wagtailcaptcha",
"wagtailmenus",
"rest_framework",
"core",
]

LOCAL_APPS = [
"core.users",
"core_settings",
# Your stuff: custom apps go here
"core",
"location",
"institution",
]

# https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
Expand Down Expand Up @@ -345,7 +347,6 @@
('en', "English"),
('es', "Spanish"),
('pt-BR', "Portuguese"),
('it', "Italian"),
]

WAGTAIL_I18N_ENABLED = True
Expand All @@ -354,7 +355,6 @@
('en', "English"),
('es', "Spanish"),
('pt-BR', "Portuguese"),
('it', "Italian")
]

NOCAPTCHA = True
Expand Down
1 change: 1 addition & 0 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
# User management
path("api/v2/", api_router.urls),
path("users/", include("core.users.urls", namespace="users")),
path('i18n/', include('django.conf.urls.i18n')),
path("", include("allauth.urls")),
path("", include(wagtail_urls)),
)
Expand Down
2 changes: 0 additions & 2 deletions core/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import os

from django.db import models
from django.contrib.auth import get_user_model
from django.utils.translation import gettext as _
Expand Down
Empty file added institution/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions institution/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions institution/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class InstitutionConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'institution'
13 changes: 13 additions & 0 deletions institution/choices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.utils.translation import gettext as _


inst_type = (
('', ''),
('agência de apoio à pesquisa', _('agência de apoio à pesquisa')),
('universidade e instâncias ligadas à universidades', _('universidade e instâncias ligadas à universidades')),
('empresa ou instituto ligadas ao governo', _('empresa ou instituto ligadas ao governo')),
('organização privada', _('organização privada')),
('organização sem fins de lucros', _('organização sem fins de lucros')),
('sociedade científica, associação pós-graduação, associação profissional', _('sociedade científica, associação pós-graduação, associação profissional')),
('outros', _('outros')),
)
1,838 changes: 1,838 additions & 0 deletions institution/fixtures/institutions.csv

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions institution/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from wagtail.admin.forms import WagtailAdminModelForm


class InstitutionForm(WagtailAdminModelForm):

def save_all(self, user):
inst = super().save(commit=False)

if self.instance.pk is not None:
inst.updated_by = user
else:
inst.creator = user

self.save()

return inst
35 changes: 35 additions & 0 deletions institution/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 3.2.12 on 2022-08-29 09:10

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
('location', '0001_initial'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='Institution',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', models.DateTimeField(auto_now_add=True, verbose_name='Data de criação')),
('updated', models.DateTimeField(auto_now=True, verbose_name='Data da última atualização')),
('name', models.CharField(blank=True, max_length=255, null=True, verbose_name='Nome')),
('institution_type', models.CharField(blank=True, choices=[('', ''), ('agência de apoio à pesquisa', 'agência de apoio à pesquisa'), ('universidade e instâncias ligadas à universidades', 'universidade e instâncias ligadas à universidades'), ('empresa ou instituto ligadas ao governo', 'empresa ou instituto ligadas ao governo'), ('organização privada', 'organização privada'), ('organização sem fins de lucros', 'organização sem fins de lucros'), ('sociedade científica, associação pós-graduação, associação profissional', 'sociedade científica, associação pós-graduação, associação profissional'), ('outros', 'outros')], max_length=255, null=True, verbose_name='Institution Type')),
('acronym', models.CharField(blank=True, max_length=255, null=True, verbose_name='Acronym to the institution')),
('creator', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, related_name='institution_creator', to=settings.AUTH_USER_MODEL, verbose_name='Criador')),
('location', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='location.location')),
('updated_by', models.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='institution_last_mod_user', to=settings.AUTH_USER_MODEL, verbose_name='Atualizador')),
],
options={
'abstract': False,
},
),
]
38 changes: 38 additions & 0 deletions institution/migrations/0002_auto_20220826_1504.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 3.2.12 on 2022-08-26 15:04

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('institution', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='institution',
name='level_1',
field=models.CharField(blank=True, max_length=255, null=True, verbose_name='Level 1 organization'),
),
migrations.AddField(
model_name='institution',
name='level_2',
field=models.CharField(blank=True, max_length=255, null=True, verbose_name='Level 2 organization'),
),
migrations.AddField(
model_name='institution',
name='level_3',
field=models.CharField(blank=True, max_length=255, null=True, verbose_name='Level 3 organization'),
),
migrations.AddField(
model_name='institution',
name='logo',
field=models.ImageField(blank=True, null=True, upload_to='', verbose_name='Logo'),
),
migrations.AddField(
model_name='institution',
name='url',
field=models.URLField(blank=True, null=True, verbose_name='url'),
),
]
Empty file.
88 changes: 88 additions & 0 deletions institution/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from django.db import models
from django.utils.translation import gettext as _
from modelcluster.models import ClusterableModel
from wagtail.admin.edit_handlers import FieldPanel, InlinePanel

from core.models import CommonControlField
from location.models import Location

from . import choices
from .forms import InstitutionForm


class Institution(CommonControlField, ClusterableModel):
name = models.CharField(_("Name"), max_length=255, null=True, blank=True)
institution_type = models.CharField(_("Institution Type"), choices=choices.inst_type,
max_length=255, null=True, blank=True)

location = models.ForeignKey(Location, null=True, blank=True, on_delete=models.SET_NULL)

acronym = models.CharField(_("Institution Acronym"), blank=True, null=True, max_length=255)

level_1 = models.CharField(_("Organization Level 1"), blank=True, null=True, max_length=255)

level_2 = models.CharField(_("Organization Level 2"), blank=True, null=True, max_length=255)

level_3 = models.CharField(_("Organization Level 3"), blank=True, null=True, max_length=255)

url = models.URLField("url", blank=True, null=True)

logo = models.ImageField(_("Logo"), blank=True, null=True)

panels = [
FieldPanel('name'),
FieldPanel('acronym'),
FieldPanel('institution_type'),
FieldPanel('location'),
FieldPanel('level_1'),
FieldPanel('level_2'),
FieldPanel('level_3'),
FieldPanel('url'),
FieldPanel('logo'),
]

def __unicode__(self):
return (
u'%s | %s | %s | %s | %s' %
(self.name, self.acronym, self.level_1,
self.level_2, self.level_3, self.location))

def __str__(self):
return (
u'%s | %s | %s | %s | %s' %
(self.name, self.acronym, self.level_1,
self.level_2, self.level_3, self.location))

@classmethod
def get_or_create(cls, inst_name, inst_acronym, level_1, level_2, level_3, location):

# Institution
# check if exists the institution
parms = {}
if inst_name:
parms['name'] = inst_name
if inst_acronym:
parms['acronym'] = inst_acronym
if location:
parms['location'] = location
if level_1:
parms['level_1'] = level_1
if level_2:
parms['level_2'] = level_2
if level_3:
parms['level_3'] = level_3

try:
return cls.objects.get(**parms)
except:
institution = cls()
institution.name = inst_name
institution.acronym = inst_acronym
institution.level_1 = level_1
institution.level_2 = level_2
institution.level_3 = level_3
institution.location = Location
institution.save()
return institution

base_form_class = InstitutionForm
48 changes: 48 additions & 0 deletions institution/scripts/bulk_institution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import csv
import os

from institution import models
from location.models import Location, Country, State

from django.contrib.auth import get_user_model


User = get_user_model()


def run(*args):
user_id = 1

# Delete all actions
models.Institution.objects.all().delete()

# User
if args:
user_id = args[0]

creator = User.objects.get(id=user_id)

with open(os.path.dirname(os.path.realpath(__file__)) + "/../fixtures/institutions.csv", 'r') as csvfile:
data = csv.DictReader(csvfile, delimiter=";")

try:
for line, row in enumerate(data):
inst = models.Institution()
inst.name = row['Name']
inst.institution_type = row['Institution Type']
inst.acronym = row['Acronym']
inst.level_1 = row['Level_1']
inst.level_2 = row['Level_2']
inst.level_3 = row['Level_3']
inst.location = Location.get_or_create(user=creator,
location_country=Country.get_or_create(user=creator, name="Brasil", acronym='BR'),
location_state=State.get_or_create(user=creator, acronym=row['State Acronym']),
location_city=None)
inst.creator = creator
inst.save()

except Exception as ex:
print("Import error: %s, Line: %s, Data: %s" % (ex, str(line + 2), row))
else:
print("File imported successfully!")

3 changes: 3 additions & 0 deletions institution/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions institution/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
31 changes: 31 additions & 0 deletions institution/wagtail_hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from django.http import HttpResponseRedirect
from django.utils.translation import gettext as _

from wagtail.contrib.modeladmin.views import CreateView
from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register

from .models import Institution


class InstitutionCreateView(CreateView):

def form_valid(self, form):
self.object = form.save_all(self.request.user)
return HttpResponseRedirect(self.get_success_url())


class InstitutionAdmin(ModelAdmin):
model = Institution
create_view_class = InstitutionCreateView
menu_label = _('Institution')
menu_icon = 'folder'
menu_order = 300
add_to_settings_menu = False # or True to add your model to the Settings sub-menu
exclude_from_explorer = False # or True to exclude pages of this type from Wagtail's explorer view
list_display = ('name', 'institution_type', 'creator', 'updated', 'created', 'updated_by')
search_fields = ('name', 'institution_type', 'creator', 'updated', 'created', 'updated_by')
list_export = ('name', 'institution_type', 'level_1', 'level_2', 'level_3', 'creator', 'updated', 'created', 'updated_by')
export_filename = 'institutions'


modeladmin_register(InstitutionAdmin)
Empty file added location/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions location/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions location/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class LocationConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'location'
Loading