예제 #1
0
    def _logout(self):
        """ Logout from the server and close the session. """

        url = 'http://bamboo-mec.de/index.php5'
        payload = {'logout': '1'}

        response = self.remote_session.get(url, params=payload)

        if response.history[0].status_code == 302:
            # We have been redirected to the home page
            notify('Logged out. Goodbye!')

        self.remote_session.close()
예제 #2
0
    def package(self, serial_items: list) -> Tables:
        """
        In goes serial data (raw strings) as returned by the Scraper. Out comes
        unserialized & packaged ORM table row objects digestable by the database.

        :param serial_items: a list of Stamped(Stamp, serial_data) objects
        :return: a Tables(clients, orders, checkpoints, checkins) object
        """

        assert serial_items is not None, 'Argument cannot be None.'

        clients = list()
        orders = list()
        checkpoints = list()
        checkins = list()

        for serial_item in serial_items:

            # Unpack the data
            day = serial_item[0][0]
            uuid = serial_item[0][1]
            job_details = serial_item[1][0]
            addresses = serial_item[1][1]

            client = Client(**{'client_id': self._unserialise(int, job_details['client_id']),
                               'name': self._unserialise(str, job_details['client_name'])})

            order = Order(**{'order_id': self._unserialise(int, job_details['order_id']),
                             'client_id': self._unserialise(int, job_details['client_id']),
                             'uuid': int(uuid),
                             'date': day,
                             'distance': self._unserialise_float(job_details['km']),
                             'cash': self._unserialise(bool, job_details['cash']),
                             'city_tour': self._unserialise_float(job_details['city_tour']),
                             'extra_stops': self._unserialise_float(job_details['extra_stops']),
                             'overnight': self._unserialise_float(job_details['overnight']),
                             'fax_confirm': self._unserialise_float(job_details['fax_confirm']),
                             'waiting_time': self._unserialise_float(job_details['waiting_time']),
                             'type': self._unserialise_type(job_details['type'])})

            clients.append(client)
            orders.append(order)

            for address in addresses:
                geocoded = self.geocode(address)

                checkpoint = Checkpoint(**{'checkpoint_id': geocoded['osm_id'],
                                           'display_name': geocoded['display_name'],
                                           'lat': geocoded['lat'],
                                           'lon': geocoded['lat'],
                                           'street': self._unserialise(str, address['address']),
                                           'city': self._unserialise(str, address['city']),
                                           'postal_code': self._unserialise(int, address['postal_code']),
                                           'company': self._unserialise(str, address['company'])})

                checkin = Checkin(**{'checkin_id': self._hash_timestamp(day, address['timestamp']),
                                     'checkpoint_id': geocoded['osm_id'],
                                     'order_id': self._unserialise(int, job_details['order_id']),
                                     'timestamp': self._unserialise_timestamp(day, address['timestamp']),
                                     'purpose': self._unserialise_purpose(address['purpose']),
                                     'after_': self._unserialise_timestamp(day, address['after']),
                                     'until': self._unserialise_timestamp(day, address['until'])})

                checkpoints.append(checkpoint)
                checkins.append(checkin)

            notify('Packaged {}-uuid-{}.', str(day), uuid)

        # The order matters when we commit to the database
        # because foreign keys must be refer to existing
        # rows in related tables, c.f. the model module.
        tables = Tables(clients, orders, checkpoints, checkins)

        return tables