def add_filter_menu(update: Update, context: CallbackContext, new=False):
    log.info('ДОБАВИТЬ ФИЛЬТР')
    chat_id = update.effective_user['id']
    user, created = User.objects.get_or_create(
        external_id=chat_id,
        defaults={'username': update.effective_user['username']})
    new_filter = Filter(user=user)
    new_filter.save()
    context.user_data['editable_filter'] = new_filter
    edit_filter_menu(update, context, new)
Exemple #2
0
def __to_attribute_filter(parameter):
    if not any(filter.attribute == parameter
               for filter in Repo.filterable_attributes()):
        return Filter.no_filter()  # This includes default "All"
    else:
        return next(filter for filter in Repo.filterable_attributes()
                    if filter.attribute == parameter)
Exemple #3
0
def new_filter():
    """New filter"""
    form = FilterNewForm()
    if request.method == 'POST' and form.validate_on_submit():
        filter = Filter(name=form.name.data,
                        vendor=form.vendor.data,
                        model=form.model.data,
                        descr=form.descr.data,
                        filter_type=form.filter_type.data,
                        diameter_inch=form.diameter_inch.data,
                        is_active=True,
                        is_deleted=False,
                        user_id=current_user.id,
                        create_by=current_user.id,
                        update_by=current_user.id,
                        create_date=datetime.now(),
                        update_date=datetime.now())
        db.session.add(filter)
        db.session.commit()
        flash(gettext('Filter successfully created'), 'form-success')
        return redirect(
            url_for('main_equipment.filter_edit', filter_id=filter.id))
    return render_template('main/equipment/filter_edit.html',
                           form=form,
                           is_new=True)
Exemple #4
0
def index():
    # Build start and end dates
    (start, end) = __build_dates_from_request(request)
    # Gather filters
    filter = request.args.get('group',
                              default=Filter.no_filter(),
                              type=__to_attribute_filter)
    # Build Repo models
    repos = Repo.all_display()
    # Group Repos by filter
    groups = Group.group_repos(repos, filter, start, end)
    # Generate report
    report = Report(groups, filter, start, end)
    # Build filters
    filters = [Filter.no_filter()] + Repo.filterable_attributes()
    # Render from template
    rendered = render_template("report.html", report=report, filters=filters)
    # Build response
    return __build_response_with_dates_cookies(rendered, 200, start, end)
def save_filter(filter, name):
    """
    Function saves filter to DB
    :param filter: filter to be saved.
    :param name: name of filter which appear in DB table
    :return: id of saved filter
    """
    new_filter = Filter(name=name, params=filter)
    db.session.add(new_filter)
    db.session.commit()
    return new_filter.id
def add_cal_to_user(google_id, cal_url, cal_id, filters):
    """
    Adds a user to the database
    :param google_id: The users Google ID
    :param cal_url: The url to ical file the user uses
    :param cal_id: The Google Calendar ID the user uses
    :param filters: The users filters
    :param cred: The users credentials
    :return:
    """
    user = User.query.filter_by(google_id=google_id).first()
    calendar = Calendar(cal_id=cal_id, cal_url=cal_url, owner=user)
    db.session.add(calendar)

    for cal_filter in filters:
        filter_object = Filter(course_code=cal_filter.course_code,
                               description=cal_filter.description,
                               group_name=cal_filter.group_name,
                               owner=calendar)
        db.session.add(filter_object)
    db.session.commit()
Exemple #7
0
            "balance": 100,
            "filters": [
                {"name": "criteria_a", "type": "bool", "goal": "max", "value": 1},
                {"name": "criteria_b", "type": "bool", "goal": "min", "value": 0},
            ]
        },
        {
            "id": 2,
            "name": "person two",
            "wallet": "0x75a36fe489a8a6aac8f3b72af7dee6bfaed3728e",
            "balance": 200,
            "filters": [
                {"name": "criteria_a", "type": "bool", "goal": "max", "value": 1},
                {"name": "criteria_b", "type": "bool", "goal": "min", "value": 0},
            ]
        },
    ]

    ## Seed the database if empty
    if not list(User.query.filter({})):
        for e in example_users:
            filters = []
            for f in d['filters']:
                filters.append(Filter(name=f['name'], type=f['type'], goal=f['goal'], value=f['value']))
            usr = User(name= d["name"], wallet= d["blockchain"], balance= d["balance"], filters=filters)
            usr.save()
    
    with app.app_context():
        init_demo(unlock=True)
        app.run(host='0.0.0.0', use_reloader=False)
Exemple #8
0
def create_filter():
    form = CreateFilterForm()
    if form.validate_on_submit():
        local_filter = Filter.query.filter_by(
            user_id=current_user.id,
            status_id=int(form.status_name.data)).first()

        if not local_filter:
            local_filter = Filter(name=form.name.data,
                                  user_id=current_user.id,
                                  status_id=int(form.status_name.data))
            db.session.add(local_filter)
            db.session.flush()

        # Create tags-----------------------
        raw_tags = form.tags.data.split('$')
        clean_tags = [
            tag.lower() for tag in [tag.strip() for tag in raw_tags]
            if ' ' not in tag and tag
        ]
        tags = []

        for each_clean_tag in clean_tags:
            temp_tag = Tag.query.filter_by(name=each_clean_tag).first()

            if not temp_tag:
                temp_tag = Tag(name=each_clean_tag)
                db.session.add(temp_tag)

            tags.append(temp_tag)

        # Create region-------------------------
        if form.region_name.data:
            local_region = Region.query.filter_by(
                name=form.region_name.data).first()
        else:
            local_region = Region(latitude=form.latitude.data,
                                  longitude=form.longitude.data,
                                  radius=form.radius.data)
            db.session.add(local_region)

        # Create Recurrence Pattern----------------
        recurrence = RecurrencePattern(
            recurrence_type=form.recurrence_type.data
            if form.recurrence_type.data else None,
            start_time=form.start_time.data,
            end_time=form.end_time.data,
            start_date=form.start_date.data,
            end_date=form.end_date.data,
            separation_count=form.separation_count.data
            if form.separation_count.data > 0 else None,
            day_of_week=form.day_of_week.data
            if form.day_of_week.data else None,
            week_of_month=form.week_of_month.data
            if form.week_of_month.data else None,
            day_of_month=form.day_of_month.data
            if form.day_of_month.data else None,
            month_of_year=form.month_of_year.data
            if form.month_of_year.data else None)
        db.session.add(recurrence)
        db.session.flush()

        local_filter.regions.append(local_region)
        local_filter.tags.extend(tags)
        local_filter.recurrences.append(recurrence)

        db.session.commit()
        flash('success$Filter has been created !')
        return redirect(url_for('index'))

    return render_template('new_filter.html', form=form)
Exemple #9
0
 def make_filter(self, data):
     return Filter(**data)
Exemple #10
0
def import_observations(user, import_user, import_history_rec_id, file):
    log_warn = []
    log_error = []

    oal_observations = parse(file, silence=True)

    # Locations
    oal_sites = oal_observations.get_sites()
    found_locations = {}
    add_hoc_locations = {}
    if oal_sites and oal_sites.get_site():
        for oal_site in oal_sites.get_site():
            location = None
            if oal_site.get_name():
                location = Location.query.filter_by(name=oal_site.get_name()).first()
            if location is None:
                add_hoc_locations[oal_site.name] = (oal_site.get_latitude(), oal_site.get_longitude())
                log_warn.append(lazy_gettext('OAL Location "{}" not found').format(oal_site.get_name()))
            else:
                found_locations[oal_site.get_id()] = location

    # Scopes
    found_telescopes = {}
    oal_scopes = oal_observations.get_scopes()
    if oal_scopes:
        for oal_scope in oal_scopes.get_scope():
            model = oal_scope.get_model()
            telescope_type = _get_telescope_type_from_oal_scope_type(oal_scope.get_type())
            vendor = oal_scope.get_vendor()
            aperture_mm = oal_scope.get_aperture()

            focal_length_mm = None
            fixed_magnification = None
            if isinstance(oal_scope, OalscopeType):
                focal_length_mm = oal_scope.get_focalLength()
            if isinstance(oal_scope, OalfixedMagnificationOpticsType):
                fixed_magnification = oal_scope.get_magnification()

            telescope_query = Telescope.query.filter_by(user_id=current_user.id)
            if model:
                telescope_query = telescope_query.filter_by(model=model)
            if telescope_type:
                telescope_query = telescope_query.filter_by(telescope_type=telescope_type)
            if vendor:
                telescope_query = telescope_query.filter_by(vendor=vendor)
            if aperture_mm:
                telescope_query = telescope_query.filter_by(aperture_mm=aperture_mm)
            if focal_length_mm:
                telescope_query = telescope_query.filter_by(focal_length_mm=focal_length_mm)
            if fixed_magnification:
                telescope_query = telescope_query.filter_by(fixed_magnification=fixed_magnification)
            telescope = telescope_query.first()
            if not telescope:
                telescope = Telescope(
                    name=oal_scope.get_id(),
                    vendor=vendor,
                    model=model,
                    descr='',
                    aperture_mm=aperture_mm,
                    focal_length_mm=focal_length_mm,
                    fixed_magnification=fixed_magnification,
                    telescope_type=telescope_type,
                    is_default=False,
                    is_active=True,
                    is_deleted=False,
                    import_history_rec_id=import_history_rec_id,
                    user_id=current_user.id,
                    create_by=current_user.id,
                    update_by=current_user.id,
                    create_date=datetime.now(),
                    update_date=datetime.now()
                )
                db.session.add(telescope)
            found_telescopes[oal_scope.get_id()] = telescope

    # Eyepieces
    found_eyepieces = {}
    oal_eyepieces = oal_observations.get_eyepieces()
    if oal_eyepieces:
        for oal_eyepiece in oal_eyepieces.get_eyepiece():
            model = oal_eyepiece.get_model()
            vendor = oal_eyepiece.get_vendor()
            focal_length_mm = oal_eyepiece.get_focalLength()
            fov_deg = _get_angle_from_oal_angle(oal_eyepiece.get_apparentFOV())

            eyepiece_query = Eyepiece.query.filter_by(user_id=current_user.id)
            if model:
                eyepiece_query = eyepiece_query.filter_by(model=model)
            if vendor:
                eyepiece_query = eyepiece_query.filter_by(vendor=vendor)
            if focal_length_mm:
                eyepiece_query = eyepiece_query.filter_by(focal_length_mm=focal_length_mm)
            if fov_deg:
                eyepiece_query = eyepiece_query.filter_by(fov_deg=fov_deg)
            eyepiece = eyepiece_query.first()
            if not eyepiece:
                eyepiece = Eyepiece(
                    name=oal_eyepiece.get_id(),
                    vendor=vendor,
                    model=model,
                    descr='',
                    focal_length_mm=focal_length_mm,
                    fov_deg=fov_deg,
                    diameter_inch=None,
                    is_active=True,
                    is_deleted=False,
                    import_history_rec_id=import_history_rec_id,
                    user_id=current_user.id,
                    create_by=current_user.id,
                    update_by=current_user.id,
                    create_date=datetime.now(),
                    update_date=datetime.now()
                )
                db.session.add(eyepiece)
            found_eyepieces[oal_eyepiece.get_id()] = eyepiece

    # Filters
    found_filters = {}
    oal_filters = oal_observations.get_filters()
    if oal_filters:
        for oal_filter in oal_filters.get_filter():
            model = oal_filter.get_model()
            vendor = oal_filter.get_vendor()
            filter_type = _get_filter_type_from_oal_filter_kind(oal_filter.get_type())

            filter_query = Filter.query.filter_by(user_id=current_user.id)
            if model:
                filter_query = filter_query.filter_by(model=model)
            if vendor:
                filter_query = filter_query.filter_by(vendor=vendor)
            if filter_type:
                filter_query = filter_query.filter_by(filter_type=filter_type)

            filter = filter_query.first()
            if not filter:
                filter = Filter(
                    name=oal_filter.get_id(),
                    vendor=vendor,
                    model=model,
                    descr='',
                    filter_type=filter_type,
                    diameter_inch=None,
                    is_active=True,
                    is_deleted=False,
                    import_history_rec_id=import_history_rec_id,
                    user_id=current_user.id,
                    create_by=current_user.id,
                    update_by=current_user.id,
                    create_date=datetime.now(),
                    update_date=datetime.now()
                )
                db.session.add(filter)
            found_filters[oal_filter.get_id()] = filter

    # Lenses
    found_lenses = {}
    oal_lenses = oal_observations.get_lenses()
    if oal_lenses:
        for oal_lens in oal_lenses.get_lens():
            model = oal_lens.get_model()
            vendor = oal_lens.get_vendor()
            factor = oal_lens.get_factor()

            lens_query = Lens.query.filter_by(user_id=current_user.id)
            if model:
                lens_query = lens_query.filter_by(model=model)
            if vendor:
                lens_query = lens_query.filter_by(vendor=vendor)
            if factor:
                lens_query = lens_query.filter_by(magnification=factor)

            lens = lens_query.first()
            if not lens:
                lens = Lens(
                    name=oal_lens.get_id(),
                    vendor=vendor,
                    model=model,
                    descr='',
                    lens_type=None,
                    magnification=factor,
                    diameter_inch=None,
                    is_active=True,
                    is_deleted=False,
                    import_history_rec_id=import_history_rec_id,
                    user_id=current_user.id,
                    create_by=current_user.id,
                    update_by=current_user.id,
                    create_date=datetime.now(),
                    update_date=datetime.now()
                )
                db.session.add(lens)
            found_lenses[oal_lens.get_id()] = lens

    # Observations
    oal_sessions = oal_observations.get_sessions()
    found_observing_sessions = {}
    new_observing_sessions = {}
    if oal_sessions and oal_sessions.get_session():
        for oal_session in oal_sessions.get_session():
            begin = oal_session.get_begin()
            end = oal_session.get_end()
            if begin and not end:
                end = begin
            if end and not begin:
                begin = end

            observing_session = ObservingSession.query.filter(ObservingSession.user_id == user.id) \
                .filter(and_(ObservingSession.date_to >= begin, ObservingSession.date_from <= end)) \
                .first()
            if observing_session and observing_session.update_date != observing_session.create_date:
                found_observing_sessions[oal_session.get_id()] = observing_session
            else:
                location = found_locations.get(oal_session.get_site())
                location_position = add_hoc_locations.get(oal_session.get_site())
                if location:
                    title = location.name + ' ' + begin.strftime('%d.%m.%Y')
                elif begin:
                    title = begin.strftime('%d.%m.%Y')
                else:
                    title = oal_session.get_id()

                now = datetime.now()
                if not observing_session:
                    observing_session = ObservingSession(
                        user_id=user.id,
                        title=title,
                        date_from=begin,
                        date_to=end,
                        location_id=location.id if location else None,
                        location_position=location_position,
                        sqm=None,
                        faintest_star=None,
                        seeing=None,
                        transparency=None,
                        rating=None,
                        weather=oal_session.get_weather(),
                        equipment=oal_session.get_equipment(),
                        notes=oal_session.get_comments(),
                        import_history_rec_id=import_history_rec_id,
                        create_by=import_user.id,
                        update_by=import_user.id,
                        create_date=now,
                        update_date=now
                    )
                    new_observing_sessions[oal_session.get_id()] = observing_session
                else:
                    observing_session.title = title
                    observing_session.date_from = begin
                    observing_session.date_to = end
                    observing_session.location_id = location.id if location else None
                    observing_session.location_position = location_position
                    observing_session.weather = oal_session.get_weather()
                    observing_session.equipment = oal_session.get_equipment()
                    observing_session.notes = oal_session.get_comments()
                    observing_session.import_history_rec_id = import_history_rec_id
                    observing_session.update_by = import_user.id
                    observing_session.create_date = now
                    observing_session.update_date = now
                    found_observing_sessions[oal_session.get_id()] = observing_session

                db.session.add(observing_session)

    # Targets
    oal_targets = oal_observations.get_targets()
    found_dsos = {}
    found_double_stars = {}
    not_found_targets = set()
    if oal_targets and oal_targets.get_target():
        for target in oal_targets.get_target():
            if isinstance(target, (OaldeepSkyDS, OaldeepSkyMS)):
                double_star = search_double_star(target.get_name())
                if double_star:
                    found_double_stars[target.get_id()] = double_star
                else:
                    not_found_targets.add(target.get_id())
                    log_error.append(lazy_gettext('Double star "{}" not found').format(target.get_name()))
            else:
                normalized_name = normalize_dso_name_ext(denormalize_dso_name(target.get_name()))
                dso = DeepskyObject.query.filter_by(name=normalized_name).first()
                if dso:
                    found_dsos[target.get_id()] = dso
                else:
                    not_found_targets.add(target.get_id())
                    log_error.append(lazy_gettext('DSO "{}" not found').format(target.get_name()))

    oal_observations = oal_observations.get_observation()

    for oal_observation in oal_observations:
        observing_session = found_observing_sessions.get(oal_observation.get_session())
        is_session_new = False
        if not observing_session:
            observing_session = new_observing_sessions.get(oal_observation.get_session())
            is_session_new = True

        observed_double_star = found_double_stars.get(oal_observation.get_target())
        observed_dso = found_dsos.get(oal_observation.get_target())

        if not observed_dso and not observed_double_star:
            if oal_observation.get_target() not in not_found_targets:
                log_error.append(lazy_gettext('OAL Target "{}" not found.').format(oal_observation.get_target()))
            continue

        observation = None
        if not is_session_new:
            for obs in observing_session.observations:
                if obs.target_type == ObservationTargetType.DBL_STAR:
                    if observed_double_star and observed_double_star.id == obs.double_star_id:
                        observation = obs
                elif obs.target_type == ObservationTargetType.DSO:
                    if observed_dso:
                        for dso in obs.deepsky_objects:
                            if dso.id == observed_dso.id:
                                observation = obs
                                break
                if observation:
                    break

        if observation and observation.create_date != observation.update_date:
            log_warn.append(lazy_gettext('OAL Observation "{}" for session "{}" already exists and was modified by user.').format(oal_observation.get_id(), oal_observation.get_session()))
        else:
            notes = ''
            if oal_observation.get_result():
                notes = oal_observation.get_result()[0].get_description()

            location = found_locations.get(oal_observation.get_site())
            location_position = add_hoc_locations.get(oal_observation.get_site())

            if observing_session:
                if location and observing_session.location_id == location.id:
                    location = None
                if location_position and observing_session.location_position == location_position:
                    location_position = None

            telescope = found_telescopes.get(oal_observation.get_scope()) if oal_observation.get_scope() else None
            eyepiece = found_eyepieces.get(oal_observation.get_eyepiece()) if oal_observation.get_eyepiece() else None
            filter = found_filters.get(oal_observation.get_filter()) if oal_observation.get_filter() else None

            oal_lens = oal_observation.get_lens()
            lens = found_lenses.get(oal_observation.get_lens()) if oal_observation.get_lens() else None

            now = datetime.now()
            if not observation:
                observation = Observation(
                    user_id=current_user.id,
                    observing_session_id=observing_session.id if observing_session else None,
                    location_id=location.id if location else None,
                    location_position=location_position,
                    date_from=oal_observation.get_begin(),
                    date_to=oal_observation.get_end(),
                    sqm=_get_sqm_from_oal_surface_brightness(oal_observation.get_sky_quality()),
                    faintest_star=oal_observation.get_faintestStar(),
                    seeing=_get_seeing_from_oal_seeing(oal_observation.get_seeing()),
                    telescope_id=telescope.id if telescope else None,
                    eyepiece_id=eyepiece.id if eyepiece else None,
                    filter_id=filter.id if filter else None,
                    lens_id=lens.id if lens else None,
                    notes=notes,
                    import_history_rec_id=import_history_rec_id,
                    create_by=current_user.id,
                    update_by=current_user.id,
                    create_date=now,
                    update_date=now,
                )
                if observed_double_star:
                    observation.double_star_id = observed_double_star.id
                    observation.target_type = ObservationTargetType.DBL_STAR
                db.session.add(observation)
                if observed_dso:
                    observation.deepsky_objects.append(observed_dso)
                    observation.target_type = ObservationTargetType.DSO
                if observing_session:
                    observing_session.observations.append(observation)
            else:
                observation.observing_session_id = observing_session.id if observing_session else None
                observation.location_id = location.id if location else None
                observation.location_position = location_position
                observation.date_from = oal_observation.get_begin()
                observation.date_to = oal_observation.get_end()
                observation.sqm = _get_sqm_from_oal_surface_brightness(oal_observation.get_sky_quality())
                observation.faintest_star = oal_observation.get_faintestStar()
                observation.seeing = _get_seeing_from_oal_seeing(oal_observation.get_seeing())
                observation.telescope_id = telescope.id if telescope else None
                observation.eyepiece_id = eyepiece.id if eyepiece else None
                observation.filter_id = filter.id if filter else None
                observation.lens_id = lens.id if lens else None
                observation.notes = notes
                observation.import_history_rec_id = import_history_rec_id
                observation.update_by = current_user.id
                observation.create_date = now  # set create date to update date to easy detect user modifications
                observation.update_date = now
                db.session.add(observation)

            if is_session_new and observing_session:
                if not observing_session.sqm and oal_observation.get_sky_quality():
                    observing_session.sqm = _get_sqm_from_oal_surface_brightness(oal_observation.get_sky_quality())
                if not observing_session.faintest_star and oal_observation.get_faintestStar():
                    observing_session.faintest_star = oal_observation.get_faintestStar()
                if not observing_session.seeing and oal_observation.get_seeing():
                    observing_session.seeing = _get_seeing_from_oal_seeing(oal_observation.get_seeing())

    db.session.commit()

    return log_warn, log_error