Пример #1
0
def edit_institution(request, id=None):
    context = {'page_title': u'Instituição', 'edit_name': 'institution', 'has_back': False, 'features':get_user_features(request)}
    t = get_template('edit.html')
    institution = None
    form = InstitutionForm()
    try:
        if request.method == 'POST':
            form = InstitutionForm(request.POST)
            if form.is_valid():
                cd = form.cleaned_data
                institution = _save_institution(cd)
                initial = institution.__dict__
                messages.success(request, 'Instituição salva com sucesso.')
                form = InstitutionForm(initial={})

        elif id:
            institution = Institution.objects.get(id=id)
            initial = institution.__dict__
            form = InstitutionForm(initial=initial)

    except Exception as e:
        log.error(e)
        messages.error(request, u'Ocorreu um erro ao processar a requisição, por favor tente novamente.')
    context = _set_institution_form_context(institution, form, context)
    return render_to_response('edit.html', context, context_instance=RequestContext(request))
Пример #2
0
def view_institutions():
    page = request.args.get('page', 1)
    q = request.args.get('q')
    institutions = restful.GetInstitutions(int(page), q)
    if not institutions.has_key(restful.ITEM_OBJECTS):
        return redirect(url_for('view_institutions'))

    institutionforms = [
        logic.GetInstitutionFormById(x[restful.ITEM_ID])
        for x in institutions[restful.ITEM_OBJECTS]
    ]
    while None in institutionforms:
        institutionforms.remove(None)


#    form.message = form.data
    if request.method == 'POST':
        form = InstitutionForm(request.form)
        if request.form.has_key('delete'):
            for x in orm.Institutionimage.query.filter_by(
                    institution_id=int(form.id.data)).all():
                pathfile_server = os.path.join(UPLOAD_PATH, x.file)
                if os.path.exists(pathfile_server):
                    os.remove(pathfile_server)
            orm.db.session.delete(orm.Institution.query.get(int(form.id.data)))
            orm.db.session.commit()
            return redirect(url_for('view_institutions', page=page, q=q))

    form = PageInfo()
    logic.LoadBasePageInfo('所有培训机构', '查看', form)

    return render_template('view_institutions.html',
                           forms=institutionforms,
                           form=form,
                           paging=restful.GetPagingFromResult(institutions))
Пример #3
0
def GetInstitutionFormById(institution_id):
    institution = orm.Institution.query.get(int(institution_id))
    if institution is None:
        return None
    institutionform = InstitutionForm()
    institutionform.id.data = institution.id
    institutionform.name.data = institution.name
    institutionform.agespan_id.data = institution.agespan_id
    institutionform.agespan_name = institution.agespan.name
    institutionform.area_id.data = institution.area_id
    institutionform.area_name = institution.area.name
    institutionform.address.data = institution.address
    institutionform.location.data = institution.location
    institutionform.website.data = institution.website
    institutionform.telephone.data = institution.telephone
    institutionform.feedesc.data = institution.feedesc
    institutionform.timeopen.data = institution.timeopen
    institutionform.timeclose.data = institution.timeclose
    institutionform.feetype_id.data = institution.feetype_id
    institutionform.feetype_name = institution.feetype.name
    institutionform.longitude.data = institution.longitude
    institutionform.latitude.data = institution.latitude
    institutionform.institutionimages = institution.institutionimages
    institutionform.feature_ids.data = [
        x.feature_id for x in institution.institutionfeatures
    ]

    institutionform.area_id.choices = g_choices_area
    institutionform.feature_ids.choices = g_choices_feature
    institutionform.agespan_id.choices = g_choices_agespan
    institutionform.feetype_id.choices = g_choices_feetype

    return institutionform
Пример #4
0
def view_institution():
    institution_id = request.args.get('id')
    q = request.args.get('q')
    if q is not None:
        return redirect(url_for('view_institutions', page=1, q=q))
    form = InstitutionForm(request.form)
    form.area_id.choices = logic.g_choices_area
    form.feature_ids.choices = logic.g_choices_feature
    form.agespan_id.choices = logic.g_choices_agespan
    form.feetype_id.choices = logic.g_choices_feetype
    form.timeopen.data = datetime.time(8, 30)
    form.timeclose.data = datetime.time(22, 00)
    #    form.message = form.data
    if request.method == 'POST' and form.validate():
        if form.id.data:
            institution = orm.Institution.query.get(int(form.id.data))
            institution.name = form.name.data
            institution.agespan_id = form.agespan_id.data
            institution.area_id = form.area_id.data
            institution.address = form.address.data
            institution.location = form.location.data
            institution.website = form.website.data
            institution.telephone = form.telephone.data
            institution.feedesc = form.feedesc.data
            institution.timeopen = form.timeopen.data
            institution.timeclose = form.timeclose.data
            institution.feetype_id = form.feetype_id.data
            institution.longitude = form.longitude.data
            institution.latitude = form.latitude.data
            orm.db.session.commit()
        else:
            institution = orm.Institution(
                form.name.data, form.agespan_id.data, form.area_id.data,
                form.address.data, form.location.data, form.website.data,
                form.telephone.data, form.feedesc.data, form.timeopen.data,
                form.timeclose.data, form.feetype_id.data, form.longitude.data,
                form.latitude.data, None)
            orm.db.session.add(institution)
            orm.db.session.commit()
            form.id.data = institution.id

        logic.SetInstitutionFeatures(int(form.id.data), form.feature_ids.data)

        if request.form.has_key('upload'):
            file = request.files['image']
            if file:
                file_server = str(uuid.uuid1()) + Util.file_extension(
                    file.filename)
                pathfile_server = os.path.join(UPLOAD_PATH, file_server)
                file.save(pathfile_server)
                if os.stat(pathfile_server).st_size < 1 * 1024 * 1024:
                    institutionimage = orm.Institutionimage(
                        institution.id, file_server)
                    orm.db.session.merge(institutionimage)
                    orm.db.session.commit()
                else:
                    os.remove(pathfile_server)
        else:
            return redirect(url_for('view_institution'))
    elif request.method == 'GET' and institution_id:
        form = logic.GetInstitutionFormById(institution_id)
        logic.LoadBasePageInfo('修改培训机构', '输入并确定', form)
    else:
        logic.LoadBasePageInfo('新建培训机构', '输入并确定', form)

    if form.id.data:
        institution = orm.Institution.query.get(int(form.id.data))
        form.institution = institution
        if form.institution:
            form.institutionimages = form.institution.institutionimages

    return render_template('view_institution.html', form=form)