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

V2.0 url refactor #332

Open
wants to merge 3 commits into
base: v2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions konfera/models/ticket_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class TicketType(FromToModel):
def __str__(self):
return self.title

def _get_current_status(self):
def get_current_status(self):
now = timezone.now()
status = TicketType.ACTIVE

Expand All @@ -81,7 +81,7 @@ def _get_current_status(self):

@property
def status(self):
return TicketType.STATUSES[self._get_current_status()]
return TicketType.STATUSES[self.get_current_status()]

@property
def issued_tickets(self):
Expand Down
2 changes: 2 additions & 0 deletions konfera/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ class TicketTypeListSerializer(serializers.ModelSerializer):
class Meta:
model = TicketType
fields = [
'uuid',
'title',
'description',
'price',
'status',
'attendee_type',
'usage',
]

Expand Down
14 changes: 8 additions & 6 deletions konfera/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ def test_get_ticket_type_list(self):
# We have 2 events and 2 ticket types, but only PUBLIC ticket type for self.event should be loaded
self.assertEquals(len(json.loads(response.content.decode('utf-8'))), 1)
self.assertEquals(json.loads(response.content.decode('utf-8')), [{
'uuid': str(self.tt2.uuid),
'title': self.tt2.title,
'description': self.tt2.description,
'price': str(self.tt2.price),
'status': self.tt2.status,
'attendee_type': self.tt2.attendee_type,
'usage': self.tt2.usage,
}])

Expand All @@ -114,7 +116,7 @@ def setUp(self):
cfp_end=now + day, status=Event.PUBLIC)
self.event2 = mommy.make(Event, organizer=organizer, date_from=now + day, date_to=now + 2 * day,
status=Event.PUBLIC)
self.url = '/event/%s/ticket-types/' % self.event.slug
self.url = '/event/%s/ticket-types/detail/' % self.event.slug

self.tt = mommy.make(TicketType, event=self.event, date_from=now, date_to=now + 3 * day,
attendee_type=TicketType.ATTENDEE, accessibility=TicketType.PRIVATE)
Expand Down Expand Up @@ -157,8 +159,8 @@ def test_get_ticket_type_details(self):

class AidTicketViewSetTest(APITestCase):
def setUp(self):
self.url = '/tickets/aid/'
self.tt = mommy.make(TicketType, date_from=now, date_to=now + day, attendee_type=TicketType.AID)
self.url = '/event/%s/tickets/aid/' % self.tt.event.slug

def test_post_valid_data_saves_object_and_returns_uuid(self):
data = {
Expand Down Expand Up @@ -270,7 +272,7 @@ def test_post_valid_data_saves_object_and_returns_uuid(self):
'email': '[email protected]',
},
}
response = self.client.post('/talks/', data, format='json')
response = self.client.post('/event/' + self.event.slug + '/talks/', data, format='json')

talks = Talk.objects.all()
speakers = Speaker.objects.all()
Expand All @@ -281,7 +283,7 @@ def test_post_valid_data_saves_object_and_returns_uuid(self):
self.assertEqual(response.data, {'key': talks.first().uuid})

def test_invalid_data_shows_validation_erros(self):
response = self.client.post('/talks/', {'primary_speaker': {}}, format='json')
response = self.client.post('/event/' + self.event.slug + '/talks/', {'primary_speaker': {}}, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data, {
'event_uuid': ['This field is required.'],
Expand All @@ -296,7 +298,7 @@ def test_invalid_data_shows_validation_erros(self):

def test_get_cfp_by_uuid(self):
talk = mommy.make(Talk, event=self.event)
response = self.client.get('/talks/' + str(talk.uuid) + '/', format='json')
response = self.client.get('/event/' + talk.event.slug + '/talks/' + str(talk.uuid) + '/', format='json')

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEquals(json.loads(response.content.decode('utf-8')), {
Expand Down Expand Up @@ -359,7 +361,7 @@ def test_update_cfp(self):
}
}

response = self.client.put('/talks/' + str(talk.uuid) + '/', data, format='json')
response = self.client.put('/event/' + talk.event.slug + '/talks/' + str(talk.uuid) + '/', data, format='json')

self.maxDiff = None
self.assertEqual(response.status_code, status.HTTP_200_OK)
Expand Down
9 changes: 4 additions & 5 deletions konfera/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@

router = routers.SimpleRouter()
router.register(r'events', EventViewSet)
router.register(r'talks', TalkViewSet)
router.register(r'tickets/aid', AidTicketViewSet)


router.register(r'event/(?P<slug>.+)/talks', TalkViewSet)
router.register(r'event/(?P<slug>.+)/tickets/aid', AidTicketViewSet)
# urlpatterns = router.urls

urlpatterns = [
url(r'^', include(router.urls)),
url('^event/(?P<slug>.+)/ticket-types/detail/(?P<uuid>.+)/$', EventTicketTypeDetail.as_view()),
url('^event/(?P<slug>.+)/ticket-types/$', EventTicketTypesList.as_view()),
url('^event/(?P<slug>.+)/ticket-types/(?P<uuid>.+)/$', EventTicketTypeDetail.as_view()),
url('^event/(?P<slug>.+)/ticket-types/(?P<attendee_type>.+)/$', EventTicketTypesList.as_view()),
]
13 changes: 12 additions & 1 deletion konfera/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@ class EventTicketTypesList(generics.ListAPIView):

def get_queryset(self):
slug = self.kwargs['slug']
return TicketType.objects.filter(event__slug=slug, accessibility=TicketType.PUBLIC)
attendee_type = self.kwargs.get('attendee_type', None)
exclude = []
all_ticket_types = TicketType.objects.filter(event__slug=slug, accessibility=TicketType.PUBLIC)

for ticket_type in all_ticket_types:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines are an ideal candidate for list comprehension.

if ticket_type.get_current_status() != TicketType.ACTIVE:
exclude.append(str(ticket_type.uuid))

if attendee_type:
all_ticket_types = all_ticket_types.filter(attendee_type=attendee_type)

return all_ticket_types.exclude(uuid__in=exclude)


class EventTicketTypeDetail(generics.RetrieveAPIView):
Expand Down