Пример #1
0
 def _get_catalogs(self, client, username):
     """Retrieve catalogs for a user. Returns the empty list if none are found."""
     try:
         response = client.catalogs.get(username=username)
         return [Catalog(attributes=catalog) for catalog in response['results']]
     except HttpNotFoundError:
         return []
Пример #2
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))
Пример #3
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']}))
Пример #4
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))
Пример #5
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(),
     })
Пример #6
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(),
     })
Пример #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.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']}))
Пример #8
0
 def _get_catalogs(self, client, username):
     """
     Retrieve catalogs for a user. Returns the empty list if none are found.
     """
     try:
         response = client.get(self.catalogs_api_url,
                               params={"username": username})
         response.raise_for_status()
         return [
             Catalog(attributes=catalog)
             for catalog in response.json()['results']
         ]
     except HTTPError as err:
         if err.response.status_code == 404:
             return []
         else:
             raise
Пример #9
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']}))
Пример #10
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():
            response = client.api.v1.catalogs.get(username=username)
            catalogs = [Catalog(attributes=catalog) for catalog in response['results']]
            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']}))