def regist(self,request,community_id): ''' 注册 :param request: :param community: :return: ''' data = request.data passwd = data.get('password',None) id_card = data.get('id_card',None) email = data.get('email',None) username = data.get('username',None) q = Q(Q(username=username)|Q(id_card=id_card)|Q(email=email)) queryset = User.objects.filter(q) if queryset: return JsonResponse(data={'status':0,'msg':'您的账号被已注册'},status=0) try: user = User.objects.create(username=username,address = data.get('address',None),email=email,id_card=data.get('id_card',None)) except: return JsonResponse(data={'status':0,'msg':'注册失败'},status=0) user.set_password(passwd) user.save() down = send_email_tt.s(user) down.delay() community = get_obj(Community,community_id) community.members.add(user) return JsonResponse(data={'status':1,'msg':'注册成功'},status=1)
def comment(self, request, pk): user = request.user obj = get_obj(self.model_class,pk) serializer = CommentSerializer(data=request.data) if not serializer.is_valid(): return JsonResponse(u'数据格式错误', code=JsonResponse.CODE_ERROR_DATA, data=serializer.errors) comment = Comment.objects.create(author=user, content_object=obj, **serializer.validated_data) serializer = CommentSerializer(comment) return JsonResponse(u'ok', data=serializer.data,status=1)
def get_resource_category(self, request, pkc): category = get_obj(Category, pkc) if isinstance(category, Category): res = self.model_class.objects.filter(Q(category=category), Q(is_used=False)) ser = self.get_serializer(res, many=True).data return JsonResponse('ok', status=1, data=ser) else: return JsonResponse(status=0)
def get_comment_comments(self, request, pk): """ 获取我的评论,时间逆序访问 :param request: :return: """ obj = get_obj(self.model_class, pk) com = ContentType.objects.get(app_label="comments") comments = self.queryset.filter(object_id=pk, content_type_id=com.id) serializer = self.get_serializer(comments, many=True).data return JsonResponse('ok', data=serializer, status=1)
def cancle_foc(self, request, pk): ''' 关注资源 :param request: :return: ''' user = request.user obj = get_obj(self.model_class, pk) obj.focus.remove(user) obj.save() return JsonResponse(u'关注成功', status=1)
def return_back(self, request): ''' 归还 :param request: :return: ''' order_id = request.data.get('order_id', None) order = get_obj(self.model_class, order_id) if order.process in [1, 2]: return JsonResponse(u'资源没有在使用中,请检查', status=0) order.process = 4 order.save() return JsonResponse('ok', status=1)
def create(self, request, pk=None): ''' 创建资源 :param request: :param args: :param kwargs: :return: ''' import urllib data = request.data user = request.user # print data category_id = data.pop('category', []) resource_pictures = data.pop('resource_picture', []) print resource_pictures resource_pictures = urllib.unquote_plus(resource_pictures) print resource_pictures fname = str(resource_pictures).split('/')[-1] file_size = os.path.getsize(settings.MEDIA_ROOT + resource_pictures.split('/media')[-1]) with open(settings.MEDIA_ROOT + resource_pictures.split('/media')[-1], 'r') as f: resource_picture = UploadedFile(f, name=fname, size=file_size) data['resource_picture'] = resource_picture if not pk: serializer = self.get_serializer(data=data) if not serializer.is_valid(): return JsonResponse(code=JsonResponse.CODE_ERROR_DATA, data=serializer.errors) else: try: res = self.model_class.objects.get(id=pk) except self.model_class.DoesNotExist: return JsonResponse(u'请选择合适的资源', status=0) resource_picture = data.get('resource_picture', None) if not resource_picture: data.pop('resource_picture') serializer = self.get_serializer(instance=res, data=data) if not serializer.is_valid(): return JsonResponse(code=JsonResponse.CODE_ERROR_DATA, data=serializer.errors) community = user.belong_to_community.first() print 'com', community serializer.save(master=user, community_id=community.id) resource = serializer.instance # resource.community=community category = get_obj(Category, category_id) resource.category.add(category) resource.save() return JsonResponse(u'成功', status=1)
def modify(self, request, id): ''' 修改信息 :param request: :param id: :return: ''' data = request.data obj = get_obj(self.model_class,id) for key in data.keys(): # print key,data[key] obj.__setattr__(key, data[key]) obj.save() return JsonResponse(u'成功',status=1)
def comment_list(self, request, pk): obj = get_obj(self.model_class,pk) content_id = ContentType.objects.get(app_label="comments").id queryset = obj.comments.all().order_by('-dateline') serializer_class = CommentSerializer page = self.paginate_queryset(queryset) serializer = self.get_serializer(page, many=True) if not serializer_class \ else serializer_class(page, context={'request': request}, many=True) other_dict = OrderedDict([ ('count', self.paginator.page.paginator.count), ('next', self.paginator.get_next_link()), ('previous', self.paginator.get_previous_link()), ('content_id',content_id), ]) return JsonResponse(data=serializer.data, other_dict=other_dict,status=1)
def cancel(self, request): ''' 取消 :param request: :return: ''' order_id = request.data.get('order_id', None) order = get_obj(self.model_class, order_id) if order.process != 1: return JsonResponse(u'资源在使用中获取已关闭,请检查', status=0) order.process = 2 order.resource.is_used = False order.resource.save() order.save() return JsonResponse('ok', status=1)
def order_operation(self, request): ''' 订单操作,取消订单,锁定,使用,归还 :param request: :return: ''' data = request.data id = data.get('order_id', None) process_type = data.get('process', None) if not process_type and not id: return JsonResponse(JsonResponse.CODE_ERROR_DATA, status=0) order = get_obj(self.model_class, id) order.process = process_type if process_type == '4': #归还 order.end_time = datetime.datetime.now() elif process_type == '2': order.resource.is_used = False elif process_type == '3': #使用 order.buyer.intergration = order.buyer.intergration - order.intergration #使用 用户积分扣除 order.master.intergration = order.master.intergration + order.intergration #发布 用户积分增加 order.save() return JsonResponse('ok', status=1)
def delete(self, request, pk): obj = get_obj(self.model_class,pk) obj.delete() return JsonResponse(msg=u'删除成功',status=1)
def detail(self, request, pk): obj = get_obj(self.model_class,pk) serializer = self.serializer_class(obj, context={'request': request}) return JsonResponse(data=serializer.data,status=1)