Skip to content

Commit

Permalink
Merge branch 'institution' of github.com:robertatakenaka/template-scm…
Browse files Browse the repository at this point in the history
…s into robertatakenaka-institution
  • Loading branch information
gitnnolabs committed Oct 16, 2022
2 parents 67aff2e + c6eceb0 commit 5dbff49
Show file tree
Hide file tree
Showing 15 changed files with 2,123 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
# Your stuff: custom apps go here
"core",
"location",
"institution",
]

# https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
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)

0 comments on commit 5dbff49

Please sign in to comment.