Exemplo n.º 1
0
    def __get_device_data(self, start_time, end_time):
        client = bigquery.Client()

        query = """
                 SELECT channel_id, created_at, pm2_5, pm10 , s2_pm2_5,
                  s2_pm10, temperature , humidity, voltage, altitude, latitude, longitude, no_sats, hdope, wind 
                  FROM airqo-250220.thingspeak.clean_feeds_pms where ({0}) 
                  AND created_at BETWEEN '{1}' AND '{2}'
                    """.format(build_channel_id_filter(self.devices),
                               str_to_date(start_time), str_to_date(end_time))

        dataframe = (client.query(query).result().to_dataframe())

        return dataframe
Exemplo n.º 2
0
  def testStrToDate(self):  # - - - - - - - - - - - - - - - - - - - - - - - - -
    """Test 'str_to_date' string encoding"""

    for d in self.dates:

      format_str = self.date_parse_formats[d[3]]

      d1 = date.str_to_date(d[0], format_str, self.pivot_year)

      assert d1 == d[1], 'Wrongly parsed date "'+d[0]+'" into: '+ str(d1)
Exemplo n.º 3
0
    def testStrToDate(
            self):  # - - - - - - - - - - - - - - - - - - - - - - - - -
        """Test 'str_to_date' string encoding"""

        for d in self.dates:

            format_str = self.date_parse_formats[d[3]]

            d1 = date.str_to_date(d[0], format_str, self.pivot_year)

            assert d1 == d[
                1], 'Wrongly parsed date "' + d[0] + '" into: ' + str(d1)
Exemplo n.º 4
0
def create_box(id):
    # if  id == 0 create new box, otherwise update box by id
    # authenticate admin:
    if not is_auth_box(current_user):
        return unathorized("You are not authorized to create Idea Box.",
                           "error")

    # authenticate company
    if id > 0 and not authenticate_company(id, current_user):
        return unathorized("You are not authorized to update Idea Box.",
                           "error")

    current_box = Boxes.query.get(id)
    form = CreateBoxForm()

    if form.validate_on_submit():
        name = form.name.data
        description = form.description.data
        close_at = form.close_at.data
        if id == 0:
            # add new Idea Box to the Boxes table:
            new_box = Boxes(name=name,
                            description=description,
                            close_at=close_at,
                            admin_id=get_admin_id(current_user))

            db.session.add(new_box)
            error = "Any error occured when created new Idea Box. Please try again."
            success = "New Idea Box successfully created."
        else:
            # edit box by id:
            success = ""
            if name != current_box.name:
                current_box.name = name
                success += "Title updated.\n"
            if description != current_box.description:
                current_box.description = description
                success += "Description updated.\n"
            # close_at is a date object, have to convert to string
            str_close_at = close_at.strftime("%Y-%m-%d")
            if str_close_at != current_box.close_at:
                current_box.close_at = close_at
                success += "Closing date updated.\n"

            error = "Any error occured when updated Idea Box. Please try again."

        try:
            db.session.commit()
            flash(success, "inform")
        except:
            db.session.rollback()
            flash(error, "error")
            return redirect(url_for("create_box", id=id))

        return redirect(url_for("main"))

    if id > 0:
        # edit mode:
        form.submit.label.text = "Edit Box"
        form.name.data = current_box.name
        form.description.data = current_box.description
        form.close_at.data = str_to_date(current_box.close_at)

    return render_template(
        "create_box.html",
        form=form,
        id=id,
        logo=get_logo(current_user),
        change_logo=is_auth_company(
            current_user
        ),  # to add click event to change logo for authorized admin
        nav=get_nav(current_user))
Exemplo n.º 5
0
def create_sample_company():
    # instatiate Company:
    company = Company(name="Eric BLABLA KGB")
    company.set_founder_password("aaa")
    company.set_joining_password("bbb")

    # update database and query the ID of the new company:
    try:
        db.session.add(company)
        db.session.commit()
    except:
        db.session.rollback()
        flash(
            "Any error occured when created the sample company registration. Please try again.",
            "error")
        return redirect(url_for("register_company"))

    registered_company = Company.query.filter_by(
        name="Eric BLABLA KGB").first()

    # instatiate Jhon Do:
    colleague = Colleagues(user_name="jhon_do",
                           email="*****@*****.**",
                           first_name="Jhon",
                           last_name="Do",
                           position="Founder",
                           confirmed=1)

    colleague.set_password("aaa")

    data = {
        "company_id": registered_company.id,
        "colleague": colleague,
        "sample_avatar": "john_do.jpg"
    }

    create_sample_colleague(data)

    # set the founder as Admin with full privilegs:
    registered_colleague = Colleagues.query.filter_by(
        email="*****@*****.**").first()
    # instatiate Admins:
    admin = instatiate_admin(True)
    admin.colleague_id = registered_colleague.id
    try:
        db.session.add(admin)
        db.session.commit()
    except:
        db.session.rollback()
        flash(
            "Any error occured when created sample admin registration. Please try again.",
            "error")
        return redirect(url_for("register_company"))

    # copy logo:
    location = "static/sample_logo/blabla.png"
    destination = f"static/logo/{registered_colleague.company_id}.png"
    shutil.copy2(location, destination)

    # update database:
    company.logo = "png"
    try:
        db.session.commit()
        print("Company logo copied.")
    except:
        db.session.rollback()
        print("An error occured when copied logo.")

    # instatiate Jane Do:
    colleague = Colleagues(user_name="jane_do",
                           email="*****@*****.**",
                           first_name="Jane",
                           last_name="Do",
                           position="Co-Founder",
                           confirmed=1)
    colleague.set_password("aaa")
    data = {
        "company_id": registered_company.id,
        "colleague": colleague,
        "sample_avatar": "jane_do.png"
    }

    create_sample_colleague(data)

    # instatiate Do Do:
    colleague = Colleagues(user_name="dodo",
                           email="*****@*****.**",
                           first_name="Do",
                           last_name="Do",
                           position="dodo",
                           confirmed=1)
    colleague.set_password("aaa")
    data = {
        "company_id": registered_company.id,
        "colleague": colleague,
        "sample_avatar": "dodo.svg"
    }

    create_sample_colleague(data)

    # instatiate x more colleagues:
    x_more = 20

    usernames = open("fake_dataset/username.txt").readlines()
    emails = open("fake_dataset/fake_email.txt").readlines()
    first_names = open("fake_dataset/first_name.txt").readlines()
    last_names = open("fake_dataset/last_name.txt").readlines()
    positions = open("fake_dataset/position.txt").readlines()

    for x in range(x_more):

        colleague = Colleagues(
            user_name=get_random_item(usernames).strip(),
            email=get_random_item(emails),
            first_name=get_random_item(first_names),
            last_name=get_random_item(last_names).lower().title(),
            position=get_random_item(positions),
            confirmed=1)
        colleague.set_password("aaa")
        data = {
            "company_id": registered_company.id,
            "colleague": colleague,
            "sample_avatar": None
        }

        create_sample_colleague(data)

    # create sample Idea Box:
    admin = Admins.query.filter(
        Admins.colleague_id == registered_colleague.id).first()
    for x in range(2):
        new_box = Boxes(name=lorem.sentence().replace(".", ""),
                        description=lorem.paragraph(),
                        close_at=str_to_date(
                            add_day(str_to_date(today()),
                                    x).strftime('%Y-%m-%d')),
                        admin_id=admin.id)

        try:
            print("Trying to add new Idea Box to the database...")
            db.session.add(new_box)
            db.session.commit()
        except SQLAlchemyError as e:
            error = str(e.__dict__['orig'])
            print("**************************************")
            print(error)
            print("New Idea Box not created!")
            print("new_box.name: ", new_box.name)
            print("new_box.description: ", new_box.description)
            print("new_box.close_at: ", new_box.close_at)
            print("new_box.admin_id: ", new_box.admin_id)
            db.session.rollback()

    # create sample Idea:
    colleagues = Colleagues.query.filter(
        Colleagues.company_id == registered_company.id).all()
    boxes = db.session.query(
        Boxes, Admins, Colleagues).filter(Boxes.admin_id == admin.id).all()
    for x in range(7):
        colleague = get_random_item(colleagues)
        sign = [
            "incognito", colleague.user_name, colleague.first_name,
            colleague.fullname()
        ]
        idea = Ideas(idea=lorem.paragraph(),
                     sign=get_random_item(sign),
                     box_id=get_random_item(boxes).Boxes.id,
                     colleague_id=colleague.id)
        db.session.add(idea)

    try:
        db.session.commit()
    except:
        db.session.rollback()

    print("The sample company registered successfully!")