Beispiel #1
0
def edit_group(context, request):
    if 'id' in request.matchdict:
        try:
            group = DBSession().query(Group).get(request.matchdict.get('id'))
            is_new = False
        except SQLAlchemyError:
            raise HTTPNotFound
        if not group:
            raise HTTPNotFound
    else:
        group = Group()
        is_new = True

    schema = GroupSchema(after_bind=maybe_remove_fields).bind(request=request, group=group)
    form = deform.Form(schema, buttons=('zapisz', 'anuluj'), css_class=u'form-horizontal', requirements = ( ('bootstrap', None), ))
    js.deform.auto_need(form)

    if request.POST:
        if not 'zapisz' in request.POST:
            return HTTPFound(location='/groups/only_mine')
        items = request.POST.items()
        try:
            appstruct = form.validate(items)
        except deform.ValidationFailure, e:
            return {'form': e.render(),
                    'group_nav':  get_renderer('templates/group_macros.pt').implementation(),
                    'group': group,
                    'main':  get_renderer('templates/master.pt').implementation()}

        if 'activation_code' in request.POST:
            group.state = u'aktywna'
            request.session.flash({'title': u'Gotowe!',
                                    'body': u'Grupa %s została aktywowana.' % group.name},
                                    queue='success')
            return HTTPFound(location='/groups/%s' % group.id)
        if appstruct['logo']:
            logo = File()
            logo.filename = appstruct['logo']['filename']
            logo.mimetype = appstruct['logo']['mimetype']
            appstruct['logo']['fp'].seek(0)
            logo.data = appstruct['logo']['fp'].read()
            appstruct['logo']['fp'].seek(0,2)
            logo.size = appstruct['logo']['fp'].tell()
            appstruct['logo']['fp'].close()
            appstruct['logo'] = logo

        group = merge_session_with_post(group, appstruct)
        session = DBSession()
        session.add(group)
        if is_new:
            group.add_member(request.user, 'owner')
            request.session.flash({'title': u'Gotowe!',
                                    'body': u'Grupa %s została stworzona.' % group.name},
                                    queue='success')
        else:
            request.session.flash({'title': u'Gotowe!',
                                    'body': u'Grupa %s została zaktualizowana.' % group.name},
                                    queue='success')
        return HTTPFound(location='/groups/only_mine')