Skip to content

Commit

Permalink
Merge branch 'qa' into f/db-cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
fanglinfang authored Aug 11, 2023
2 parents d2128c6 + 901a6c1 commit 0261cb5
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 33 deletions.
18 changes: 12 additions & 6 deletions myuw/management/commands/load_buildings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# SPDX-License-Identifier: Apache-2.0

import logging
import traceback
from django.core.mail import send_mail
from django.core.management.base import BaseCommand
from uw_space import Facilities
Expand Down Expand Up @@ -333,15 +334,20 @@ def handle(self, *args, **options):
builds_in_db = CampusBuilding.objects.all()
for bdg in builds_in_db:
try:
fac = space.search_by_number(bdg.number)
if not bdg.no_change(fac):
updated_bdg = CampusBuilding.upd_building(fac)
logger.info("Updated {}".format(updated_bdg))
count += 1
fac_objs = space.search_by_code(bdg.code)
if fac_objs and len(fac_objs):
updated_bdg = CampusBuilding.upd_building(fac_objs[0])
if not bdg.no_change(updated_bdg):
logger.info("Updated {}".format(updated_bdg))
count += 1
except Exception as ex:
msg = {"Load building": bdg, "err": ex}
msg = {"Update building": bdg,
"err": traceback.format_exc(chain=False)}
logger.error(msg)
messages.append("\n{}".format(msg))
logger.info(
"Updated {}/{} buildings".format(
count, len(builds_in_db)))

if len(messages):
send_mail(
Expand Down
63 changes: 43 additions & 20 deletions myuw/models/campus_building.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,43 +29,61 @@ def __eq__(self, other):
def __hash__(self):
return super().__hash__()

@classmethod
def exists(cls, code):
@staticmethod
def exists(code):
return CampusBuilding.objects.filter(code=code).exists()

@classmethod
def exists_by_number(cls, number):
@staticmethod
def exists_by_number(number):
return CampusBuilding.objects.filter(number=number).exists()

@classmethod
def get_building_by_code(cls, code):
@staticmethod
def get_building_by_code(code):
return CampusBuilding.objects.get(code=code)

@classmethod
def get_building_by_number(cls, number):
@staticmethod
def get_building_by_number(number):
return CampusBuilding.objects.get(number=number)

@staticmethod
@transaction.atomic
def upd_building(fac_obj):
if CampusBuilding.exists_by_number(fac_obj.number):
b_entry = CampusBuilding.objects.select_for_update().get(
number=fac_obj.number)
if not b_entry.no_change(fac_obj):
b_entry.code = fac_obj.code
b_entry.latitude = fac_obj.latitude
b_entry.longitude = fac_obj.longitude
b_entry.name = fac_obj.name
b_entry.save()
return b_entry
qset = CampusBuilding.objects.filter(code=fac_obj.code)
if qset:
if len(qset.values()) > 1:
qset.delete()
elif len(qset.values()) == 1:
b_entry = CampusBuilding.objects.select_for_update().get(
code=fac_obj.code)
if not b_entry.no_change(fac_obj):
b_entry.number = fac_obj.number
b_entry.latitude = fac_obj.latitude
b_entry.longitude = fac_obj.longitude
b_entry.name = fac_obj.name
b_entry.save()
return b_entry

qset = CampusBuilding.objects.filter(number=fac_obj.number)
if qset:
if len(qset.values()) > 1:
qset.delete()
elif len(qset.values()) == 1:
b_entry = CampusBuilding.objects.select_for_update().get(
number=fac_obj.number)
if not b_entry.no_change(fac_obj):
b_entry.code = fac_obj.code
b_entry.latitude = fac_obj.latitude
b_entry.longitude = fac_obj.longitude
b_entry.name = fac_obj.name
b_entry.save()
return b_entry

return CampusBuilding.objects.create(
code=fac_obj.code,
number=fac_obj.number,
latitude=fac_obj.latitude,
longitude=fac_obj.longitude,
name=fac_obj.name
)
name=fac_obj.name)

def no_change(self, fac_obj):
return (
Expand All @@ -86,3 +104,8 @@ def json_data(self):

def __str__(self):
return json.dumps(self.json_data(), default=str)

class Meta:
app_label = 'myuw'
# db_table = "myuw_campusbuilding"
# would cause a rename of table
33 changes: 27 additions & 6 deletions myuw/test/management/commands/test_load_buildings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,51 @@

class TestDeleteSessions(TransactionTestCase):

def test_run(self):
def test_init_load(self):
records = CampusBuilding.objects.all()
self.assertEquals(len(records), 0)
call_command('load_buildings', '-l')
records = CampusBuilding.objects.all()
self.assertEquals(len(records), 1)
self.assertEquals(records[0].code, 'MEB')

with patch.object(Facilities, 'search_by_number', spec=True) as mock:
mock.return_value = Facility(
def test_update(self):
obj = CampusBuilding.objects.create(
code='NMEB',
number='1347',
latitude='47.653693',
longitude='',
name='Mechanical Engineering Building',
)
obj.save()
obj = CampusBuilding.objects.create(
code='NMEB',
number='1347',
latitude='',
longitude='-122.304747',
name='Mechanical Engineering Building',
)
obj.save()
records = CampusBuilding.objects.all()
self.assertEquals(len(records), 2)

with patch.object(Facilities, 'search_by_code', spec=True) as mock:
mock.return_value = [Facility(
code='NMEB',
last_updated=datetime.now(),
latitude='47.653693',
longitude='-122.304747',
name='Mechanical Engineering Building',
number='1347',
site='Seattle Main Campus',
type='Building')
status='A',
type='Building')]
call_command('load_buildings')
records = CampusBuilding.objects.all()
self.assertEquals(len(records), 1)
self.assertEquals(records[0].code, 'NMEB')

@patch.object(Facilities, 'search_by_number', spec=True)
@patch.object(Facilities, 'search_by_code', spec=True)
def test_error(self, mock):
fac_obj = Facility(
code='MEB',
Expand All @@ -49,7 +70,7 @@ def test_error(self, mock):
type='Building')
CampusBuilding.upd_building(fac_obj)
mock.side_effect = DataFailureException(
'facility/1347.json', 404, '')
'facility.json?facility_code=MEB', 404, '')
call_command('load_buildings')
records = CampusBuilding.objects.all()
self.assertEquals(len(records), 1)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
'UW-RestClients-PWS~=2.1',
'UW-RestClients-SWS~=2.4',
'UW-RestClients-Sdbmyuw~=1.0',
'UW-RestClients-Space~=1.1',
'UW-RestClients-Space~=1.2',
'UW-RestClients-Trumba~=1.4',
'UW-RestClients-UPass~=2.0',
'UW-RestClients-UWNetID~=1.0',
Expand Down

0 comments on commit 0261cb5

Please sign in to comment.