Skip to content

Commit b495a35

Browse files
Adiciona feature flag lib (#3693)
Adiciona feature flag lib e refatora Solr para usá-la
1 parent 6c00245 commit b495a35

21 files changed

+125
-47
lines changed

docker/docker-compose.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ services:
5454
SOLR_COLLECTION: sapl
5555
SOLR_URL: http://solr:solr@saplsolr:8983
5656
IS_ZK_EMBEDDED: 'True'
57+
ENABLE_SAPN: 'False'
5758
TZ: America/Sao_Paulo
5859
volumes:
5960
- sapl_data:/var/interlegis/sapl/data

docker/start.sh

+17
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ create_env() {
3939
echo "SOLR_COLLECTION = ""${SOLR_COLLECTION-sapl}" >> $FILENAME
4040
echo "SOLR_URL = ""${SOLR_URL-http://localhost:8983}" >> $FILENAME
4141
echo "IS_ZK_EMBEDDED = ""${IS_ZK_EMBEDDED-False}" >> $FILENAME
42+
echo "ENABLE_SAPN = ""${ENABLE_SAPN-False}" >> $FILENAME
4243

4344
echo "[ENV FILE] done."
4445
}
@@ -85,14 +86,30 @@ if [ "${USE_SOLR-False}" == "True" ] || [ "${USE_SOLR-False}" == "true" ]; then
8586
fi
8687

8788
python3 solr_cli.py -u $SOLR_URL -c $SOLR_COLLECTION -s $NUM_SHARDS -rf $RF -ms $MAX_SHARDS_PER_NODE $ZK_EMBEDDED &
89+
# Enable SOLR switch on, creating if it doesn't exist on database
90+
./manage.py waffle_switch SOLR_SWITCH on --create
8891
else
8992
echo "Solr is offline, not possible to connect."
93+
# Disable Solr switch off, creating if it doesn't exist on database
94+
./manage.py waffle_switch SOLR_SWITCH off --create
9095
fi
9196

9297
else
9398
echo "Solr support is not initialized."
99+
# Disable Solr switch off, creating if it doesn't exist on database
100+
./manage.py waffle_switch SOLR_SWITCH off --create
94101
fi
95102

103+
## Enable/Disable SAPN
104+
if [ "${ENABLE_SAPN-False}" == "True" ] || [ "${ENABLE_SAPN-False}" == "true" ]; then
105+
echo "Enabling SAPN"
106+
./manage.py waffle_switch SAPLN_SWITCH on --create
107+
else
108+
echo "Enabling SAPL"
109+
./manage.py waffle_switch SAPLN_SWITCH off --create
110+
fi
111+
112+
96113
echo "Creating admin user..."
97114

98115
user_created=$(python3 create_admin.py 2>&1)

docs/feature-flags.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
### Types
3+
4+
Waffle supports three types of feature flippers:
5+
6+
### Flags
7+
8+
_"Flags can be used to enable a feature for specific users, groups, users meeting
9+
certain criteria (such as being authenticated, or superusers) or a certain percentage
10+
of visitors."_
11+
12+
https://waffle.readthedocs.io/en/stable/types/flag.html
13+
14+
### Switches
15+
16+
_"Switches are simple booleans: they are on or off, for everyone, all the time.
17+
They do not require a request object and can be used in other contexts, such
18+
as management commands and tasks."_
19+
20+
Enabling/Disabling via CLI (example):
21+
22+
./manage.py waffle_switch SOLR_SWITCH on --create
23+
24+
./manage.py waffle_switch SOLR_SWITCH off --create
25+
26+
27+
https://waffle.readthedocs.io/en/stable/types/switch.html
28+
29+
### Samples
30+
31+
_"Samples are on a given percentage of the time. They do not require a request
32+
object and can be used in other contexts, such as management commands and tasks."_
33+
34+
Liga e desliga baseado em amostragem aleatória.
35+
36+
https://waffle.readthedocs.io/en/stable/types/sample.html
37+
38+
### Reference
39+
* Documentation: https://waffle.readthedocs.io/
40+
* Managing from CLI: https://waffle.readthedocs.io/en/stable/usage/cli.html
41+
* Templates: https://waffle.readthedocs.io/en/stable/usage/templates.html
42+
* Views: https://waffle.readthedocs.io/en/stable/usage/views.html
43+
* Decorators: https://waffle.readthedocs.io/en/stable/usage/decorators.html
44+
45+

requirements/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ django-extra-views==0.12.0
1111
django-model-utils==3.1.2
1212
django-extensions==2.1.4
1313
django-image-cropping==1.2
14+
django-waffle==3.0.0
1415
django-webpack-loader==1.6.0
1516
drf-spectacular==0.18.2
1617
django-ratelimit==3.0.1

sapl/base/forms.py

-1
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,6 @@ class Meta:
10091009
'google_recaptcha_site_key',
10101010
'google_recaptcha_secret_key',
10111011
'google_analytics_id_metrica',
1012-
'sapl_as_sapn',
10131012
'identificacao_de_documentos',
10141013
]
10151014

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Generated by Django 2.2.28 on 2023-11-27 00:23
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('base', '0058_appconfig_ordenacao_pesquisa_materia'),
10+
]
11+
12+
operations = [
13+
migrations.RemoveField(
14+
model_name='appconfig',
15+
name='sapl_as_sapn',
16+
),
17+
]

sapl/base/models.py

-4
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,6 @@ class AppConfig(models.Model):
107107
default="",
108108
verbose_name=_('Esfera Federação'),
109109
choices=ESFERA_FEDERACAO_CHOICES)
110-
sapl_as_sapn = models.BooleanField(
111-
verbose_name=_(
112-
'Utilizar SAPL apenas como SAPL-Normas?'),
113-
choices=YES_NO_CHOICES, default=False)
114110

115111
# MÓDULO PARLAMENTARES
116112

sapl/base/views.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
from sapl.sessao.models import (Bancada, SessaoPlenaria)
5151
from sapl.settings import EMAIL_SEND_USER
5252
from sapl.utils import (gerar_hash_arquivo, intervalos_tem_intersecao, mail_service_configured,
53-
SEPARADOR_HASH_PROPOSICAO, show_results_filter_set, google_recaptcha_configured, sapl_as_sapn,
54-
get_client_ip)
53+
SEPARADOR_HASH_PROPOSICAO, show_results_filter_set, google_recaptcha_configured,
54+
get_client_ip, sapn_is_enabled)
5555
from .forms import (AlterarSenhaForm, CasaLegislativaForm, ConfiguracoesAppForm, EstatisticasAcessoNormasForm)
5656
from .models import AppConfig, CasaLegislativa
5757

@@ -62,7 +62,7 @@ def get_casalegislativa():
6262

6363
class IndexView(TemplateView):
6464
def get(self, request, *args, **kwargs):
65-
if sapl_as_sapn():
65+
if sapn_is_enabled():
6666
return redirect('/norma/pesquisar')
6767
return TemplateView.get(self, request, *args, **kwargs)
6868

sapl/context_processors.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
from django.conf import settings
44
from django.utils.translation import ugettext_lazy as _
55

6-
from sapl.utils import google_recaptcha_configured as google_recaptcha_configured_utils,\
7-
sapl_as_sapn as sapl_as_sapn_utils
6+
from sapl.utils import google_recaptcha_configured as google_recaptcha_configured_utils, sapn_is_enabled
87
from sapl.utils import mail_service_configured as mail_service_configured_utils
98

109

@@ -36,10 +35,10 @@ def google_recaptcha_configured(request):
3635
return {'google_recaptcha_configured': True}
3736

3837

39-
def sapl_as_sapn(request):
38+
def enable_sapn(request):
4039
return {
41-
'sapl_as_sapn': sapl_as_sapn_utils(),
40+
'sapl_as_sapn': sapn_is_enabled(),
4241
'nome_sistema': _('Sistema de Apoio ao Processo Legislativo')
43-
if not sapl_as_sapn_utils()
42+
if not sapn_is_enabled()
4443
else _('Sistema de Apoio à Publicação de Leis e Normas')
4544
}

sapl/materia/views.py

-3
Original file line numberDiff line numberDiff line change
@@ -2132,9 +2132,6 @@ def get_context_data(self, **kwargs):
21322132

21332133
context['show_results'] = show_results_filter_set(qr)
21342134

2135-
context['USE_SOLR'] = settings.USE_SOLR if hasattr(
2136-
settings, 'USE_SOLR') else False
2137-
21382135
return context
21392136

21402137

sapl/norma/views.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
MasterDetailCrud, make_pagination)
2929
from sapl.materia.models import Orgao
3030
from sapl.utils import show_results_filter_set, get_client_ip,\
31-
sapl_as_sapn
31+
sapn_is_enabled
3232

3333
from .forms import (AnexoNormaJuridicaForm, NormaFilterSet, NormaJuridicaForm,
3434
NormaPesquisaSimplesForm, NormaRelacionadaForm,
@@ -187,8 +187,6 @@ def get_context_data(self, **kwargs):
187187
context['filter_url'] = ('&' + qr.urlencode()) if len(qr) > 0 else ''
188188

189189
context['show_results'] = show_results_filter_set(qr)
190-
context['USE_SOLR'] = settings.USE_SOLR if hasattr(
191-
settings, 'USE_SOLR') else False
192190

193191
return context
194192

sapl/sessao/views.py

-3
Original file line numberDiff line numberDiff line change
@@ -3997,9 +3997,6 @@ def get_context_data(self, **kwargs):
39973997
context['page_range'] = make_pagination(
39983998
page_obj.number, paginator.num_pages)
39993999

4000-
context['USE_SOLR'] = settings.USE_SOLR if hasattr(
4001-
settings, 'USE_SOLR') else False
4002-
40034000
return context
40044001

40054002
def get(self, request, *args, **kwargs):

sapl/settings.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080
'crispy_forms',
8181
'floppyforms',
8282

83+
'waffle',
84+
8385
'drf_spectacular',
8486
'rest_framework',
8587
'rest_framework.authtoken',
@@ -137,6 +139,7 @@
137139
'django.middleware.security.SecurityMiddleware',
138140
'whitenoise.middleware.WhiteNoiseMiddleware',
139141
'django_prometheus.middleware.PrometheusAfterMiddleware',
142+
'waffle.middleware.WaffleMiddleware',
140143
]
141144
if DEBUG:
142145
INSTALLED_APPS += ('debug_toolbar',)
@@ -210,8 +213,7 @@
210213
'sapl.context_processors.parliament_info',
211214
'sapl.context_processors.mail_service_configured',
212215
'sapl.context_processors.google_recaptcha_configured',
213-
'sapl.context_processors.sapl_as_sapn',
214-
216+
'sapl.context_processors.enable_sapn',
215217
],
216218
'debug': DEBUG
217219
},
@@ -255,6 +257,16 @@
255257
SERVER_EMAIL = config('SERVER_EMAIL', cast=str, default='')
256258
EMAIL_RUNNING = None
257259

260+
# Feature Flag
261+
WAFFLE_FLAG_DEFAULT = False
262+
WAFFLE_SWITCH_DEFAULT = False
263+
WAFFLE_CREATE_MISSING_FLAGS = True
264+
WAFFLE_LOG_MISSING_FLAGS = True
265+
WAFFLE_CREATE_MISSING_SWITCHES = True
266+
WAFFLE_LOG_MISSING_SWITCHES = True
267+
WAFFLE_ENABLE_ADMIN_PAGES = True
268+
269+
258270
MAX_DOC_UPLOAD_SIZE = 150 * 1024 * 1024 # 150MB
259271
MAX_IMAGE_UPLOAD_SIZE = 2 * 1024 * 1024 # 2MB
260272
DATA_UPLOAD_MAX_MEMORY_SIZE = 10 * 1024 * 1024 # 10MB

sapl/templates/base.html

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<!DOCTYPE html>
22
{% load i18n staticfiles menus %}
33
{% load common_tags %}
4+
{% load waffle_tags %}
45
{% load render_bundle from webpack_loader %}
56
{% load webpack_static from webpack_loader %}
67

@@ -11,13 +12,10 @@
1112

1213
<head>
1314
<meta charset="utf-8">
14-
<title>{% block head_title %}{% if sapl_as_sapn %}SAPL-Normas - {% else %}SAPL - {%endif%}{{nome_sistema}}{% endblock %}</title>
15+
<title>{% block head_title %}{% switch "SAPLN_SWITCH" %}SAPL-Normas - {% else %}SAPL - {%endswitch%}{{nome_sistema}}{% endblock %}</title>
1516
<meta name="viewport" content="width=device-width, initial-scale=1.0">
1617
<meta name="theme-color" content="#343a40">
1718

18-
19-
20-
2119
{% block head_content %}
2220
<link rel="icon" href="{% webpack_static 'img/favicon.ico' %}" type="image/png">
2321
{% block webpack_loader_css %}

sapl/templates/base/layouts.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ UserDetail:
2020
AppConfig:
2121

2222
{% trans 'Configurações Gerais' %}:
23-
- esfera_federacao sapl_as_sapn
23+
- esfera_federacao
2424

2525
#{% trans 'Módulo Parlamentares' %}:
2626
#{% trans 'Módulo Mesa Diretora' %}:

sapl/templates/materia/materialegislativa_filter.html

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{% extends "crud/detail.html" %}
22
{% load i18n %}
33
{% load tz %}
4+
{% load waffle_tags %}
45
{% load crispy_forms_tags common_tags%}
56
{% load webpack_static from webpack_loader %}
67

78
{% block actions %}
89

910
<div class="actions btn-group" role="group">
10-
{% if USE_SOLR %}
11-
<a href="{% url 'sapl.base:haystack_search' %}" class="btn btn-outline-primary">
12-
Pesquisa Textual
13-
</a>
14-
{% endif %}
15-
11+
{% switch "SOLR_SWITCH" %}
12+
<a href="{% url 'sapl.base:haystack_search' %}" class="btn btn-outline-primary">
13+
Pesquisa Textual
14+
</a>
15+
{% endswitch %}
1616
{% if perms.materia.add_materialegislativa %}
1717
<a href="{% url 'sapl.materia:materialegislativa_create' %}" class="btn btn-outline-primary">
1818
{% blocktrans with verbose_name=view.verbose_name %} Adicionar Matéria Legislativa {% endblocktrans %}

sapl/templates/norma/normajuridica_detail.html

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{% extends "crud/detail.html" %}
22
{% load i18n common_tags %}
3+
{% load waffle_tags %}
34
{% load render_bundle from webpack_loader %}
45

56
{% block webpack_loader_css %}

sapl/templates/norma/normajuridica_filter.html

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{% extends "crud/detail.html" %}
22
{% load i18n %}
3+
{% load waffle_tags %}
34
{% load crispy_forms_tags common_tags %}
45

56
{% block actions %}
67
<div class="actions btn-group float-right" role="group">
7-
{% if USE_SOLR %}
8+
{% switch "SOLR_SWITCH" %}
89
<a href="{% url 'sapl.base:haystack_search' %}" class="btn btn-outline-primary">
910
Pesquisa Textual
1011
</a>
11-
{% endif %}
12+
{% endswitch %}
1213
{% if perms.norma.add_normajuridica %}
1314
<a href="{% url 'sapl.norma:normajuridica_create' %}" class="btn btn-outline-primary">
1415
{% blocktrans with verbose_name=view.verbose_name %} Adicionar Norma Jurídica {% endblocktrans %}

sapl/templates/search/search.html

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{% extends 'crud/form.html' %}
22
{% load crispy_forms_tags %}
33
{% load common_tags %}
4-
4+
{% load waffle_tags %}
55
{% block base_content %}
66
<h1><legend>Pesquisa Textual</legend></h1>
77
</br>
@@ -11,9 +11,7 @@ <h1><legend>Pesquisa Textual</legend></h1>
1111
{{ form.q|as_crispy_field }}
1212
</div>
1313
</div>
14-
15-
{% if not sapl_as_sapn %}
16-
14+
{% switch "SAPLN_SWITCH" %}
1715
<div class="row">
1816
<div class="col-md-8">
1917
<h3> Em quais tipos de documento deseja pesquisar?</h3>
@@ -27,7 +25,7 @@ <h3> Em quais tipos de documento deseja pesquisar?</h3>
2725
{{ form.models }}
2826
</div>
2927
</div>
30-
{% endif %}
28+
{% endswitch %}
3129

3230
<div class="row">
3331
<div class="col-md-12">

sapl/templates/sessao/sessaoplenaria_filter.html

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{% extends "crud/detail.html" %}
22
{% load i18n %}
3+
{% load waffle_tags %}
34
{% load crispy_forms_tags common_tags %}
45

56
{% block actions %}
67
<div class="actions btn-group float-right" role="group">
7-
{% if USE_SOLR %}
8+
{% switch "SOLR_SWITCH" %}
89
<a href="{% url 'sapl.base:haystack_search' %}" class="btn btn-outline-primary">
910
Pesquisa Textual
1011
</a>
11-
{% endif %}
12+
{% endswitch %}
1213
{% if perms.sessao %}
1314
<a href="{% url 'sapl.sessao:sessaoplenaria_create' %}" class="btn btn-outline-primary">
1415
{% blocktrans with verbose_name=view.verbose_name %} Adicionar Sessão Plenária {% endblocktrans %}

0 commit comments

Comments
 (0)