mirror of https://github.com/fspc/workstand.git
Drew Larson
8 years ago
7 changed files with 129 additions and 1 deletions
@ -0,0 +1,60 @@ |
|||||
|
import logging |
||||
|
import re |
||||
|
from typing import Dict, Union, Optional |
||||
|
|
||||
|
import requests |
||||
|
from bs4 import BeautifulSoup |
||||
|
from django.core.exceptions import ObjectDoesNotExist |
||||
|
from django.utils import timezone |
||||
|
|
||||
|
from bike.models import Bike |
||||
|
|
||||
|
logger = logging.getLogger('cpic') |
||||
|
|
||||
|
|
||||
|
def _is_stolen(serial: str) -> Optional[bool]: |
||||
|
url = 'http://app.cpic-cipc.ca/English/searchFormResultsbikes.cfm' |
||||
|
data = {'ser': message.get('serial_number'), |
||||
|
'toc': 1, |
||||
|
'Submit': 'Begin Search'} |
||||
|
|
||||
|
r = requests.post(url, data=data) |
||||
|
html = r.text |
||||
|
soup = BeautifulSoup(html) |
||||
|
|
||||
|
no_records = r'^No Records were found in our database on.+$' |
||||
|
found_records = r'^WE HAVE A RECORD ON FILE THAT MATCHES THE IDENTIFIERS THAT YOU PROVIDED.+$' |
||||
|
if soup.body.findAll(text=re.compile(no_records)): |
||||
|
return False |
||||
|
elif soup.body.findAll(text=re.compile(found_records)): |
||||
|
return True |
||||
|
|
||||
|
return None |
||||
|
|
||||
|
|
||||
|
def check_cpic(message: Dict[str, Union[str, int]]) -> None: |
||||
|
""" |
||||
|
Makes a remote call to CPIC to determine whether a bike has been stolen. |
||||
|
""" |
||||
|
try: |
||||
|
bike = Bike.objects.get(id=message['bike_id']) |
||||
|
except ObjectDoesNotExist: |
||||
|
logger.error(f'check_epic: Invalid Bike id: {message["bike_id"]}') |
||||
|
return |
||||
|
|
||||
|
stolen = _is_stolen(message['serial_number']) |
||||
|
|
||||
|
if stolen: |
||||
|
bike.cpic_searched_at = timezone.now() |
||||
|
bike.stolen = True |
||||
|
elif stolen is None: |
||||
|
logger.error(f'check_epic: Unable to check CPIC records with serial number: {message["serial_number"]}.') |
||||
|
return |
||||
|
else: |
||||
|
bike.cpic_searched_at = timezone.now() |
||||
|
bike.stolen = False |
||||
|
|
||||
|
bike.save() |
||||
|
|
||||
|
|
||||
|
|
@ -0,0 +1,7 @@ |
|||||
|
from channels.routing import route |
||||
|
|
||||
|
from .consumers import check_cpic |
||||
|
|
||||
|
channel_routing = [ |
||||
|
route('check-cpic', check_cpic), |
||||
|
] |
@ -0,0 +1,16 @@ |
|||||
|
from channels import Channel |
||||
|
from django.db.models.signals import post_save |
||||
|
from django.dispatch import receiver |
||||
|
|
||||
|
from .models import Bike |
||||
|
|
||||
|
|
||||
|
@receiver(post_save, sender=Bike) |
||||
|
def bike_save_handler(sender, instance, created, **kwargs): |
||||
|
if created: |
||||
|
message = { |
||||
|
'bike_id': instance.id, |
||||
|
'serial_number': instance.serial_number, |
||||
|
} |
||||
|
|
||||
|
Channel('check-cpic').send(message) |
Loading…
Reference in new issue