Esempio n. 1
0
    def test_return_attribute_of_subtheme(self):
        """
        Adding a theme and subtheme to the database and then testing to see if it's data is
        retrieved correctly
        """

        theme = Theme("Test_Theme")
        theme.save()
        theme.commit()

        sub_theme = SubTheme(theme.id, "Test_Sub_Theme")
        sub_theme.save()
        sub_theme.commit()

        attributes = Attributes("1234567890-123456789-123456789",
                                "_test_attribute_", "_table_name_",
                                sub_theme.id, 1)
        attributes.save()
        attributes.commit()

        response = self.testing_client.get('/data',
                                           data=dict(subtheme=theme.id))

        self.assertEqual(theme.json(), response.get_json())

        attributes.delete()
        attributes.commit()

        sub_theme.delete()
        sub_theme.commit()

        theme.delete()
        theme.commit()
 def create_theme(self, name: str) -> db.Model:
     """
     Create Theme
     :param name: Name of theme
     :return: Theme
     """
     theme = Theme(name=name)
     theme.save()
     return theme
Esempio n. 3
0
def test_submit():
    u = Unit('kg', 'Kilogram')
    u2 = Unit('g', 'Grams')
    u3 = Unit('km', 'KiloMeter')
    u.save()
    u2.save()
    u3.save()

    t = Theme('Environment')
    t2 = Theme('Transport')
    t.save()
    t2.save()

    st = SubTheme(t.id, 'Airquality')
    st2 = SubTheme(t2.id, 'Traffic')
    st.save()
    st2.save()

    db.session.commit()
 def create_dummy_theme(self) -> Theme:
     """
     Create a Theme
     :return: a Theme
     """
     theme = Theme.get_by_name("_test_add_theme_")
     if not theme:
         theme = Theme("_test_add_theme_")
         theme.save()
         theme.commit()
         return theme
     return theme
Esempio n. 5
0
 def create_dummy_theme(self) -> Theme:
     """
     Create a Theme
     :return: a Theme instance
     """
     theme = Theme.get_by_name("_test_add_theme_")
     if not theme:
         theme = Theme("_test_add_theme_")
         theme.save()
         theme.commit()
         self.dummy_ids.append(theme.id)
         return theme
     return theme
def create_theme(name: str = 'Environment') -> Theme:
    """
    Create a Theme
    :param name: Themes name
    :raises ValueError: If the new Theme is not persisted to the dB
    :return: A new Theme
    """
    theme = Theme.get_by_name(name)
    if not theme:
        theme = Theme(name)
        theme.save()
        theme.commit()
    if not theme:
        logger.critical('ValueError raised while creating Theme')
        raise ValueError
    return theme
Esempio n. 7
0
    def create_theme(self) -> None:
        """
        Create a Theme
        """
        self.theme = Theme.get_by_name("_test_theme_")
        if self.theme:
            return

        self.theme = Theme("_test_theme_")
        self.theme.save()
        try:
            self.theme.commit()

        except Exception as exp:
            logger.error(exp)
            self.tearDown()

        if not self.theme:
            self.fail()
    def post(self) -> ({str: str}, HTTPStatus):
        """
        Creates a new theme
        :post_argument  name: the name of the new theme
        :post_type  name: str
        :returns: A JSON with a message, theme id, and new themes name with a http status of 200 (OK) otherwise,
                  A JSON with an appropriate error message and http status applicable to the error
        """
        if not get_jwt_claims()['admin']:
            return {
                "error": "administration privileges required"
            }, HTTPStatus.FORBIDDEN

        # Get arguments
        args = self.reqparser.parse_args()

        # Check the theme name is not empty, abort if it is empty
        if not args["name"]:
            return {
                'error': 'Theme cannot be empty',
                'name': "''"
            }, HTTPStatus.BAD_REQUEST

        # Check theme does not exist (avoid duplicates)
        if Theme.get_by_name(args["name"]):
            return {
                'error': 'Theme already exists.',
                'id': " ",
                'name': args["name"]
            }, HTTPStatus.BAD_REQUEST

        # Create the new theme
        theme = Theme(args["name"])
        theme.save()
        theme.commit()

        return {
            "message": "New theme created",
            "id": theme.id,
            "name": theme.name
        }, HTTPStatus.OK
Esempio n. 9
0
    def test_return_subthemes_of_theme(self):
        """
        Adding a theme and subtheme (linked to to that theme) to the database and then
        testing to see if it is retrieved correctly
        """
        theme = Theme(name='Test_Theme')
        theme.save()
        theme.commit()

        sub_theme = SubTheme(theme.id, "Test_Sub_Theme")
        sub_theme.save()
        sub_theme.commit()

        response = self.testing_client.get('/data', data=dict(theme=theme.id))

        self.assertEqual(sub_theme.json(), response.get_json())

        sub_theme.delete()
        sub_theme.commit()

        theme.delete()
        theme.commit()
Esempio n. 10
0
    def post(self) -> (dict, HTTPStatus):
        """
        Get dummy data from CSV. Store dummy data in database
        :param file_name: File name to extract data from.
        :return: A Status Report detailing the dB Entries created and an HTTP
        Status code 200 on success otherwise, a JSON error message is returned
        with the appropriate HTTPStatus code
        """
        args = self.reqparser.parse_args()
        # Get size of tables before import
        self.loc_stats["before"] = db.session.query(func.count(
            LocationData.id)).scalar()
        self.tracker_stats["before"] = db.session.query(func.count(
            Tracker.id)).scalar()

        # Fetch Data from CSV
        try:
            df = pd.read_csv(args['file_name'])
        except IOError as ioe:
            logger.error("Unable to parse CSV data to dataframe",
                         ioe.with_traceback(ioe.__traceback__))
            return dict(error="Unable to parse CSV data to dataframe",
                        trackback=ioe.with_traceback(
                            ioe.__traceback__)), HTTPStatus.BAD_REQUEST

        moving_theme = Theme.get_by_name("Moving_Sensors")
        if moving_theme:
            moving_sensor_theme_id = moving_theme.id
        else:
            moving_sensor_theme = Theme("Moving_Sensors")
            moving_sensor_theme.save()
            moving_sensor_theme.commit()
            moving_sensor_theme_id = moving_sensor_theme.id

        moving_subtheme = SubTheme.get_by_name("Moving_Airquality")
        if moving_subtheme:
            moving_sensor_subtheme_id = moving_subtheme.id
        else:
            moving_sensor_subtheme = SubTheme(moving_sensor_theme_id,
                                              "Moving_Airquality")
            moving_sensor_subtheme.save()
            moving_sensor_subtheme.commit()
            moving_sensor_subtheme_id = moving_sensor_subtheme.id

        # Trackers must be unique, Fetch trackers and make dB entries for
        # each unique tracker
        unique_tracker_ids = df["tracker"].unique()

        for tracker_id in unique_tracker_ids:
            self.t_ids[self.create_trackers(str(tracker_id),
                                            moving_sensor_subtheme_id)] = 0

        # Define Location data Pandas DataFrame Column names
        loc_df = df[[
            'tracker', 'datetime', 'latitude', 'longitude', 'speed', 'heading',
            'elevation', 'charger', 'battery', 'signalquality', 'satcnt'
        ]]

        # Drop all entries that are incomplete have NaN or None/ Null values
        loc_df = loc_df.dropna()

        # Store Location Data in the dB
        for index, row in loc_df.iterrows():
            self.add_location_data(row['tracker'], row['datetime'],
                                   row['latitude'], row['longitude'],
                                   row['speed'], row['heading'],
                                   row['elevation'], row['charger'],
                                   row['battery'], row['signalquality'],
                                   row['satcnt'])

        self.loc_stats["after"] = db.session.query(func.count(
            LocationData.id)).scalar()
        self.tracker_stats["after"] = db.session.query(func.count(
            Tracker.id)).scalar()

        return self.status_report(), 200
Esempio n. 11
0
from pymongo import MongoClient
from urlparse import urlparse
from models.theme import Theme

logging.basicConfig()

config = local_settings.env
app = Flask(config.get('APPLICATION_NAME', 'email_render'))
theme_logger = logging.getLogger('theme')
ql = logger(logger=theme_logger, level=logging.INFO)
uri = "localhost"
client = MongoClient(uri)
db = client['affiliate_website']
collection = db['themes']

theme = Theme(ql, collection)

#--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
# ERROR HANDLING


@app.errorhandler(500)
def internal_500_error(exception):
    app.logger.exception(exception)
    return pprint.pformat(exception)


@app.errorhandler(404)
def internal_404_error(exception):
    app.logger.exception(exception)
    return 'awe<br/>\n%s<br/>\n%s' % (exception, request.url), 404
Esempio n. 12
0
 def create_theme(self, name):
     theme = Theme(name=name)
     theme.save()
     return theme