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

Issue 3 #16

Open
wants to merge 28 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
1b8f803
set up celery and rabbitmq
Joaquinecc Mar 26, 2021
f192655
Notify moderator that a new registration came in
Joaquinecc Mar 27, 2021
1a66bac
Notify registrants that theiy have been approved
Joaquinecc Mar 27, 2021
f1a453e
setting enviroment variable
Joaquinecc Mar 27, 2021
b9e1c29
add celery service
Joaquinecc Mar 28, 2021
7576cb2
Merge branch 'develop' into issue_3
Joaquinecc Mar 30, 2021
65aaf05
center Container 'Invesigadores del paraguay'
Joaquinecc Mar 30, 2021
1518a5a
Add new tools to Readme
Joaquinecc Mar 30, 2021
4aaab69
new template email registration
Joaquinecc Mar 30, 2021
3850061
new template approve registration
Joaquinecc Mar 31, 2021
4b3028f
add new raabbitmq .env.dev variable
Joaquinecc Mar 31, 2021
d99ada2
Change DEFAULT_APP_EMAIL_ADDRESS to ADMIN_EMAIL_AD
Joaquinecc Apr 1, 2021
685ece5
text changes on email templates
Joaquinecc Apr 1, 2021
f6dc1af
Modify Readme
Joaquinecc Apr 1, 2021
41a8313
Add new email env varables
Joaquinecc Apr 1, 2021
2936c55
specify version rabbit image
Joaquinecc Apr 1, 2021
067f41d
modify local variable name i=onnotify_user_appr
Joaquinecc Apr 1, 2021
c69d507
Modify readme
Joaquinecc Apr 1, 2021
6dc9dfa
modify readme
Joaquinecc Apr 1, 2021
86e34df
MODERATOR_EMAIL_ADDRESSES
Joaquinecc Apr 5, 2021
c1a9af9
send email with zoho
Joaquinecc Apr 6, 2021
60370ed
add more styling email templates
Joaquinecc Apr 6, 2021
d8c0c6e
move celery broker variable
Joaquinecc Apr 11, 2021
27de7df
Add more style[approved_email.html]
Joaquinecc Apr 11, 2021
7313549
Update Readme
Joaquinecc Apr 17, 2021
13a077d
remove unnecesary ADMIN_EMAIL_ADDRESS env variable
Joaquinecc May 5, 2021
d5c479c
remove $ on CELERY_BROKER_URL
Joaquinecc May 22, 2021
33777e6
reedit template email aproved
Joaquinecc May 25, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .env.dev.sample
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@ SQL_PASSWORD=
SQL_HOST=db
SQL_PORT=3306
GOOGLE_MAPS_API_KEY=
DATABASE=mysql
DATABASE=mysql
EMAIL_BACKEND=django.core.mail.backends.smtp.EmailBackend
EMAIL_HOST=smtp.gmail.com
EMAIL_HOST_PASSWORD=
[email protected]
[email protected],[email protected]
RABBITMQ_DEFAULT_USER=guest
RABBITMQ_DEFAULT_PASS=guest
EMAIL_PORT=587
EMAIL_USE_TLS=1
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ RUN apk --no-cache add --virtual build-dependencies \
gcc \
libc-dev \
libffi-dev \
mariadb-dev
Joaquinecc marked this conversation as resolved.
Show resolved Hide resolved
mariadb-dev

# install app dependencies
RUN pip install --upgrade pip
Expand All @@ -28,4 +28,4 @@ COPY ./entrypoint.sh /usr/src/app/entrypoint.sh
COPY ./ /usr/src/app/

# run entrypoint.sh
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]
Joaquinecc marked this conversation as resolved.
Show resolved Hide resolved
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]
joausaga marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ A live version of CIPE can be accessed [here](https://investigadoresparaguayosen

## Installation

Install docker and docker-compose in your local machine. Check the official installation [guidelines](https://docs.docker.com/install/);
joausaga marked this conversation as resolved.
Show resolved Hide resolved

### Production

For the impatient: The only key you need to get a **really quick start**, is a valid Google Map API KEY, all the others default credentials will be used. However your are encouraged to complete the setup process if your planning to use this proyect in the long run.
1. Obtain a google maps api key by following the instructions [here](https://developers.google.com/maps/documentation/embed/get-api-key);
2. Clone the repository `git clone https://github.com/joausaga/cipe.git`;
3. Get into the directory `cipe`;
0. For the impatient: The only key you need to get a **really quick start**, is a valid Google Map API KEY, all the others default credentials will be used. However your are encouraged to complete the setup process if your planning to use this proyect in the long run.
1. Install docker and docker-compose in your local machine. Check the official installation [guidelines](https://docs.docker.com/install/);
2. Obtain a google maps api key by following the instructions [here](https://developers.google.com/maps/documentation/embed/get-api-key);
3. Clone the repository `git clone https://github.com/joausaga/cipe.git`;
4. Get into the directory `cipe`;
4. Run `scripts/prepare-config-templates.sh`
5. Set the configuration parameters of the database in `.env.prod.db`;
Joaquinecc marked this conversation as resolved.
Show resolved Hide resolved
6. Set the SECRET_KEY and GOOGLE_MAPS_API_KEY obtained before as well as the configuration parameters
Expand Down Expand Up @@ -59,6 +56,8 @@ for accessing public information about the [BECAL](http://www.becal.gov.py/) fe
2. [MySQL Community Server](https://www.mysql.com/downloads/)
3. [Django 2.2](https://www.djangoproject.com)
4. Google Maps
5. [Rabbitmqctl](https://www.rabbitmq.com)
6. [Celery](https://docs.celeryproject.org)

## Issues

Expand Down
17 changes: 17 additions & 0 deletions app/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import logging
import re

from app.tasks import send_approved_email_task

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -54,10 +55,26 @@ def affiliation(self, obj):
except:
return ''
affiliation.short_description = 'Affiliation'
def save_model(self, request, obj, form, change):
if(change):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

De donde sale la variable change?

try:
current_obj=Scientist.objects.get(id=obj.id)
self.notify_user_approved(obj,current_obj)
except:
logger.info("Error ocurred sending email of aproven user")

super().save_model(request, obj, form, change)

def notify_user_approved(self,obj,current_obj):
if(obj.approved and obj.approved != current_obj.approved):
send_approved_email_task.delay(obj.first_name ,obj.slug,obj.email)


def approve_scientists(self, request, queryset):
for scientist in queryset:
scientist.approved = True
current_scientist=Scientist.objects.get(id=scientist.id)
self.notify_user_approved(scientist,current_scientist)
scientist.save()
approve_scientists.short_description = 'Approve scientists'

Expand Down
41 changes: 41 additions & 0 deletions app/email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

from django.template import Context
from django.template.loader import render_to_string
from django.core.mail import EmailMessage
from django.conf import settings

def send_new_email_registration(name,position, institution):

context = {
'name': name,
'position':position,
'institution':institution
}
email_subject = 'Nuevo Registro ingresado'
html_message = render_to_string('email/new_email_registration.html', context)
email = EmailMessage(
subject=email_subject,
body=html_message,
from_email= settings.DEFAULT_FROM_EMAIL,
to=[settings.MODERATOR_EMAIL_ADDRESSES],
)
email.content_subtype = 'html'
return email.send()

def send_approved_email(name,slug,to_email):

context = {
'name': name,
'slug': slug,
}
email_subject = 'Tu registro en el sitio Investigadores Paraguayos en el Mundo ha sido aprobado'
html_message = render_to_string('email/approved_email.html', context)
email = EmailMessage(
Joaquinecc marked this conversation as resolved.
Show resolved Hide resolved
subject=email_subject,
body=html_message,
from_email= settings.DEFAULT_FROM_EMAIL,
to=[to_email],
)
email.content_subtype = 'html'
return email.send()

4 changes: 2 additions & 2 deletions app/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ class RegistrationForm(forms.Form):
location_name = forms.CharField(widget=forms.HiddenInput(), required=False)
location_lat = forms.CharField(widget=forms.HiddenInput(), required=False)
location_lng = forms.CharField(widget=forms.HiddenInput(), required=False)


Joaquinecc marked this conversation as resolved.
Show resolved Hide resolved
class RegistrationEditForm(forms.Form):
first_name = forms.CharField(label='Nombre *', widget=forms.TextInput(
attrs={
Expand Down
16 changes: 16 additions & 0 deletions app/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from celery.decorators import task
from celery.utils.log import get_task_logger
from .email import send_new_email_registration,send_approved_email

logger=get_task_logger(__name__)

@task(name="send_new_registration_email_task")
def send_new_registration_email_task(name,position,institution):
logger.info("Sending email to moderators ... ")
return send_new_email_registration(name,position,institution)


@task(name="send_approved_email_task")
def send_approved_email_task(name,slug,email):
logger.info("Sent approve email task")
return send_approved_email(name,slug,email)
30 changes: 30 additions & 0 deletions app/templates/email/approved_email.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

<div style="max-width: 1088px;">
<img src="https://user-images.githubusercontent.com/2123231/112866846-8c792b00-90ba-11eb-85ae-9fd161c93f83.png" style="display:block; width: 100%;
max-width: 1088px;">
<br><br>
Hola <strong> {{ name }}</strong>.
<br><br>
Tu registro en el sitio <a href="https://investigadoresparaguayosenelmundo.com" target="_blank">
Investigadores Paraguayos en el Mundo</a> ha sido aprobado. Puedes verificar tu información en el sitio accediendo al mapa
<a href="https://investigadoresparaguayosenelmundo.com/map" targe="_blank">aquí</a>.
<br><br>
Puedes modificar datos de tu registro <a href="https://investigadoresparaguayosenelmundo.com/registration/edit/{{ slug }}">aqui</a>.
<br><br>
<strong>
Recuerda que el objetivo del sitio es:
</strong>
<br><br>
<ol>
<li> Generar colaboraciones entre académicos paraguayos residiendo en el extranjero y trabajando en áreas afines con el potencial de iniciar lineas de investigación que puedan derivar en investigaciones conjuntas en el futuro;</li>
<li>Fortalecer vínculos entre investigadores paraguayos residiendo en lugares próximos propiciando la creación de comunidades locales para el intercambio de ideas, conocimientos, y experiencias</li>
</ol>
<br><br>
<strong> Te animo a aprovechar la información del sitio para establecer contacto y colaboración que puedan ayudarte en tus actividades académicas..</strong>
<br><br>
Cualquier otra cosa, estoy a las órdenes.
<br><br>
Saludos!
<br><br>
Jorge Saldivar Galli
</div>
13 changes: 13 additions & 0 deletions app/templates/email/new_email_registration.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div>
<br><br>
Hola!
<br><br>

<strong> {{name|safe}} </strong> afiliado a <strong> {{institution|safe}} </strong> en calidad de <strong> {{position|safe}} </strong> desea registrarse en la plataforma Investigadores Paraguayos en el Mundo.
<br><br>

Favor ingrese al panel de administración para aprobar el registro.
<br><br>

<strong>Gracias</strong>
</div>
8 changes: 6 additions & 2 deletions app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
from django.forms.models import model_to_dict
from django.shortcuts import render
from django.http import HttpResponse


from app.tasks import send_new_registration_email_task
logger = logging.getLogger(__name__)
countries_iso2 = load_countries_iso2()

Expand Down Expand Up @@ -220,6 +219,11 @@ def registration(request):
'institution': inst_obj})
msg = f"Registro exitoso! Luego de su aprobación, los datos podrán ser " \
f"visualizados en el mapa de investigadores."

#Send mail to notify new registration to moderator
send_new_registration_email_task.delay(scientist_obj.first_name+' '+scientist_obj.last_name,scientist_obj.position,inst_obj.name)


form = RegistrationForm()
registration_error = 0
else:
Expand Down
3 changes: 3 additions & 0 deletions cipe/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .celery import app as celery_app

__all__ = ('celery_app',)
22 changes: 22 additions & 0 deletions cipe/celery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os

from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'cipe.settings')

app = Celery('cipe')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
print(f'Request: {self.request!r}')
14 changes: 14 additions & 0 deletions cipe/settings.py.sample
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,17 @@ LOGGING = {
}
}
}


CELERY_BROKER_URL = "amqp://{}:{}@rabbit:5672/%2F".format(os.environ.get('RABBITMQ_DEFAULT_USER'),os.environ.get('RABBITMQ_DEFAULT_PASS'))


EMAIL_BACKEND = os.environ.get("EMAIL_BACKEND")
EMAIL_HOST = os.environ.get("EMAIL_HOST")
EMAIL_PORT = os.environ.get("EMAIL_PORT")
EMAIL_USE_TLS = int(os.environ.get("EMAIL_PORT"))

EMAIL_HOST_USER = os.environ.get('ADMIN_EMAIL_ADDRESS')
EMAIL_HOST_PASSWORD=os.environ.get('EMAIL_HOST_PASSWORD')
DEFAULT_FROM_EMAIL=os.environ.get('ADMIN_EMAIL_ADDRESS')
MODERATOR_EMAIL_ADDRESSES= os.environ.get('MODERATOR_EMAIL_ADDRESSES').split(",")
26 changes: 25 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
version: '3.7'

services:
rabbit:
hostname: rabbit
image: rabbitmq:3.8.14-management
environment:
- RABBITMQ_DEFAULT_USER=${RABBITMQ_DEFAULT_USER}
- RABBITMQ_DEFAULT_PASS=${RABBITMQ_DEFAULT_PASS}
ports:
- "5672:5672"
- "15672:15672"
app:
restart: always
build: .
Expand All @@ -25,4 +34,19 @@ services:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT}
- MYSQL_DATABASE=${SQL_DATABASE}
- MYSQL_USER=${SQL_USER}
- MYSQL_PASSWORD=${SQL_PASSWORD}
- MYSQL_PASSWORD=${SQL_PASSWORD}
celery_worker:
build: .
command: celery -A cipe worker --loglevel=info
volumes:
- .:/usr/src/app/
environment:
- DEBUG=${DEBUG}
- DJANGO_ALLOWED_HOSTS=${DJANGO_ALLOWED_HOSTS}
- CELERY_BROKER=amqp://${RABBITMQ_DEFAULT_USER}:${RABBITMQ_DEFAULT_PASS}@rabbit:5672/%2F
depends_on:
- app
- rabbit
env_file:
- ./.env.dev

17 changes: 16 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
amqp==5.0.5
billiard==3.6.3.0
celery==5.0.5
certifi==2019.6.16
chardet==3.0.4
click==7.1.2
click-didyoumean==0.0.3
click-plugins==1.1.1
click-repl==0.1.6
Django==2.2.10
googlemaps==3.0.2
gunicorn==19.9.0
idna==2.8
importlib-metadata==3.7.3
kombu==5.0.2
mysqlclient==1.4.2.post1
prompt-toolkit==3.0.18
pytz==2019.1
requests==2.22.0
six==1.15.0
sqlparse==0.3.0
typing-extensions==3.7.4.3
urllib3==1.25.3
gunicorn==19.9.0
vine==5.0.0
wcwidth==0.2.5
zipp==3.4.1