# Path: registration/views.py
# class MemberFormView(View):
#     def get(self, request, member_id=None):
#         try:
#             member = Member.objects.get(id=member_id)
#             form = MemberForm(instance=member)
#         except Member.DoesNotExist:
#             form = MemberForm()
#             member = None
# 
#         context = dict(form=form)
#         if member:
#             context['member'] = member
#             return TemplateResponse(request, 'edit_member_form.html', context=context)
# 
#         return TemplateResponse(request, 'member_form.html', context=context)
# 
#     def post(self, request, member_id=None):
#         try:
#             member = Member.objects.get(id=member_id)
#             form = MemberForm(request.POST, instance=member)
#         except Member.DoesNotExist:
#             member = None
#             form = MemberForm(request.POST)
# 
#         if form.is_valid():
#             member_instance = form.save()
#             return HttpResponseRedirect(reverse('member_edit', kwargs=dict(member_id=member_instance.id)))
# 
#         context = {'form': form}
#         if member:
#             context['member'] = member
#         return TemplateResponse(request, 'member_form.html', context=context)
# 
# class MemberSearchView(View):
#     def get(self, request, query):
#         sqs = SearchQuerySet().models(Member).autocomplete(text=query)[:5]
#         results = [dict(name=result.object.get_full_name(), email=result.object.email, id=result.object.id)
#                    for result in sqs]
# 
#         data = json.dumps(dict(results=results))
# 
#         return HttpResponse(data, content_type='application/json')
# 
# class MemberSignIn(View):
#     @method_decorator(csrf_exempt)
#     def dispatch(self, request, *args, **kwargs):
#         return super(MemberSignIn, self).dispatch(request, *args, **kwargs)
# 
#     def post(self, request):
#         member = get_object_or_404(Member, id=request.POST.get('id'))
#         try:
#             visit = signin_member(member, request.POST.get('purpose'))
#             membership = Membership.objects.select_related('payment').filter(member=member).last()
#         except ObjectDoesNotExist:
#             membership = None
#         except ValidationError:
#             return JsonResponse(data=dict(), status=status.HTTP_400_BAD_REQUEST)
# 
#         membership_dict = dict(renewed_at=membership.renewed_at, payment=membership.payment.type,
#                                expires_at=membership.expires_at) if membership else None
#         data = dict(results=dict(id=member.id, first_name=member.first_name, last_name=member.last_name,
#                                  suspended=member.suspended, banned=member.banned,
#                                  created_at=visit.created_at.isoformat(), notes=member.notes,
#                                  membership=membership_dict))
# 
#         return JsonResponse(data=data, safe=False, status=201)
# 
#     def get(self, request):
#         visits = get_signed_in_members().prefetch_related()
#         serializer = VisitSerializer(visits, many=True)
# 
#         return JsonResponse(data=serializer.data, safe=False, status=200)
# 
# class Members(TemplateView):
#     template_name = 'members.html'
# 
#     def get(self, request):
#         members = Member.objects.all()
#         return self.render_to_response(dict(members=members))
# 
# class MemberViewSet(viewsets.ModelViewSet):
#     queryset = Member.objects.all()
#     serializer_class = MemberSerializer


from django.conf.urls import url
from .views import MemberFormView, MemberSearchView, MemberSignIn, Members, MemberViewSet


apiRoutes = (
    (r'members', MemberViewSet),
)

urlpatterns = [
    url(r'^new/$', MemberFormView.as_view(), name='member_new'),
    url(r'^search/(?P<query>[\w|\W]+)/$', MemberSearchView.as_view(), name='member_search'),
    url(r'^edit/(?P<member_id>[0-9]+)/$', MemberFormView.as_view(), name='member_edit'),
    url(r'^signin/$', MemberSignIn.as_view(), name='member_signin'),

# Path: registration/views.py
# class MemberFormView(View):
#     def get(self, request, member_id=None):
#         try:
#             member = Member.objects.get(id=member_id)
#             form = MemberForm(instance=member)
#         except Member.DoesNotExist:
#             form = MemberForm()
#             member = None
# 
#         context = dict(form=form)
#         if member:
#             context['member'] = member
#             return TemplateResponse(request, 'edit_member_form.html', context=context)
# 
#         return TemplateResponse(request, 'member_form.html', context=context)
# 
#     def post(self, request, member_id=None):
#         try:
#             member = Member.objects.get(id=member_id)
#             form = MemberForm(request.POST, instance=member)
#         except Member.DoesNotExist:
#             member = None
#             form = MemberForm(request.POST)
# 
#         if form.is_valid():
#             member_instance = form.save()
#             return HttpResponseRedirect(reverse('member_edit', kwargs=dict(member_id=member_instance.id)))
# 
#         context = {'form': form}
#         if member:
#             context['member'] = member
#         return TemplateResponse(request, 'member_form.html', context=context)
# 
# class MemberSearchView(View):
#     def get(self, request, query):
#         sqs = SearchQuerySet().models(Member).autocomplete(text=query)[:5]
#         results = [dict(name=result.object.get_full_name(), email=result.object.email, id=result.object.id)
#                    for result in sqs]
# 
#         data = json.dumps(dict(results=results))
# 
#         return HttpResponse(data, content_type='application/json')
# 
# class MemberSignIn(View):
#     @method_decorator(csrf_exempt)
#     def dispatch(self, request, *args, **kwargs):
#         return super(MemberSignIn, self).dispatch(request, *args, **kwargs)
# 
#     def post(self, request):
#         member = get_object_or_404(Member, id=request.POST.get('id'))
#         try:
#             visit = signin_member(member, request.POST.get('purpose'))
#             membership = Membership.objects.select_related('payment').filter(member=member).last()
#         except ObjectDoesNotExist:
#             membership = None
#         except ValidationError:
#             return JsonResponse(data=dict(), status=status.HTTP_400_BAD_REQUEST)
# 
#         membership_dict = dict(renewed_at=membership.renewed_at, payment=membership.payment.type,
#                                expires_at=membership.expires_at) if membership else None
#         data = dict(results=dict(id=member.id, first_name=member.first_name, last_name=member.last_name,
#                                  suspended=member.suspended, banned=member.banned,
#                                  created_at=visit.created_at.isoformat(), notes=member.notes,
#                                  membership=membership_dict))
# 
#         return JsonResponse(data=data, safe=False, status=201)
# 
#     def get(self, request):
#         visits = get_signed_in_members().prefetch_related()
#         serializer = VisitSerializer(visits, many=True)
# 
#         return JsonResponse(data=serializer.data, safe=False, status=200)
# 
# class Members(TemplateView):
#     template_name = 'members.html'
# 
#     def get(self, request):
#         members = Member.objects.all()
#         return self.render_to_response(dict(members=members))
# 
# class MemberViewSet(viewsets.ModelViewSet):
#     queryset = Member.objects.all()
#     serializer_class = MemberSerializer


# Path: registration/urls.py
from django.conf.urls import url
from .views import MemberFormView, MemberSearchView, MemberSignIn, Members, MemberViewSet



apiRoutes = (
    (r'members', MemberViewSet),
)

urlpatterns = [
    url(r'^new/$', MemberFormView.as_view(), name='member_new'),
    url(r'^search/(?P<query>[\w|\W]+)/$', MemberSearchView.as_view(), name='member_search'),
    url(r'^edit/(?P<member_id>[0-9]+)/$', MemberFormView.as_view(), name='member_edit'),
    url(r'^signin/$', MemberSignIn.as_view(), name='member_signin'),

    url(r'^$', Members.as_view(), name='members'),