Skip to content

Commit

Permalink
Merge pull request #142 from CodeHive-Solutions/dev
Browse files Browse the repository at this point in the history
Fix bug in cyc points and goals
  • Loading branch information
S-e-b-a-s authored Nov 12, 2024
2 parents bc89eaf + 5f9e420 commit 7de9153
Show file tree
Hide file tree
Showing 8 changed files with 2,310 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 5.0.8 on 2024-11-12 11:23

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('goals', '0006_alter_goals_cedula_alter_historicalgoals_cedula'),
]

operations = [
migrations.AlterField(
model_name='historicaltableinfo',
name='days',
field=models.IntegerField(max_length=350),
),
migrations.AlterField(
model_name='tableinfo',
name='days',
field=models.IntegerField(max_length=350),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 5.0.8 on 2024-11-12 11:24

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('goals', '0007_alter_historicaltableinfo_days_alter_tableinfo_days'),
]

operations = [
migrations.AlterField(
model_name='historicaltableinfo',
name='days',
field=models.IntegerField(),
),
migrations.AlterField(
model_name='tableinfo',
name='days',
field=models.IntegerField(),
),
]
2 changes: 1 addition & 1 deletion INSIGHTSAPI/goals/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class TableInfo(models.Model):
name = models.CharField(max_length=50)
fringe = models.CharField(max_length=100)
diary_goal = models.IntegerField()
days = models.CharField(max_length=350)
days = models.IntegerField()
month_goal = models.IntegerField()
hours = models.IntegerField()
collection_account = models.IntegerField()
Expand Down
80 changes: 73 additions & 7 deletions INSIGHTSAPI/goals/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from django.urls import reverse
from django.utils import timezone
from rest_framework import status

from services.tests import BaseTestCase
from users.models import User

Expand Down Expand Up @@ -91,7 +90,7 @@ def test_metas_upload(self, called=False):
# Assert the response status code and perform additional assertions for the response data
number_goals = Goals.objects.all().count()
# print(response.data)
self.assertEqual(response.status_code, 201)
self.assertEqual(response.status_code, 201, response.data)
self.assertTrue(number_goals > 0)

def test_metas_upload_without_permission(self):
Expand Down Expand Up @@ -133,7 +132,7 @@ def test_execution_upload(self, called=False):
# Send the POST request to the upload-excel URL with the Excel file data
response = self.client.post(reverse("goal-list"), {"file": excel_file})
# Assert the response status code and perform additional assertions for the response data
self.assertEqual(response.status_code, 201)
self.assertEqual(response.status_code, 201, response.data)
count = Goals.objects.exclude(total="").count()
self.assertTrue(count > 0)

Expand All @@ -145,7 +144,7 @@ def test_borrado_accepted_with_another_upload(self):
self.test_metas_upload(called=True)
# See if there are goals created
number_goals = Goals.objects.all().count()
self.assertTrue(number_goals > 0)
self.assertTrue(number_goals > 0)
# put accepted to True and accepted_at to now() to Goals
Goals.objects.all().update(accepted=True, accepted_at=timezone.now())
# Read the file again and upload it
Expand All @@ -159,7 +158,7 @@ def test_borrado_accepted_with_another_upload(self):
)
# Send the POST request to the upload-excel URL with the Excel file data
response = self.client.post(reverse("goal-list"), {"file": excel_file})
self.assertEqual(response.status_code, 201)
self.assertEqual(response.status_code, 201, response.data)
# See if the accepted goals were deleted
first_goal = Goals.objects.exclude(accepted_at=None).first()
self.assertIsNone(first_goal)
Expand Down Expand Up @@ -193,9 +192,63 @@ def test_claro_upload(self):
# Send the POST request to the upload-excel URL with the Excel file data
response = self.client.post(reverse("goal-list"), {"file": excel_file})
# Assert the response status code and perform additional assertions for the response data
self.assertEqual(response.status_code, 201)
self.assertEqual(response.status_code, 201, response.data)
self.assertTrue(Goals.objects.all().count() > 0)
self.assertTrue(TableInfo.objects.all().count() > 0)
ancient_0 = TableInfo.objects.filter(name="ANTIGUOS", fringe="CERO").first()
self.assertEqual(ancient_0.diary_goal, 70)
self.assertEqual(ancient_0.days, 24)
self.assertEqual(ancient_0.month_goal, 1680)
self.assertEqual(ancient_0.hours, 8)
self.assertEqual(ancient_0.collection_account, 83000)
ancient_30 = TableInfo.objects.filter(name="ANTIGUOS", fringe="30").first()
self.assertEqual(ancient_30.diary_goal, 40)
self.assertEqual(ancient_30.days, 22)
self.assertEqual(ancient_30.month_goal, 960)
self.assertEqual(ancient_30.hours, 4)
self.assertEqual(ancient_30.collection_account, 183000)
ancient_60 = TableInfo.objects.filter(name="ANTIGUOS", fringe="60").first()
self.assertEqual(ancient_60.diary_goal, 12)
self.assertEqual(ancient_60.days, 56)
self.assertEqual(ancient_60.month_goal, 288)
self.assertEqual(ancient_60.hours, 1)
self.assertEqual(ancient_60.collection_account, 78000)
ancient_90 = TableInfo.objects.filter(name="ANTIGUOS", fringe="90").first()
self.assertEqual(ancient_90.diary_goal, 12)
self.assertEqual(ancient_90.days, 87)
self.assertEqual(ancient_90.month_goal, 288)
self.assertEqual(ancient_90.hours, 1)
self.assertEqual(ancient_90.collection_account, 160000)
ancient_120A180 = TableInfo.objects.filter(
name="ANTIGUOS", fringe="120A180"
).first()
self.assertEqual(ancient_120A180.diary_goal, 9)
self.assertEqual(ancient_120A180.days, 15)
self.assertEqual(ancient_120A180.month_goal, 216)
self.assertEqual(ancient_120A180.hours, 1)
self.assertEqual(ancient_120A180.collection_account, 320000)
ancient_210 = TableInfo.objects.filter(name="ANTIGUOS", fringe="210").first()
ancient_prepotencial = TableInfo.objects.filter(
name="ANTIGUOS", fringe="PREPOTENCIAL"
).first()
ancient_prepotencial_2 = TableInfo.objects.filter(
name="ANTIGUOS", fringe="PREPOTENCIAL_2"
).first()
ancient_preprovision = TableInfo.objects.filter(
name="ANTIGUOS", fringe="PREPROVISION"
).first()
ancient_churn = TableInfo.objects.filter(
name="ANTIGUOS", fringe="CHURN"
).first()
ancient_prechurn = TableInfo.objects.filter(
name="ANTIGUOS", fringe="PRECHURN"
).first()
ancient_provision = TableInfo.objects.filter(
name="ANTIGUOS", fringe="PROVISION"
).first()
ancient_potencial = TableInfo.objects.filter(
name="ANTIGUOS", fringe="POTENCIAL"
).first()
self.assertTrue(Goals.objects.all().exclude(table_goal=None).count() > 0)

def test_get_history(self):
Expand Down Expand Up @@ -226,7 +279,7 @@ def test_get_history(self):
)
# Send the POST request to the upload-excel URL with the Excel file data
response = self.client.post(reverse("goal-list"), {"file": excel_file})
self.assertEqual(response.status_code, 201)
self.assertEqual(response.status_code, 201, response.data)
# Check with a month that has twice
response = self.client.get("/goals/?date=ENERO-2022&column=delivery")
self.assertEqual(response.status_code, 200)
Expand Down Expand Up @@ -540,3 +593,16 @@ def test_patch_goal_denied(self):
"Patch request solo acepta el campo 'accepted' o 'accepted_execution'.",
response.data,
)

def test_csv_not_allowed(self):
"""Test the upload-excel view."""
file_path = "utils/excels/Entrega de metas-ENERO-2018.xlsx"
with open(file_path, "rb") as file_obj:
file_data = file_obj.read()
excel_file = SimpleUploadedFile(
"Entrega de metas-ENERO-2018.csv",
file_data,
content_type="application/vnd.ms-excel",
)
response = self.client.post(reverse("goal-list"), {"file": excel_file})
self.assertEqual(response.status_code, 400)
43 changes: 40 additions & 3 deletions INSIGHTSAPI/goals/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

from django.core.exceptions import ValidationError
from django.core.mail import get_connection, send_mail
from django.db import transaction
from django.db import connections, transaction
from django.db.models import Max, Q, Subquery
from django.db.utils import IntegrityError
from django.utils import timezone
from openpyxl import load_workbook
from rest_framework import status as framework_status
Expand Down Expand Up @@ -379,6 +380,11 @@ def create(self, request, *args, **kwargs):
# Get the file from the request
file_obj = request.FILES.get("file")
if file_obj:
if not file_obj.name.endswith(".xlsx"):
return Response(
{"message": "Formato de archivo inválido."},
status=framework_status.HTTP_400_BAD_REQUEST,
)
file_name = str(file_obj.name)
year_pattern = r"\b\d{4}\b"
months = [
Expand Down Expand Up @@ -529,7 +535,7 @@ def format_cell_value(cell):
instance.update(accepted=None, accepted_at=None)
Goals.objects.update_or_create(
defaults=default_value,
**{unique_constraint: cedula},
cedula=cedula,
)
except ValidationError as validation_e:
logger.setLevel(logging.ERROR)
Expand All @@ -542,6 +548,38 @@ def format_cell_value(cell):
},
status=framework_status.HTTP_400_BAD_REQUEST,
)
except IntegrityError:
try:
with connections["default"].cursor() as cursor:
logger.setLevel(logging.ERROR)
cursor.execute(
"SELECT COUNT(*) FROM goals_goals WHERE cedula = %s",
[cedula],
)
count = cursor.fetchone()[0]
logger.exception("Error: %s", str(count))
cursor.execute(
f"DELETE FROM goals_goals WHERE cedula = '{cedula}'"
)
cursor.execute(
"SELECT COUNT(*) FROM goals_goals WHERE cedula = %s",
[cedula],
)
count = cursor.fetchone()[0]
logger.exception("Error: %s", str(count))
Goals.objects.update_or_create(
defaults=default_value,
cedula=cedula,
)
except Exception as error:
logger.setLevel(logging.ERROR)
logger.exception("Error: %s", str(error))
return Response(
{
"message": "Excel upload Failed data corruption.",
},
status=framework_status.HTTP_500_INTERNAL_SERVER_ERROR,
)
except Exception as error:
logger.setLevel(logging.ERROR)
logger.exception("Error: %s", str(error))
Expand Down Expand Up @@ -648,7 +686,6 @@ def format_cell_value(cell):
cedula = row[cedula_index].value
name = row[name_index].value

entrega = True
unique_constraint = "cedula"
if (
file_name.upper().find("EJECUCION") != -1
Expand Down
Loading

0 comments on commit 7de9153

Please sign in to comment.