def post(self, request, **kwargs): c = self.get_context_data(**kwargs) # cancel course enroll if 'cancel' == request.POST.get('action'): try: if c['user'].is_authenticated: lead = CourseLead.objects.get(course=c['course'], student=c['user']) else: if not request.session.session_key: request.session.save() lead = CourseLead.objects.get( course=c['course'], session_key=request.session.session_key, student=None) except CourseLead.DoesNotExist: client.captureException() else: lead.status = 1 lead.save() # TODO: change lead status to "cancelled by user" return HttpResponse(json.dumps({'code': 0})) # enroll here f = CourseEnrollForm(request.POST) if f.is_valid(): data = f.json() data['course'] = c['slug'] new_lead = True if c['user'].is_authenticated: lead, new_lead = CourseLead.objects.get_or_create( course=c['course'], student=c['user']) else: if not request.session.session_key: request.session.save() lead, new_lead = CourseLead.objects.get_or_create( course=c['course'], session_key=request.session.session_key, student=None) lead.name = data['name'] lead.contact = data['contact'] lead.comment = data['comment'] if new_lead: # Channel('course-enroll').send(data) pass else: if lead.status != 0: lead.status = 0 lead.save() lead.save() return HttpResponse(json.dumps({'code': 0})) else: return HttpResponse(json.dumps({ 'errors': f.errors, }))
def deliver_notification(self, notification_data): self._log('sending notification to', self._user, '\n', notification_data) self.send( json.dumps({ 'type': 'notification', 'data': notification_data }))
def build_socket_message(self, force_reencode=False): if self._msg_cache is None or force_reencode: dct = self.as_dict() self._msg_cserver.pyache = json.dumps({ 'type': 'message', 'data': { 'thread': dct['thread_uuid'], 'message': dct['message_repr'], 'received_time': dct['rcvd_time'] } }) return self._msg_cache
def post(self, request, **kwargs): # c = self.get_context_data(**kwargs) f = AddStudent(request.POST) utcnow = localtime(now()) return HttpResponse(json.dumps([{ 'id': u.pk, 'name': u.first_name, 'email': u.email } for u in User.objects.filter(lessons__start__gt=utcnow - self.last_lesson_period)]), content_type='application/json') if f.is_valid(): # new_user = User.objects.create(is_active=False) # new_user.save() # Channel('send-me-lead').send(f.json()) return HttpResponse(json.dumps({'code': 0}), content_type='application/json') else: return HttpResponse(json.dumps({ 'errors': f.errors, }), content_type='application/json')
def post(self, request, **kwargs): f = Enroll(request.POST) if f.is_valid(): # Channel('send-me-lead').send(f.json()) return HttpResponse(json.dumps({'code': 0}), content_type='application/json') else: from core.tasks import user_test user_test.apply_async(queue='root') # add.apply_async(queue='priority.high') # task.delay(arg1, arg2, kwarg1='x', kwarg2='y') return JsonResponse({ 'errors': f.errors, })
def dumps(self): return json.dumps(self.as_dict())
async def test_bytes_json(self): data = {'bytes': bytes('bytesting'.encode('utf-8'))} result = dumps(data) self.assertTrue(isinstance(result, str)) self.assertTrue(not result.startswith("b'"))
async def test_date_json(self): data = {'date': datetime.datetime.now()} result = dumps(data) self.assertTrue(isinstance(result, str))