Exemplo n.º 1
0
 def post(self, request, **kwargs):
     """Handles POST requests on article pages"""
     # pylint:disable=unused-argument
     article = self.get_object()
     authorized = self.request.user.is_staff
     action = request.POST.get("action")
     clear_cache = False
     if not authorized:
         return HttpResponseForbidden()
     if action == "projects":
         form = ProjectManagerForm(request.POST, user=request.user)
         if form.is_valid():
             projects = form.cleaned_data["projects"]
             article.projects.set(projects)
             clear_cache = True
     tags = request.POST.getlist("tags")
     if tags:
         tag_set = set()
         for tag in tags:
             new_tag, _ = Tag.objects.get_or_create(name=normalize(tag))
             tag_set.add(new_tag)
         article.tags.set(*tag_set)
         clear_cache = True
     if clear_cache:
         article.clear_cache()
     return redirect(article)
Exemplo n.º 2
0
 def test_set_projects(self):
     """Posting a group of projects to an article should set that article's projects."""
     project1 = ProjectFactory()
     project2 = ProjectFactory()
     staff = UserFactory(is_staff=True)
     project_form = ProjectManagerForm(
         {
             'projects': [project1.pk, project2.pk]
         },
         user=staff,
     )
     ok_(
         project_form.is_valid(),
         'We want to be sure we are posting valid data.'
     )
     data = {'action': 'projects'}
     data.update(project_form.data)
     response = self.post_helper(data, staff)
     self.article.refresh_from_db()
     project1.refresh_from_db()
     project2.refresh_from_db()
     ok_(response.status_code, 200)
     ok_(
         self.article in project1.articles.all(),
         'The article should be added to the project.'
     )
     ok_(
         self.article in project2.articles.all(),
         'The article should be added to teh project.'
     )
Exemplo n.º 3
0
 def _projects(self, request, foia):
     """Handle updating projects"""
     form = ProjectManagerForm(request.POST)
     has_perm = foia.has_perm(request.user, 'change')
     if has_perm and form.is_valid():
         projects = form.cleaned_data['projects']
         foia.projects = projects
     return redirect(foia)
Exemplo n.º 4
0
 def _projects(self, request, foia):
     """Handle updating projects"""
     form = ProjectManagerForm(request.POST)
     if (foia.editable_by(request.user)
             or request.user.is_staff) and form.is_valid():
         projects = form.cleaned_data['projects']
         foia.projects = projects
     return redirect(foia)
Exemplo n.º 5
0
 def _project(self, foias, user, post):
     """Add the requests to the selected projects"""
     foias = [f for f in foias if f.has_perm(user, 'change')]
     form = ProjectManagerForm(post, user=user)
     if form.is_valid():
         projects = form.cleaned_data['projects']
         for foia in foias:
             foia.projects.add(*projects)
         return 'Requests added to projects'
Exemplo n.º 6
0
 def test_add_projects(self):
     """Posting a collection of projects to a request should add it to those projects."""
     project = ProjectFactory()
     form = ProjectManagerForm({'projects': [project.pk]})
     ok_(form.is_valid())
     data = {'action': 'projects'}
     data.update(form.data)
     http_post_response(self.url, self.view, data, self.foia.user, **self.kwargs)
     project.refresh_from_db()
     ok_(self.foia in project.requests.all())
Exemplo n.º 7
0
 def _projects(self, request, foia):
     """Handle updating projects"""
     form = ProjectManagerForm(request.POST, user=request.user)
     has_perm = foia.has_perm(request.user, 'change')
     if has_perm and form.is_valid():
         projects = form.cleaned_data['projects']
         for proj in itertools.chain(foia.projects.all(), projects):
             # clear cache for old and new projects
             proj.clear_cache()
         foia.projects = projects
     return redirect(foia.get_absolute_url() + '#')
Exemplo n.º 8
0
def projects(request, foia):
    """Handle updating projects"""
    form = ProjectManagerForm(request.POST, user=request.user)
    has_perm = foia.has_perm(request.user, "change")
    if has_perm and form.is_valid():
        projects_ = form.cleaned_data["projects"]
        for proj in itertools.chain(foia.projects.all(), projects_):
            # clear cache for old and new projects
            proj.clear_cache()
        foia.projects.set(projects_)
    return _get_redirect(request, foia)
Exemplo n.º 9
0
 def post(self, request, **kwargs):
     """Handles POST requests on article pages"""
     # pylint:disable=unused-argument
     article = self.get_object()
     authorized = self.request.user.is_staff
     action = request.POST.get('action')
     if not authorized:
         return HttpResponseForbidden()
     if action == 'projects':
         form = ProjectManagerForm(request.POST)
         if form.is_valid():
             projects = form.cleaned_data['projects']
             article.projects = projects
     tags = request.POST.get('tags')
     if tags:
         tag_set = set()
         for tag in parse_tags(tags):
             new_tag, _ = Tag.objects.get_or_create(name=tag)
             tag_set.add(new_tag)
         article.tags.set(*tag_set)
     return redirect(article)