Ejemplo n.º 1
0
def new_create(request):
    if request.method == 'POST':
        form = ProjectForm(request.user, request.POST)  # new form, exclude current user
        if form.is_valid():
            form.instance.owner = request.user
            form.instance.date_added = datetime.datetime.today()
            form.instance.date_updated = datetime.datetime.today()
            form.save()
            form.instance.users.add(request.user)  # add owner to project.users. after save.
            messages.success(request, 'Project was created')
            return redirect(form.instance)  # redirect to created project
    else:
        form = ProjectForm(request.user)  # exclude current user
    return render(request, 'projects/new.html', 
                 {'form': form, 'button': 'Create project', 'header': 'Create project',  
                  'create_projects_active': True})
Ejemplo n.º 2
0
def edit_update(request, project_id=None):
    project = get_object_or_404(Project, id=project_id)
    if project.owned_by_user(request.user):
        if request.method == 'POST':
            form = ProjectForm(request.user, request.POST, instance=project)  # exclude current user
            if form.is_valid():
                try:
                    form.instance.date_updated = datetime.datetime.today()
                    form.save()
                    form.instance.users.add(request.user)  # add owner to project.users. after save.
                    messages.success(request, 'Project was updated')
                    return redirect(project)
                except:
                    return HttpResponseServerError()
        else:
            form = ProjectForm(request.user, instance=project)
    else:
        return render(request, 'shared/error.html', 
                     {'error_type': 'permission'})
    return render(request, 'projects/new.html', 
                 {'form': form, 'header': 'Edit project', 'button': 'Update project'})