Esempio n. 1
0
def add_need(request):
    db = default_storage()

    ###
    import wtforms
#    from doqu.ext.forms import document_form_factory
#    BaseNeedForm = document_form_factory(Need, storage=db)
    class NeedForm(wtforms.Form):
        summary = wtforms.TextField('summary')
        stakeholders = wtforms.FieldList(wtforms.TextField('stakeholders'),
            min_entries=1)

    ###

    form = NeedForm(request.values)
    if request.method == 'POST':
        form = NeedForm(request.values)
        if form.validate():
            obj = Need()
            form.populate_obj(obj)
            obj.save(db)
            return redirect_to('needs.need', pk=obj.pk)
    return {'form': form}
Esempio n. 2
0
def object_detail(request, namespace, model_name, pk=None):
    db = default_storage()
    model = _get_model(namespace, model_name)
    if pk:
        try:
            obj = db.get(model, pk)
        except doqu.validators.ValidationError:
            # Whoops, the data doesn't fit the schema. Let's try converting.
            obj = db.get(Document, pk)
            obj = obj.convert_to(model)
        creating = False
    else:
        obj = model()
        creating = True

    if not creating and request.form.get('DELETE'):
        # TODO: confirmation screen.
        # List related objects, ask what to do with them (cascade/ignore/..)
        obj.delete()
        return redirect_to('tool.ext.admin.views.object_list', namespace=namespace,
                        model_name=model_name)

    DocumentForm = document_form_factory(model, db)

    if not model.meta.structure:
        for k in obj:
            setattr(DocumentForm, k,
                    wtforms.fields.TextField(k.title().replace('_',' ')))

    form = DocumentForm(request.form, obj)

    for name in _get_excluded_names(model):
        del form[name]

    message = None
    if request.method == 'POST' and form.validate():
        form.populate_obj(obj)

        #assert 0==1, 'DEBUG'
        #if not __debug__:

        obj.save(db)    # storage can be omitted if not creating obj

        # TODO: move this to request.session['messages'] or smth like that
        message = u'%s has been saved.' % obj.__class__.__name__
        #if creating:
        # redirect to the same view but change URL
        # from ".../my_model/add/" to
        # to the editing URL ".../my_model/123/"
        # ...hmm, we *should* redirect even after editing an existing item
        return redirect_to('tool.ext.admin.views.object_detail',
                           namespace=namespace, model_name=model_name,
                           pk=obj.pk)
    obj_url = _get_url_for_object(obj)

    # objects of other models that are known to reference this one
    references = {}
    for model, attrs in model.meta.referenced_by.iteritems():
        for attr in attrs:
            qs = model.objects(db).where(**{attr: obj.pk})
            if qs.count():
                references.setdefault(model, {})[attr] = qs

    return {
        'namespace': namespace,
        'object': obj,
        'object_url': obj_url,
        'form': form,
        'message': message,
        'references': references,
        'other_doc_types': env('registered_models'),
    }