Skip to content

Commit

Permalink
Code cleanup (#117)
Browse files Browse the repository at this point in the history
* Code cleanup

* Use a macro to remove template duplications

* Fix behavior to don't update page URL when there is no appointments in the current day

* Review Edit / Remove Appointments test

* Code review

* Move AgendaMixin to browser module

* Code review
  • Loading branch information
hvelarde authored Jul 2, 2018
1 parent 26126ca commit 3b3f87f
Show file tree
Hide file tree
Showing 14 changed files with 261 additions and 403 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Changelog
2.0a4 (unreleased)
^^^^^^^^^^^^^^^^^^

- Nothing changed yet.
- Limpeza e reorganização do código.
[hvelarde, rodfersou]


2.0a3 (2018-06-28)
Expand Down
33 changes: 4 additions & 29 deletions src/brasil/gov/agenda/browser/agenda.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# -*- coding: utf-8 -*-
from six.moves import range # noqa: I001
from brasil.gov.agenda import _
from brasil.gov.agenda.browser.mixin import AgendaMixin
from brasil.gov.agenda.config import AGENDADIARIAFMT
from brasil.gov.agenda.interfaces import IAgendaDiaria
from brasil.gov.agenda.interfaces import ICompromisso
from brasil.gov.agenda.utils import AgendaMixin
from calendar import monthrange
from datetime import datetime
from datetime import timedelta
Expand All @@ -16,7 +15,6 @@
from Products.Five.browser import BrowserView
from zExceptions import NotFound
from zope.component import getMultiAdapter
from zope.i18nmessageid import Message
from zope.interface import implementer
from zope.publisher.interfaces import IPublishTraverse

Expand All @@ -27,14 +25,10 @@ class AgendaView(BrowserView, AgendaMixin):
"""Visao padrao da agenda."""

def setup(self):
plone_tools = getMultiAdapter((self.context, self.request),
name='plone_tools')
context_state = getMultiAdapter((self.context, self.request),
name=u'plone_context_state')
self._ts = api.portal.get_tool('translation_service')
self.catalog = plone_tools.catalog()
self.agenda = self.context
self.workflow = plone_tools.workflow()
self.editable = context_state.is_editable()

def results(self, b_size=16):
Expand Down Expand Up @@ -69,9 +63,9 @@ def agenda_recente(self):
# e se ela esta publicada
if hoje in self.context.objectIds():
agenda = self.context[hoje]
review_state = self.workflow.getInfoFor(agenda, 'review_state')
agenda = agenda if review_state == 'published' else None
return agenda
review_state = api.content.get_state(agenda)
if review_state == 'published':
return agenda

def _format_time(self, value):
return value.strftime('%Hh%M')
Expand All @@ -83,25 +77,6 @@ def get_link_erros(self):
else:
return None

@property
def date(self):
date = DateTime()
return date

def weekday(self):
date = self.date
return self._translate(self._ts.day_msgid(date.strftime('%w')))

def long_date(self):
month = self.month()
date = self.date
parts = {}
parts['day'] = date.strftime('%d')
parts['month'] = month['strmonthcomplete']
parts['year'] = date.strftime('%Y')
return self.context.translate(Message(_(u'long_date_agenda'),
mapping=parts))

def orgao(self):
orgao = self.context.orgao
return orgao
Expand Down
2 changes: 1 addition & 1 deletion src/brasil/gov/agenda/browser/agendadiaria.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
from Acquisition import aq_parent
from brasil.gov.agenda import _
from brasil.gov.agenda.utils import AgendaMixin
from brasil.gov.agenda.browser.mixin import AgendaMixin
from datetime import datetime
from Products.CMFCore.utils import getToolByName
from Products.Five.browser import BrowserView
Expand Down
7 changes: 7 additions & 0 deletions src/brasil/gov/agenda/browser/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
xmlns:i18n="http://namespaces.zope.org/i18n"
i18n_domain="brasil.gov.agenda">

<browser:page
name="agenda-macros"
template="templates/agenda_macros.pt"
for="*"
permission="zope.Public"
/>

<browser:page
for="brasil.gov.agenda.interfaces.IAgenda"
name="view"
Expand Down
66 changes: 66 additions & 0 deletions src/brasil/gov/agenda/browser/mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-
from six.moves import range # noqa: I001
from datetime import datetime
from datetime import timedelta
from Products.CMFCore.utils import getToolByName
from zope.component import getMultiAdapter


class AgendaMixin:
"""Common methods and functions used by views and and tiles."""

def _translate(self, msgid, locale='plonelocales', mapping=None):
tool = getToolByName(self.context, 'translation_service')
portal_state = getMultiAdapter((self.context, self.request),
name=u'plone_portal_state')
current_language = portal_state.language()
# XXX: Por que é retornado 'pt-br' do portal_state ao invés de 'pt_BR'?
# Quando uso 'pt-br' ao invés de 'pt_BR', não pega a tradução quando
# feita de forma manual.
target_language = ('pt_BR' if current_language == 'pt-br'
else self.current_language)
return tool.translate(msgid,
locale,
mapping=mapping,
context=self.context,
target_language=target_language)

def month(self):
tool = getToolByName(self.context, 'translation_service')
today = datetime.now()
strmonth = self._translate(tool.month_msgid(today.strftime('%m')))
return {
'strmonth': strmonth[:3].upper(),
'strmonthcomplete': strmonth.upper(),
'month': today.month,
'year': today.year,
}

def days(self):
tool = getToolByName(self.context, 'translation_service')
today = datetime.now()
# get a list with 3 days before and 3 days after today
days = [(today + timedelta(i)) for i in range(-3, 4)]
weekdays = []
for day in days:
cssclass = ['day']
if day == today:
cssclass.append('is-selected')
if self.agenda.get(day.strftime('%Y-%m-%d'), False):
cssclass.append('has-appointment')
strweek = self._translate(tool.day_msgid(day.weekday()))
weekdays.append({
'day': day.day,
'weekday': strweek[:3],
'iso': day.isoformat(),
'cssclass': ' '.join(cssclass),
})
return weekdays

@staticmethod
def compromissos():
return []

@staticmethod
def exibe_sem_compromissos():
return True
125 changes: 125 additions & 0 deletions src/brasil/gov/agenda/browser/templates/agenda_macros.pt
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<metal:form define-macro="search"
i18n:domain="brasil.gov.agenda">
<div class="search-compromisso">
<form class="search-form"
tal:attributes="action string:${view/agenda/absolute_url}/@@search">
<div class="LSBox">
<input class="searchField"
name="SearchableText"
type="text"
size="15"
title="Busca em agenda"
placeholder="Buscar em agenda"
autocomplete="off" />
<input class="searchButton" type="submit" value="Buscar" />
<input type="hidden"
name="portal_type:list"
value="AgendaDiaria"
checked="checked"
/>
</div>
</form>
</div>
</metal:form>

<metal:form define-macro="calendar"
i18n:domain="brasil.gov.agenda">
<div class="calendar-wrapper">
<div class="calendar-title"
tal:define="month view/month">
<span class="strmonth" tal:content="month/strmonthcomplete" />
<span class="from" i18n:translate="">de</span>
<span class="year" tal:content="month/year" />
</div>
<div class="calendar"></div>
</div>
</metal:form>

<metal:form define-macro="daypicker"
i18n:domain="brasil.gov.agenda">
<div class="daypicker-wrapper">
<ul class="daypicker"
tal:condition="view/days">
<tal:event repeat="day view/days">
<li tal:attributes="data-day day/iso;
class day/cssclass;">
<div class="daypicker-day"
tal:content="day/day" />
<div class="daypicker-weekday"
tal:content="day/weekday" />
</li>
</tal:event>
</ul>
</div>
</metal:form>

<metal:form define-macro="appointments"
i18n:domain="brasil.gov.agenda">
<div class="collectionevents-title-wrapper">
<div class="collectionevents-stroke"></div>
<div class="collectionevents-title"
i18n:translate="">
Compromissos do dia
</div>
<div class="collectionevents-stroke"></div>
</div>
<ul class="list-compromissos">
<li class="item-compromisso-wrapper"
tal:repeat="comp view/compromissos">
<div class="item-compromisso">
<div class="compromisso-horarios">
<time class="horario compromisso-inicio"
tal:attributes="datetime comp/start_date"
tal:content="comp/start_time">16h20</time>
<div class="now"
tal:condition="comp/is_now"
i18n:translate="">Agora</div>
</div>
<div class="compromisso-dados">
<h4 class="compromisso-titulo"
tal:content="comp/title">
Reunião com Dorneles Treméa
</h4>
<p class="compromisso-participantes"
tal:condition="comp/attendees"
tal:content="structure comp/attendees_formatted">
Dorneles Treméa
</p>
<p class="compromisso-pauta"
tal:condition="comp/description">
<label i18n:translate="">Pauta:</label>
<span tal:replace="comp/description">Assunto a ser tratado</span>
</p>
<p class="compromisso-local"
tal:condition="comp/location"
tal:content="comp/location">
Palácio do Planalto
</p>
<span class="download-compromisso">
<a class="add-agenda vcal"
tal:attributes="href string:${comp/url}/vcal_view"
i18n:translate="">VCAL</a>
<span i18n:translate="">Adicionar ao meu calendario</span>
</span>
</div>
<ul class="compromisso-acoes"
tal:condition="view/editable">
<li class="compromisso-acao">
<a class="compromisso editar_compromisso acao"
tal:attributes="href string:${comp/url}/edit"
i18n:translate="">Editar</a>
</li>
<li class="compromisso-acao">
<a class="compromisso remover_compromisso acao"
tal:attributes="href string:${comp/url}/delete_confirmation"
i18n:translate="">Remover</a>
</li>
</ul>
</div>
</li>
<li class="sem-compromisso item-compromisso"
tal:condition="view/exibe_sem_compromissos">
<span i18n:translate="label_agendadiaria_sem_compromissos">Sem compromissos oficiais.</span>
</li>
</ul>
</metal:form>
Loading

0 comments on commit 3b3f87f

Please sign in to comment.