Example #1
0
 def post(self, request, catalog_id):
     """
     Update or delete this catalog.
     """
     client = self.get_catalog_api_client(request.user)
     if request.POST.get('delete-catalog') == 'on':
         response = client.delete(
             urljoin(f"{self.catalogs_api_url}/", f"{catalog_id}/"))
         response.raise_for_status()
         return redirect(reverse('api_admin:catalog-search'))
     form = CatalogForm(request.POST)
     if not form.is_valid():
         response = client.get(
             urljoin(f"{self.catalogs_api_url}/", f"{catalog_id}/"))
         response.raise_for_status()
         catalog = Catalog(attributes=response.json())
         return render_to_response(self.template_name,
                                   self.get_context_data(catalog, form),
                                   status=400)
     catalog_response = client.patch(urljoin(f"{self.catalogs_api_url}/",
                                             f"{catalog_id}/"),
                                     data=form.cleaned_data)
     catalog_response.raise_for_status()
     return redirect(
         reverse('api_admin:catalog-edit',
                 kwargs={'catalog_id': catalog_response.json()['id']}))
Example #2
0
    def post(self, request, username):
        """Create a new catalog for a user."""
        form = CatalogForm(request.POST)
        client = course_discovery_api_client(request.user)
        if not form.is_valid():
            return render_to_response(self.template, self.get_context_data(client, username, form), status=400)

        attrs = form.instance.attributes
        catalog = client.catalogs.post(attrs)
        return redirect(reverse('api_admin:catalog-edit', kwargs={'catalog_id': catalog['id']}))
Example #3
0
 def post(self, request, catalog_id):
     """Update or delete this catalog."""
     client = course_discovery_api_client(request.user)
     if request.POST.get('delete-catalog') == 'on':
         client.catalogs(catalog_id).delete()
         return redirect(reverse('api_admin:catalog-search'))
     form = CatalogForm(request.POST)
     if not form.is_valid():
         response = client.catalogs(catalog_id).get()
         catalog = Catalog(attributes=response)
         return render_to_response(self.template_name, self.get_context_data(catalog, form, client), status=400)
     catalog = client.catalogs(catalog_id).patch(form.instance.attributes)
     return redirect(reverse('api_admin:catalog-edit', kwargs={'catalog_id': catalog['id']}))
Example #4
0
 def get(self, request, catalog_id):
     """Display a form to edit this catalog."""
     client = course_discovery_api_client(request.user)
     response = client.catalogs(catalog_id).get()
     catalog = Catalog(attributes=response)
     form = CatalogForm(instance=catalog)
     return render_to_response(self.template_name, self.get_context_data(catalog, form, client))
Example #5
0
 def get(self, request, username):
     """
     Display a list of a user's catalogs.
     """
     client = self.get_catalog_api_client(request.user)
     form = CatalogForm(initial={'viewers': [username]})
     return render_to_response(
         self.template, self.get_context_data(client, username, form))
Example #6
0
    def post(self, request, username):
        """Create a new catalog for a user."""
        form = CatalogForm(request.POST)
        client = course_discovery_api_client(request.user)
        if not form.is_valid():
            catalogs = self._get_catalogs(client, username)
            return render_to_response(self.template, {
                'form': form,
                'catalogs': catalogs,
                'username': username,
                'preview_url': reverse('api_admin:catalog-preview'),
                'catalog_api_url': client.api.v1.courses.url(),
            }, status=400)

        attrs = form.instance.attributes
        catalog = client.api.v1.catalogs.post(attrs)
        return redirect(reverse('api_admin:catalog-edit', kwargs={'catalog_id': catalog['id']}))
Example #7
0
 def post(self, request, catalog_id):
     """Update or delete this catalog."""
     client = course_discovery_api_client(request.user)
     if request.POST.get('delete-catalog') == 'on':
         client.api.v1.catalogs(catalog_id).delete()
         return redirect(reverse('api_admin:catalog-search'))
     form = CatalogForm(request.POST)
     if not form.is_valid():
         response = client.api.v1.catalogs(catalog_id).get()
         catalog = Catalog(attributes=response)
         return render_to_response('api_admin/catalogs/edit.html', {
             'catalog': catalog,
             'form': form,
             'preview_url': reverse('api_admin:catalog-preview'),
             'catalog_api_url': client.api.v1.courses.url(),
         }, status=400)
     catalog = client.api.v1.catalogs(catalog_id).patch(form.instance.attributes)
     return redirect(reverse('api_admin:catalog-edit', kwargs={'catalog_id': catalog['id']}))
Example #8
0
    def post(self, request, username):
        """
        Create a new catalog for a user.
        """
        form = CatalogForm(request.POST)
        client = self.get_catalog_api_client(request.user)
        if not form.is_valid():
            return render_to_response(self.template,
                                      self.get_context_data(
                                          client, username, form),
                                      status=400)

        attrs = form.cleaned_data
        response = client.post(self.catalogs_api_url, data=attrs)
        response.raise_for_status()
        catalog = response.json()
        return redirect(
            reverse('api_admin:catalog-edit',
                    kwargs={'catalog_id': catalog['id']}))
Example #9
0
 def get(self, request, username):
     """Display a list of a user's catalogs."""
     client = course_discovery_api_client(request.user)
     catalogs = self._get_catalogs(client, username)
     return render_to_response(self.template, {
         'username': username,
         'catalogs': catalogs,
         'form': CatalogForm(initial={'viewers': [username]}),
         'preview_url': reverse('api_admin:catalog-preview'),
         'catalog_api_url': client.api.v1.courses.url(),
     })
Example #10
0
 def get(self, request, catalog_id):
     """
     Display a form to edit this catalog.
     """
     client = self.get_catalog_api_client(request.user)
     response = client.get(
         urljoin(f"{self.catalogs_api_url}/", f"{catalog_id}/"))
     response.raise_for_status()
     catalog = Catalog(attributes=response.json())
     form = CatalogForm(instance=catalog)
     return render_to_response(self.template_name,
                               self.get_context_data(catalog, form))
Example #11
0
 def get(self, request, catalog_id):
     """Display a form to edit this catalog."""
     client = course_discovery_api_client(request.user)
     response = client.api.v1.catalogs(catalog_id).get()
     catalog = Catalog(attributes=response)
     form = CatalogForm(instance=catalog)
     return render_to_response('api_admin/catalogs/edit.html', {
         'catalog': catalog,
         'form': form,
         'preview_url': reverse('api_admin:catalog-preview'),
         'catalog_api_url': client.api.v1.courses.url(),
     })
Example #12
0
 def get(self, request, username):
     """Display a list of a user's catalogs."""
     client = course_discovery_api_client(request.user)
     response = client.api.v1.catalogs.get(username=username)
     catalogs = [Catalog(attributes=catalog) for catalog in response['results']]
     return render_to_response(self.template, {
         'username': username,
         'catalogs': catalogs,
         'form': CatalogForm(initial={'viewers': [username]}),
         'preview_url': reverse('api_admin:catalog-preview'),
         'catalog_api_url': client.api.v1.courses.url(),
     })