Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing the phonebook api for json response #1412

Merged
merged 5 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/phonebook/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from django.urls import path
from django.urls import re_path

from .views import DectExportView
from .views import DectExportCsvView
from .views import DectExportJsonView
from .views import DectRegistrationCreateView
from .views import DectRegistrationDeleteView
from .views import DectRegistrationListView
Expand All @@ -12,7 +13,8 @@
app_name = "phonebook"
urlpatterns = [
path("", PhonebookListView.as_view(), name="list"),
path("csv/", DectExportView.as_view(), name="csv"),
path("csv/", DectExportCsvView.as_view(), name="csv"),
path("json/", DectExportJsonView.as_view(), name="json"),
path(
"dectregistrations/",
include(
Expand Down
37 changes: 36 additions & 1 deletion src/phonebook/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.exceptions import PermissionDenied
from django.core.exceptions import ValidationError
from django.http import HttpResponse
from django.shortcuts import redirect
Expand All @@ -14,18 +15,52 @@
from django.views.generic import DeleteView
from django.views.generic import ListView
from django.views.generic import UpdateView
from jsonview.views import JsonView
from oauth2_provider.views.generic import ScopedProtectedResourceView

from .mixins import DectRegistrationViewMixin
from .models import DectRegistration
from camps.mixins import CampViewMixin
from teams.models import Team
from utils.mixins import RaisePermissionRequiredMixin
from utils.mixins import UserIsObjectOwnerMixin

logger = logging.getLogger("bornhack.%s" % __name__)


class DectExportView(
class DectExportJsonView(
CampViewMixin,
JsonView,
):
"""
JSON export for the POC team / DECT system
"""

required_scopes = ["phonebook:read"]

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
team = Team.objects.get(name="POC", camp=self.camp)
if self.request.user not in team.responsible_members:
raise PermissionDenied("Permission denied")
context["phonebook"] = self.dump_phonebook()
return context

Check warning on line 47 in src/phonebook/views.py

View check run for this annotation

Codecov / codecov/patch

src/phonebook/views.py#L42-L47

Added lines #L42 - L47 were not covered by tests

def dump_phonebook(self):
phonebook = []
for dect in DectRegistration.objects.filter(camp=self.camp):
phonebook.append(

Check warning on line 52 in src/phonebook/views.py

View check run for this annotation

Codecov / codecov/patch

src/phonebook/views.py#L50-L52

Added lines #L50 - L52 were not covered by tests
{
"number": dect.number,
"letters": dect.letters,
"description": dect.description,
"activation_code": dect.activation_code,
},
)
return phonebook

Check warning on line 60 in src/phonebook/views.py

View check run for this annotation

Codecov / codecov/patch

src/phonebook/views.py#L60

Added line #L60 was not covered by tests


class DectExportCsvView(
CampViewMixin,
RaisePermissionRequiredMixin,
ScopedProtectedResourceView,
Expand Down