diff --git a/sigi/apps/eventos/admin.py b/sigi/apps/eventos/admin.py index e06fc70..fb1c415 100644 --- a/sigi/apps/eventos/admin.py +++ b/sigi/apps/eventos/admin.py @@ -22,19 +22,7 @@ from django.contrib import admin from django import forms from django.utils.translation import ugettext as _ from sigi.apps.eventos.models import TipoEvento, Funcao, Evento, Equipe, Convite -from sigi.apps.utils.moodle_ws_api import get_courses -def get_course_choices(): - result = [(None, u'---------')] - - try: - courses = get_courses(sort_order='categorysortorder', idnumber__startswith='evento') - result = result + [(c['id'], c['fullname']) for c in courses] - except Exception as e: - result.append((None, _(u"Erro ao acessar o saberes: '%s'" % (e.message,)))) - - return result - class EventoAdminForm(forms.ModelForm): class Meta: model = Evento @@ -43,11 +31,6 @@ class EventoAdminForm(forms.ModelForm): 'data_cancelamento', 'motivo_cancelamento', 'curso_moodle_id', ) - def __init__(self, *args, **kwargs): - super(EventoAdminForm, self).__init__(*args, **kwargs) - self.fields['curso_moodle_id'] = forms.ChoiceField(choices=get_course_choices(), - label=_(u"Curso Saberes")) - def clean(self): cleaned_data = super(EventoAdminForm, self).clean() data_inicio = cleaned_data.get("data_inicio") diff --git a/sigi/apps/eventos/migrations/0002_auto_20151016_1449.py b/sigi/apps/eventos/migrations/0002_auto_20151016_1449.py new file mode 100644 index 0000000..13e25e6 --- /dev/null +++ b/sigi/apps/eventos/migrations/0002_auto_20151016_1449.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('eventos', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='evento', + name='curso_moodle_id', + field=models.IntegerField(blank=True, null=True, verbose_name='Curso saberes', choices=[(None, '---------'), (None, "Erro ao acessar o saberes: 'invalidtoken (moodle_exception): Token inv\xe1lido - token n\xe3o encontrado'")]), + preserve_default=True, + ), + ] diff --git a/sigi/apps/eventos/models.py b/sigi/apps/eventos/models.py index 014c876..522170e 100644 --- a/sigi/apps/eventos/models.py +++ b/sigi/apps/eventos/models.py @@ -19,12 +19,12 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. from django.db import models -from django.db.models import Q +from django.utils.functional import lazy from django.utils.translation import ugettext as _ from sigi.apps.casas.models import CasaLegislativa from sigi.apps.contatos.models import Municipio from sigi.apps.servidores.models import Servidor -from sigi.apps.mdl.models import Course +from sigi.apps.utils.moodle_ws_api import get_courses from django.core.exceptions import ValidationError class TipoEvento(models.Model): @@ -45,6 +45,18 @@ class Evento(models.Model): ('R', _(u"Realizado")), ('C', _(u"Cancelado")) ) + + def get_course_choices(): + result = [(None, u'---------')] + + try: + courses = get_courses(sort_order='categorysortorder', idnumber__startswith='evento') + result = result + [(c['id'], c['fullname']) for c in courses] + except Exception as e: + result.append((None, _(u"Erro ao acessar o saberes: '%s'" % (e.message,)))) + + return result + tipo_evento = models.ForeignKey(TipoEvento) nome = models.CharField(_(u"Nome do evento"), max_length=100) @@ -52,14 +64,16 @@ class Evento(models.Model): solicitante = models.CharField(_(u"Solicitante"), max_length=100) data_inicio = models.DateField(_(u"Data de início")) data_termino = models.DateField(_(u"Data de término")) - casa_anfitria = models.ForeignKey(CasaLegislativa, verbose_name=_(u"Casa anfitriã"), blank=True, null=True) + casa_anfitria = models.ForeignKey(CasaLegislativa, verbose_name=_(u"Casa anfitriã"), blank=True, + null=True) municipio = models.ForeignKey(Municipio) local = models.TextField(_(u"Local do evento"), blank=True) publico_alvo = models.TextField(_(u"Público alvo"), blank=True) status = models.CharField(_(u"Status"), max_length=1, choices=STATUS_CHOICES) data_cancelamento = models.DateField(_(u"Data de cancelamento"), blank=True, null=True) motivo_cancelamento = models.TextField(_(u"Motivo do cancelamento"), blank=True) - curso_moodle_id = models.IntegerField(_(u"Curso saberes"), blank=True, null=True) + curso_moodle_id = models.IntegerField(_(u"Curso saberes"), blank=True, null=True, + choices=lazy(get_course_choices, list)()) class Meta: ordering = ("-data_inicio",) @@ -80,13 +94,6 @@ class Evento(models.Model): raise ValidationError(_(u"Data de término deve ser posterior à data de início")) return super(Evento, self).save(*args, **kwargs) - @property - def curso_moodle(self): - if self.curso_moodle_id: - return Course.objects.get(id=self.curso_moodle_id) - else: - return None - class Funcao(models.Model): nome = models.CharField(_(u"Função na equipe de evento"), max_length=100) descricao = models.TextField(_(u"Descrição da função"))