Commit 107578ef authored by Imane Misrar's avatar Imane Misrar
Browse files

Feat(utils/partner) : add is_destination_open parameter in partner.py and add...

Feat(utils/partner) : add is_destination_open parameter in partner.py and add transaction in utils.py

Relates to #148
parent 5ffdc13f
# Generated by Django 2.1.7 on 2020-05-03 09:30
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [("backend_app", "0002_auto_20200417_1430")]
operations = [
migrations.AddField(
model_name="partner",
name="is_destination_open",
field=models.BooleanField(default=False),
)
]
......@@ -21,6 +21,7 @@ class Partner(BaseModel):
city = models.CharField(max_length=40, null=False)
country = models.CharField(max_length=50, null=False)
iso_code = models.CharField(max_length=2, null=False)
is_destination_open = models.BooleanField(null=False, default=False)
# field that will have to be set manually
university = models.ForeignKey(
......
......@@ -2,6 +2,7 @@ import logging
import os
import requests
from django.db import transaction
from backend_app.models.course import Course
from backend_app.models.currency import Currency
......@@ -45,25 +46,27 @@ class FixerData(object):
def update(self):
logger.info("Updating currencies from Fixer API")
response = requests.get(
"http://data.fixer.io/api/latest?access_key={}".format(self.FIXER_API_TOKEN)
)
data = response.json()
with transaction.atomic():
response = requests.get(
"http://data.fixer.io/api/latest?access_key={}".format(self.FIXER_API_TOKEN)
)
if data["success"]:
for (code, rate) in response.json()["rates"].items():
Currency.objects.update_or_create(
code=code, defaults={"one_EUR_in_this_currency": rate}
)
ExternalDataUpdateInfo.objects.create(source="fixer")
logger.info("Currency update was successful")
data = response.json()
else:
logger.error(
"Updating currency information from fixer failed, see response from the API below."
)
logger.error(response.json())
if data["success"]:
for (code, rate) in response.json()["rates"].items():
Currency.objects.update_or_create(
code=code, defaults={"one_EUR_in_this_currency": rate}
)
ExternalDataUpdateInfo.objects.create(source="fixer")
logger.info("Currency update was successful")
else:
logger.error(
"Updating currency information from fixer failed, see response from the API below."
)
logger.error(response.json())
class UtcData(object):
......@@ -75,21 +78,27 @@ class UtcData(object):
def update(self):
logger.info("Updating UTC Data")
self.update_partners()
paged_data_walk(
"{}/openedDestinations".format(self.UTC_API_ENDPOINT),
self.__import_opened_destinations,
)
paged_data_walk(
"{}/exchanges".format(self.UTC_API_ENDPOINT), self.__import_exchange_data
)
paged_data_walk(
"{}/courses".format(self.UTC_API_ENDPOINT), self.__import_course_data
)
ExternalDataUpdateInfo.objects.create(source="utc")
logger.info("Updating invalidated")
self.__update_invalidated()
logger.info("Updating UTC info done !")
with transaction.atomic():
self.update_partners()
# Reset destination open status on all partners
# before fetching open destinations
Partner.objects.all().update(is_destination_open=False)
paged_data_walk(
"{}/openedDestinations".format(self.UTC_API_ENDPOINT),
self.__import_opened_destinations,
)
paged_data_walk(
"{}/exchanges".format(self.UTC_API_ENDPOINT), self.__import_exchange_data
)
paged_data_walk(
"{}/courses".format(self.UTC_API_ENDPOINT), self.__import_course_data
)
ExternalDataUpdateInfo.objects.create(source="utc")
logger.info("Updating invalidated")
self.__update_invalidated()
logger.info("Updating UTC info done !")
update_denormalized_univ_major_minor()
update_denormalized_univ_field()
......@@ -200,13 +209,14 @@ class UtcData(object):
logger.info("Importing the list of opened destinations")
for destination in response_content:
logger.info(destination["idEtab"])
utc_partner_id = destination["idEtab"]
logger.info(utc_partner_id)
semester = destination["semestre"][:1]
year = destination["semestre"][1:]
Offer.objects.update_or_create(
utc_partner_id=destination["idEtab"],
utc_partner_id=utc_partner_id,
year=year,
semester=semester,
defaults=dict(
......@@ -217,6 +227,8 @@ class UtcData(object):
majors=destination["branches"],
),
)
# Don't forget to update the destination open status
Partner.objects.filter(utc_id=utc_partner_id).update(is_destination_open=True)
def __import_course_data(self, response_content):
logger.info("Importing courses data")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment