Exemplo n.º 1
0
def setup_user_form(form):
    if not form.region_id.choices:
        form.region_id.choices = get_regions_choices(current_user)

    if not form.user_role_id.choices:
        roles = UserRole.query.all()
        form.user_role_id.choices = [(r.id, r.name) for r in roles]
Exemplo n.º 2
0
def prepare_hospital_form(form, current_user):
    if not form.region_id.choices:
        form.region_id.choices = get_regions_choices(current_user,
                                                     with_all_regions=False)

    if not form.hospital_type_id.choices:
        form.hospital_type_id.choices = [(t.id, t.name)
                                         for t in Hospital_Type.query.all()]
Exemplo n.º 3
0
def users():
    if not current_user.is_authenticated:
        return redirect(url_for('login_blueprint.login'))

    if not current_user.user_role.can_access_users and not current_user.user_role.can_export_users:
        return render_template('errors/error-500.html'), 500

    form = UserActivityReportForm()

    regions = get_regions(current_user)

    if not form.region_id.choices:
        form.region_id.choices = [ (-1, c.all_regions) ] + [(r.id, r.name) for r in regions]
   
    change = None
    error_msg = None

    if "added_user" in request.args:
        change =_("Пользователь был успешно добавлен")
    elif "delete_user" in request.args:
        change =_("Пользователь был успешно удален")
    elif "error" in request.args:
        error_msg = request.args["error"]

    users_search_form = UserSearchForm()

    if not users_search_form.region_id.choices:
        users_search_form.region_id.choices = [(-2, _("Неважно"))]
        users_search_form.region_id.choices += get_regions_choices(current_user)

    q_patient = db.session.query(Patient.created_by_id,
                                    func.count('*').label('patient_count'))    
    
    q_patient = q_patient.group_by(Patient.created_by_id).subquery()
    q = db.session.query(User, q_patient.c.patient_count).outerjoin(q_patient, User.id == q_patient.c.created_by_id)

    header_buttons = []
    
    if current_user.user_role.can_access_roles:
        header_buttons.append((_("Управление Ролями"), "users/roles"))

    if current_user.user_role.can_add_edit_user:
        header_buttons.append((_("Добавить Пользователя"), "add_user"))
    
    users_table = UserTableModule(request, q, users_search_form, 
        header_button=header_buttons)

    users_search_form.process()
    form.process()
    return route_template('users/users', users_table = users_table, form=form,
                            users_search_form=users_search_form, constants=c, change=change, error_msg=error_msg)
Exemplo n.º 4
0
def user_profile():
    if not current_user.is_authenticated:
        return redirect(url_for('login_blueprint.login'))

    # if not current_user.user_role.can_add_edit_user:
    # return render_template('errors/error-500.html'), 500

    if "id" in request.args:
        if request.args["id"] != str(current_user.id):
            if not current_user.user_role.can_add_edit_user:
                return render_template('errors/error-500.html'), 500

        try:
            user_query = User.query.filter_by(id=request.args["id"])
            user = user_query.first()
        except exc.SQLAlchemyError:
            return render_template('errors/error-400.html'), 400

        if not user:
            return render_template('errors/error-404.html'), 404
        else:
            form = UpdateUserForm()

            change = None
            error_msg = None

            if not current_user.user_role.can_add_edit_user:
                form_fields = [
                    "full_name", "username", "email", "region_id", "telephone",
                    "organization", "is_admin", "user_role_id"
                ]

                disable_form_fields(form, form_fields)

            if 'update' in request.form:
                values = request.form.to_dict()

                if current_user.user_role.can_add_edit_user:
                    if 'username' in values:
                        new_username = values['username']

                        if not new_username == user.username:
                            if not User.query.filter_by(
                                    username=new_username).count():
                                user.username = new_username
                            else:
                                error_msg = _(
                                    "Пользователь с таким логином уже существует"
                                )

                    if 'region_id' in values:
                        if values['region_id'] == '-1':
                            values['region_id'] = None

                    if 'is_admin' in values:
                        values['is_admin'] = int(values['is_admin']) == 1
                else:
                    # Delete values that we don't update
                    values.pop("is_admin", None)
                    values.pop("username", None)
                    values.pop("region_id", None)
                    values.pop("user_role_id", None)

                if not error_msg:
                    if values.get('password', ''):
                        password = values['password']

                        user.password = hash_pass(password)

                    values.pop("password", None)
                    values.pop("csrf_token", None)
                    values.pop("update", None)

                    user_query.update(values)

                    db.session.add(user)
                    db.session.commit()

                    change = _("Данные обновлены")

            user = user_query.first()
            user_parameters = user.__dict__.copy()

            user_parameters.pop("password", None)

            populate_form(form, user_parameters)
            form.region_id.choices = get_regions_choices(current_user)

            if not form.user_role_id.choices:
                roles = UserRole.query.all()
                form.user_role_id.choices = [(r.id, r.name) for r in roles]

            form.process()

            user_patients_search_form = UserPatientsSearchForm()
            user_patients_search_form.region_id.choices = get_regions_choices(
                current_user)

            patients_table = UserPatientsTableModule(
                request, Patient.query.filter_by(created_by_id=user.id),
                user_patients_search_form)

            return route_template('users/add_user_and_profile',
                                  form=form,
                                  change=change,
                                  user=user,
                                  patients_table=patients_table,
                                  error_msg=error_msg,
                                  is_profile=True)
    else:
        return render_template('errors/error-500.html'), 500
Exemplo n.º 5
0
def train_profile():
    if not current_user.is_authenticated:
        return redirect(url_for('login_blueprint.login'))

    if not current_user.user_role.can_add_train:
        return render_template('errors/error-400.html'), 400

    if "id" in request.args:
        train = None
        try:
            train = Train.query.filter_by(id=request.args["id"]).first()
        except exc.SQLAlchemyError:
            return render_template('errors/error-400.html'), 400

        if not train:
            return render_template('errors/error-404.html'), 404
        else:
            form = TrainForm()

            disable_form_fields(form)

            form.departure_date.default = train.departure_date
            form.arrival_date.default = train.arrival_date

            populate_profile_flight_train_form(form, train)

            change = None
            error_msg = None
            patients = []

            train_type_id = TravelType.query.filter_by(
                value=c.train_type[0]).first().id

            q = db.session.query(Patient, TrainTravel)
            q = q.filter(Patient.travel_type_id == train_type_id)
            q = q.filter(Patient.id == TrainTravel.patient_id)
            q = q.filter(TrainTravel.train_id == train.id)

            patients_search_form = PatientsSearchForm()

            if not patients_search_form.region.choices:
                patients_search_form.region.choices = get_regions_choices(
                    current_user)

            patients_table = PatientsTravelTableModule(request, q,
                                                       patients_search_form,
                                                       True)

            form.process()
            return route_template('flights_trains/flight_train_profile',
                                  form=form,
                                  travel=train,
                                  change=change,
                                  patients_search_form=patients_search_form,
                                  error_msg=error_msg,
                                  patients_table=patients_table,
                                  is_trains=True,
                                  seatmap=[],
                                  patients_seat={})
    else:
        return render_template('errors/error-500.html'), 500
Exemplo n.º 6
0
def flight_profile():
    if not current_user.is_authenticated:
        return redirect(url_for('login_blueprint.login'))

    if not current_user.user_role.can_add_air:
        return render_template('errors/error-400.html'), 400

    if "id" in request.args:
        flight = None
        try:
            flight = FlightCode.query.filter_by(id=request.args["id"]).first()
        except exc.SQLAlchemyError:
            return render_template('errors/error-400.html'), 400

        if not flight:
            return render_template('errors/error-404.html'), 404
        else:
            form = FlightForm()

            disable_form_fields(form)

            form.code.default = flight.code
            form.date.default = flight.date

            populate_profile_flight_train_form(form, flight)

            form.from_country_id.default = flight.from_country_id
            form.from_city.default = flight.from_city

            form.to_country_id.default = flight.to_country_id
            form.to_city.default = flight.to_city

            change = None
            error_msg = None

            flight_type_id = TravelType.query.filter_by(
                value=c.flight_type[0]).first().id

            q = db.session.query(Patient, FlightTravel)
            q = q.filter(Patient.travel_type_id == flight_type_id)
            q = q.filter(Patient.id == FlightTravel.patient_id)
            q = q.filter(FlightTravel.flight_code_id == flight.id)

            patients_search_form = PatientsSearchForm()

            if not patients_search_form.region.choices:
                patients_search_form.region.choices = get_regions_choices(
                    current_user)

            patients_table = PatientsTravelTableModule(request, q,
                                                       patients_search_form)

            seatmap, patients_seat = generate_plane_seatmap(q)

            form.process()
            patients_search_form.process()

            return route_template('flights_trains/flight_train_profile',
                                  form=form,
                                  patients_search_form=patients_search_form,
                                  travel=flight,
                                  change=change,
                                  seatmap=seatmap,
                                  patients_seat=patients_seat,
                                  error_msg=error_msg,
                                  is_trains=False,
                                  patients_table=patients_table)
    else:
        return render_template('errors/error-500.html'), 500
Exemplo n.º 7
0
def hospitals():
    if not current_user.is_authenticated:
        return redirect(url_for('login_blueprint.login'))

    if not current_user.user_role.can_add_edit_hospital:
        return render_template('errors/error-500.html'), 500

    form = HospitalSearchForm(request.form)

    if not form.region.choices:
        form.region.choices = get_regions_choices(current_user)

    filt = dict()

    q = Hospital.query
    region = request.args.get("region", '-1')

    if not current_user.user_role.can_add_edit_hospital:
        filt["region_id"] = current_user.region_id
    else:
        if region != str(-1):
            filt["region_id"] = region
            form.region.default = region
            q = Hospital.query.filter_by(region_id=filt["region_id"])

    hospital_type = Hospital_Type.query.all()

    if not form.hospital_type.choices:
        form.hospital_type.choices = [(-1, c.all_hospital_types)] + [
            (r.id, r.name) for r in hospital_type
        ]

    hospitals = []

    hospital_type = request.args.get("hospital_type", '-1')
    if hospital_type != str(-1):
        filt["hospital_type_id"] = hospital_type
        form.hospital_type.default = hospital_type

    page = 1
    per_page = 10
    if "page" in request.args:
        page = int(request.args["page"])

    q = q.filter_by(**filt)

    total_len = q.count()

    for h in q.offset((page - 1) * per_page).limit(per_page).all():
        patients_num = Patient.query.filter_by(hospital_id=h.id).filter(
            PatientStatus.value == c.in_hospital[0]).count()
        hospitals.append((h, patients_num))

    max_page = math.ceil(total_len / per_page)

    change = None
    error_msg = None

    if "added_hospital" in request.args:
        change = _("Стационар был успешно добавлен")
    elif "delete_hospital" in request.args:
        change = _("Стационар был успешно удален")
    elif "error" in request.args:
        error_msg = request.args["error"]

    form.process()

    return route_template('hospitals/hospitals',
                          hospitals=hospitals,
                          form=form,
                          page=page,
                          max_page=max_page,
                          total=total_len,
                          change=change,
                          error_msg=error_msg)
Exemplo n.º 8
0
def hospital_profile():
    if not current_user.is_authenticated:
        return redirect(url_for('login_blueprint.login'))

    if "id" in request.args:
        try:
            hospital_query = Hospital.query.filter_by(id=request.args["id"])
            hospital = hospital_query.first()
        except exc.SQLAlchemyError:
            return render_template('errors/error-400.html'), 400

        if not hospital:
            return render_template('errors/error-404.html'), 404
        else:
            form = AddHospitalForm()

            change = None
            error_msg = None

            if not current_user.user_role.can_add_edit_hospital:
                form_fields = ["full_name", "region_id", "hospital_type_id"]

                disable_form_fields(form, form_fields)

            if 'update' in request.form and current_user.user_role.can_add_edit_hospital:
                values = request.form.to_dict()
                values.pop("csrf_token", None)
                values.pop("update", None)

                values['name'] = get_hospital_short_name(values['full_name'])

                hospital_query.update(values)

                db.session.add(hospital)
                db.session.commit()

                change = _("Данные обновлены")

            prepare_hospital_form(form, current_user)

            hospital = hospital_query.first()
            hospital_parameters = hospital.__dict__.copy()

            populate_form(form, hospital_parameters)

            form.process()

            search_form = HospitalPatientsSearchForm()
            if not search_form.region_id.choices:
                search_form.region_id.choices = get_regions_choices(
                    current_user, with_all_regions=True)

            patients_table = HospitalPatientsTableModule(
                request, Patient.query, search_form, hospital.id)

            return route_template('hospitals/add_hospital_and_profile',
                                  form=form,
                                  change=change,
                                  hospital=hospital,
                                  error_msg=error_msg,
                                  is_profile=True,
                                  patients_table=patients_table)
    else:
        return render_template('errors/error-500.html'), 500