Skip to content

Commit 129f386

Browse files
authored
Merge pull request #7 from josseed/connecting-frontend
connecting frontend
2 parents 15ccdf1 + 65f7516 commit 129f386

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+964
-371
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,31 @@ Esta aplicación permite preguntarle a los usuarios de un workspace de slack que
66

77

88
## Configuración bot slack ##
9-
9+
Primero deves crear una app de slack en el siguiente link: [Crear slack app](https://api.slack.com/apps?new_app=1)
10+
Lo invitas a tu workspace con con los siguientes permisos:
1011
* chat:write
1112
* im:history
1213
* im:write
1314
* users:read
1415

1516

17+
Modifica los siguientes settings:
18+
CLIENT_ID = [your-id]
19+
CLIENT_SECRET = [your-secret]
20+
VERIFICATION_TOKEN = [your-verification-token]
21+
BOT_USER_ACCESS_TOKEN = [your-bot-user-access-token]
22+
23+
## Configuramos el ambiente ##
24+
25+
26+
27+
## Configuraciones backend ##
28+
Este codigo esta testeado para python 3.8 por lo cual se recomienda mantener la versión.
29+
30+
Instalamos las librerias:
31+
pip install -r backend/requirements.txt
32+
33+
34+
1635

1736

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Generated by Django 3.0.3 on 2020-06-03 23:48
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('meal_manager', '0001_initial'),
10+
]
11+
12+
operations = [
13+
migrations.AlterUniqueTogether(
14+
name='order',
15+
unique_together={('worker', 'date')},
16+
),
17+
]

backend/meal_manager/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@ class Order(models.Model):
4040
date = models.DateField()
4141
date_update = models.DateTimeField(auto_now=True)
4242
date_creation = models.DateTimeField(auto_now_add=True)
43+
class Meta:
44+
unique_together = ('worker', 'date')
4345

backend/meal_manager/serializers.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ class Meta:
66
model = Worker
77
fields = '__all__'
88

9-
class MenuSerializer(serializers.ModelSerializer):
10-
class Meta:
11-
model = Menu
12-
fields = '__all__'
13-
149
class MealSerializer(serializers.ModelSerializer):
1510
class Meta:
1611
model = Meal
@@ -22,6 +17,12 @@ def create(self, validated_data):
2217
meal = Meal.objects.create(menu_id = menu_id, **validated_data)
2318
return meal
2419

20+
class MenuSerializer(serializers.ModelSerializer):
21+
meals = MealSerializer(read_only=True, many=True)
22+
class Meta:
23+
model = Menu
24+
fields = '__all__'
25+
2526
class OrderSerializer(serializers.ModelSerializer):
2627
class Meta:
2728
model = Order
File renamed without changes.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from app.celery import app
2+
from django.conf import settings
3+
from meal_manager.models import Menu, Worker
4+
from slackapp.connection.slack_connection import SlackConnection
5+
import traceback
6+
7+
8+
def update_users(users):
9+
10+
for user in users:
11+
worker, _ = Worker.objects.get_or_create(
12+
name = user['real_name'],
13+
slack_id = user['id']
14+
)
15+
if _:
16+
print(f'worker {worker.name} added.')
17+
18+
19+
@app.task
20+
def send_menu(data):
21+
"""
22+
this task send the menu to all workers of a workspace.
23+
the steps are the following:
24+
1) check if the menu exists.
25+
2) check if the menu contains any meal.
26+
3) build a list of strings with the meals.
27+
2) update the users info.
28+
4) check if the users
29+
3) send the menu to all the users.
30+
"""
31+
#checking
32+
if not data.get('menu_id'):
33+
raise Exception('menu_id is required.')
34+
try:
35+
menu = Menu.objects.get(pk = data['menu_id'])
36+
except Menu.DoesNotExist:
37+
raise Exception('fails to get menu')
38+
39+
meals = menu.meals.all()
40+
if meals.count() == 0:
41+
raise Exception("menu does not contain any meal")
42+
43+
#build
44+
meal_list = [meal.name for meal in meals]
45+
46+
#initialize slack
47+
slack = SlackConnection()
48+
try:
49+
users = slack.get_users()
50+
update_users(users)
51+
except:
52+
raise Exception('fails to update users')
53+
54+
sended = slack.send_menu_to_users(meal_list)
55+
if sended:
56+
print('menu sended correctly.')
57+
return None
58+
else:
59+
raise Exception('fails to send the menu to slack.')
60+

backend/meal_manager/tests/test_order.py

Lines changed: 0 additions & 103 deletions
This file was deleted.

backend/meal_manager/tests/test_meal.py renamed to backend/meal_manager/tests/views/test_meal.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

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

1919
client = Client()

0 commit comments

Comments
 (0)