Exemplo n.º 1
0
    def create(self, validated_data):
        qobj = Question()
        qobj.topic = validated_data['topic']
        qobj.detail = validated_data['detail']

        user = None
        request = self.context.get("request")
        if request and hasattr(request, "user"):
            user = request.user
            uobj = Euser.objects.get(username=str(user.username))
            qobj.user = uobj
        qobj.save()

        for t in validated_data['tags_list']:

            try:
                cobj = Category.objects.get(tag=t)

            except Category.DoesNotExist:
                cobj = None

            if cobj is not None:
                qobj.tag_list.add(cobj)

        qobj.save()

        return qobj
Exemplo n.º 2
0
 def post(self, request):
     if not request.user.is_authenticated():
         return HttpResponse(json.dumps({
             "status": "fail",
             "msg": "用户未登录"
         }),
                             content_type='application/json')
     content = request.POST.get('content', '')
     obj_type = request.POST.get('obj_type', '')
     obj_id = request.POST.get('obj_id', '')
     if int(obj_id) > 0 and content:
         question = Question()
         question.content = content
         question.user = request.user
         question.question_type = '3'
         question.question_source = '2'
         question.file_linenum = 9999
         if obj_type == 'file':
             file = File.objects.get(id=obj_id)
             question.content_object = file
             question.file_id = file.id
         elif obj_type == 'function':
             function = Function.objects.get(id=obj_id)
             question.content_object = function
             question.function_id = function.id
         elif obj_type == 'line':
             line = Line.objects.get(id=obj_id)
             question.content_object = line
             question.file_linenum = line.file_linenum
             question.file_id = line.file_id
             if line.function_linenum:
                 question.function_linenum = line.function_linenum
                 question.function_id = line.function_id
                 question.function_content = content
         question.save()
         return HttpResponse('{"status":"success","msg":"提问成功"}',
                             content_type='application/json')
     else:
         return HttpResponse('{"status":"fail","msg":"提问失败"}',
                             content_type='application/json')