Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,31 @@ Esta aplicación permite preguntarle a los usuarios de un workspace de slack que


## Configuración bot slack ##

Primero deves crear una app de slack en el siguiente link: [Crear slack app](https://api.slack.com/apps?new_app=1)
Lo invitas a tu workspace con con los siguientes permisos:
* chat:write
* im:history
* im:write
* users:read


Modifica los siguientes settings:
CLIENT_ID = [your-id]
CLIENT_SECRET = [your-secret]
VERIFICATION_TOKEN = [your-verification-token]
BOT_USER_ACCESS_TOKEN = [your-bot-user-access-token]

## Configuramos el ambiente ##



## Configuraciones backend ##
Este codigo esta testeado para python 3.8 por lo cual se recomienda mantener la versión.

Instalamos las librerias:
pip install -r backend/requirements.txt





17 changes: 17 additions & 0 deletions backend/meal_manager/migrations/0002_auto_20200603_1948.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.0.3 on 2020-06-03 23:48

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('meal_manager', '0001_initial'),
]

operations = [
migrations.AlterUniqueTogether(
name='order',
unique_together={('worker', 'date')},
),
]
2 changes: 2 additions & 0 deletions backend/meal_manager/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ class Order(models.Model):
date = models.DateField()
date_update = models.DateTimeField(auto_now=True)
date_creation = models.DateTimeField(auto_now_add=True)
class Meta:
unique_together = ('worker', 'date')

11 changes: 6 additions & 5 deletions backend/meal_manager/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ class Meta:
model = Worker
fields = '__all__'

class MenuSerializer(serializers.ModelSerializer):
class Meta:
model = Menu
fields = '__all__'

class MealSerializer(serializers.ModelSerializer):
class Meta:
model = Meal
Expand All @@ -22,6 +17,12 @@ def create(self, validated_data):
meal = Meal.objects.create(menu_id = menu_id, **validated_data)
return meal

class MenuSerializer(serializers.ModelSerializer):
meals = MealSerializer(read_only=True, many=True)
class Meta:
model = Menu
fields = '__all__'

class OrderSerializer(serializers.ModelSerializer):
class Meta:
model = Order
Expand Down
60 changes: 60 additions & 0 deletions backend/meal_manager/tasks/send_menu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from app.celery import app
from django.conf import settings
from meal_manager.models import Menu, Worker
from slackapp.connection.slack_connection import SlackConnection
import traceback


def update_users(users):

for user in users:
worker, _ = Worker.objects.get_or_create(
name = user['real_name'],
slack_id = user['id']
)
if _:
print(f'worker {worker.name} added.')


@app.task
def send_menu(data):
"""
this task send the menu to all workers of a workspace.
the steps are the following:
1) check if the menu exists.
2) check if the menu contains any meal.
3) build a list of strings with the meals.
2) update the users info.
4) check if the users
3) send the menu to all the users.
"""
#checking
if not data.get('menu_id'):
raise Exception('menu_id is required.')
try:
menu = Menu.objects.get(pk = data['menu_id'])
except Menu.DoesNotExist:
raise Exception('fails to get menu')

meals = menu.meals.all()
if meals.count() == 0:
raise Exception("menu does not contain any meal")

#build
meal_list = [meal.name for meal in meals]

#initialize slack
slack = SlackConnection()
try:
users = slack.get_users()
update_users(users)
except:
raise Exception('fails to update users')

sended = slack.send_menu_to_users(meal_list)
if sended:
print('menu sended correctly.')
return None
else:
raise Exception('fails to send the menu to slack.')

103 changes: 0 additions & 103 deletions backend/meal_manager/tests/test_order.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

class MealTest(TestCase):
""" this test has the following attempts:
1) create a valid meal and expect a 201 status code.
2) trying to create a invalid meal and expect a 400 status code.
3) trying to create a meal has unauthorized user and expect a 401 status code.
4) trying to get the meal list has unauthorized user and expect a 401 status code.
5) trying to create two meals with the same name for the same menu and expect a 409 status code.
6) trying to create a meal for an unexist menu and expect a 404 code
7) create two meals and get the list of meals expecting a 200 status code and length 2.
- Create a valid meal and expect a 201 status code.
- Trying to create a invalid meal and expect a 400 status code.
- Trying to create a meal has unauthorized user and expect a 401 status code.
- Trying to get the meal list has unauthorized user and expect a 401 status code.
- Trying to create two meals with the same name for the same menu and expect a 409 status code.
- Trying to create a meal for an unexist menu and expect a 404 code.
- Create two meals and get the list of meals expecting a 200 status code and length 2.
"""

client = Client()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

class MenuTest(TestCase):
""" this test has the following attempts:
1) create a valid menu and expect a 201 status code.
2) trying to create a invalid menu and expet a 400 status code.
3) trying to create a munu has unauthorized user and expect a 401 status code.
4) trying to get the menu list has unauthorized user and expect a 401 status code.
5) trying to create two menus for the same day and expect a 409 status code.
6) create two menus and get the list of menus expecting a 200 status code and length 2.
- Create a valid menu and expect a 201 status code.
- Trying to create a invalid menu and expet a 400 status code.
- Trying to create a munu has unauthorized user and expect a 401 status code.
- Trying to get the menu list has unauthorized user and expect a 401 status code.
- Trying to create two menus for the same day and expect a 409 status code.
- Create two menus and get the list of menus expecting a 200 status code and length 2.
"""

client = Client()
Expand Down
66 changes: 66 additions & 0 deletions backend/meal_manager/tests/views/test_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from django.test import TestCase, Client
from meal_manager.models import User, Menu, Worker, Order, Meal
from rest_framework import status
from django.urls import reverse
from meal_manager.utils.jwt_token import jwt_payload_handler, jwt_encode_handler
import json

class OrderTest(TestCase):
""" this test has the following attempts:
- Get all the orders of a meal.
"""

client = Client()

def setUp(self):
user = User.objects.create_user(
email = "[email protected]",
password = "pass",
)
user.save()
payload = jwt_payload_handler(user)
self.token = jwt_encode_handler(payload)


def test_get_orders_of_a_meal(self):
menu = Menu.objects.create(
name = "menu test 1",
date = "2020-02-01"
)
meal = Meal.objects.create(
menu_id = menu.id,
name = "vegetarian mix"
)
worker_1 = Worker.objects.create(
name = "order test",
slack_id = "sadlgwm2iqw"
)
worker_2 = Worker.objects.create(
name = "order test",
slack_id = "sadlgwm2iqw"
)
Order.objects.create(
meal_id = meal.id,
worker_id = worker_2.id,
date = menu.date
)
Order.objects.create(
meal_id = meal.id,
worker_id = worker_1.id,
date = menu.date,
customization = "without salad"
)
response = self.client.get(
reverse(
'order_list',
kwargs = {
'menu_id': menu.id,
'meal_id': meal.id
}
),
content_type='application/json',
HTTP_AUTHORIZATION = 'JWT ' + self.token
)
len_menus = len(json.loads(response.content))
self.assertEqual(len_menus, 2)
self.assertEqual(response.status_code, status.HTTP_200_OK)
Loading