예제 #1
0
파일: new.py 프로젝트: bytearchive/todo
 def done(self, request, form_list):
     clean = {}
     for form in form_list:
         if form.is_valid():
             clean.update(form.cleaned_data)
     parent = clean.pop('parent_tracker')
     if parent is None and clean['parent_summary']:
         # user wants to create a new generic tracker which will be the
         # parent
         parent = Tracker(summary=clean.pop('parent_summary'),
                          alias=clean.pop('parent_alias'))
         parent.save()
         parent.activate(request.user)
     clean['parent'] = parent
     # If the parent exists the desired outcome is for created todo objects'
     # aliases to be appended to the parent's alias.  Suffix (set on child
     # trackers) works exactly this way. If there is no parent, `suffix`
     # will behave like `alias` (cf. `Task.__init__` and `Tracker.__init__`)
     clean['suffix'] = clean.pop('alias', None)
     tracker_proto = clean.pop('tracker_proto')
     task_proto = clean.pop('task_proto')
     prototype = tracker_proto or task_proto
     # finally, make sure the projects passed to the prototype are instances 
     # of `todo.models.Project`
     clean['projects'] = [getattr(p, 'todo', None) or p 
                          for p in clean['projects']]
     if prototype.clone_per_locale:
         # spawn_per_locale is a generator
         spawned = list(prototype.spawn_per_locale(request.user, **clean))
     else:
         spawned = [prototype.spawn(request.user, **clean)]
     redirect_url = self.get_redirect_url(spawned,
                                          prototype == tracker_proto,
                                          parent)
     return HttpResponseRedirect(redirect_url)
예제 #2
0
파일: __init__.py 프로젝트: Pike/todo
def create(request, obj):
    if obj == 'tasks':
        form_class = AddTasksForm
    else:
        form_class = AddTrackersForm

    if request.method == 'POST':
        form = form_class(request.POST)
        parent_form = ChooseParentForm(request.POST)
        if form.is_valid() and parent_form.is_valid():
            fields = form.cleaned_data
            parent_clean = parent_form.cleaned_data
            parent = parent_clean['tracker']
            if (parent is None and
                parent_clean['parent_summary'] and
                parent_clean['parent_project']):
                # user wants to create a new tracker which will be the parent
                parent = Tracker(summary=parent_clean['parent_summary'],
                                 project=parent_clean['parent_project'],
                                 locale=parent_clean['parent_locale'],
                                 suffix=parent_clean['parent_suffix'])
                parent.save()
                # send the 'created' signal
                status_changed.send(sender=parent, user=request.user,
                                    action=parent.status)
                parent.activate(request.user)
            if parent is not None:
                # For consistency's sake, if a parent is specified, try to
                # use its values for `project` and `locale` to create the
                # task, ignoring values provided by the user in the form.
                fields['project'] = parent.project
                if parent.locale:
                    fields['locales'] = [parent.locale]
            fields['parent'] = parent
            prototype = form.cleaned_data.pop('prototype')
            if prototype.clone_per_locale is True:
                for todo in prototype.spawn_per_locale(request.user, **fields):
                    todo.activate(request.user)
            else:
                todo = prototype.spawn(request.user, **fields) 
                todo.activate(request.user)
            return HttpResponseRedirect(reverse('todo.views.demo.%s' % obj[:-1],
                                                args=[todo.pk]))
    else:
        form = form_class()
        parent_form = ChooseParentForm()
    return render_to_response('todo/new_%s.html' % obj[:-1],
                              {'form': form,
                               'parent_form': parent_form,})