0

2020欧洲杯手机版注册I am trying to recreate game of hacks because there isnt an API to create my own questions , and implement on external site , however I am using django with restful framework for this task. (I am not sure , if this is the right to achieve this). I will do this via server because I dont want people change js and bypass the stuff or even disable js and stop time , and continue with the same question

apiview.py

@api_view(['GET', 'POST'])def questions_view(request):    if request.method == 'GET':        questions = Question.objects.all()        serializer = QuestionListPageSerializer(questions, many=True)        return Response(serializer.data)    elif request.method == 'POST':        serializer = QuestionListPageSerializer(data=request.data)        if serializer.is_valid():            question = serializer.save()            return Response(QuestionListPageSerializer(question).data, status=status.HTTP_201_CREATED)        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

2020欧洲杯手机版注册serializers

class QuestionListPageSerializer(serializers.Serializer):    id = serializers.IntegerField(read_only=True)    question_text = serializers.CharField(max_length=200)    pub_date = serializers.DateTimeField()    was_published_recently = serializers.BooleanField(read_only=True) # Serializer is smart enough to understand that was_published_recently is a method on Question    code = serializers.CharField(max_length=200)    def create(self, validated_data):        return Question.objects.create(**validated_data)    def update(self, instance, validated_data):        for key, value in validated_data.items():            setattr(instance, key, value)        instance.save()        return instance

question list my app

HTTP 200 OKAllow: POST, OPTIONS, GETContent-Type: application/jsonVary: Accept[    {        "id": 1,        "question_text": "helllo",        "pub_date": "2020-01-15T02:30:40Z",        "was_published_recently": false,        "code": "int main (int argc, char *argv[])\r\n{\r\n\tchar *val = argv[argc -1];\r\n\tsystem(val);\r\n\treturn(0);\r\n}"    },    {        "id": 2,        "question_text": "What is the meaning of life?",        "pub_date": "2020-01-15T02:30:40Z",        "was_published_recently": false,        "code": "SOME STRING"    }]
question with answer my app
HTTP 200 OKAllow: PATCH, OPTIONS, DELETE, GETContent-Type: application/jsonVary: Accept{    "id": 1,    "question_text": "helllo",    "pub_date": "2020-01-15T02:30:40Z",    "was_published_recently": false,    "code": "int main (int argc, char *argv[])\r\n{\r\n\tchar *val = argv[argc -1];\r\n\tsystem(val);\r\n\treturn(0);\r\n}",    "choices": [        {            "id": 1,            "choice_text": "11111"        }    ]}

game of hacks external app

{  "_id": "53fb4014e5d4d40400c7fa4f",  "answers": [    "LDAP Injection",    "CGI Reflected XSS",    "Connection String Injection",    "Reflected XSS"  ],  "batch_score": 39.89902034664657,  "checkmarx": true,  "from": "Gilad",  "language": "PHP",  "level": "1",  "question": "What vulnerability the following code contains?",  "snippets": [    {      "code": "<?php\n$dn = $_GET['host'];\n$filter=\"(|(sn=$person*)(givenname=$person*))\";\n$justthese = array(\"ou\", \"sn\", \"givenname\", \"mail\");\n$sr=ldap_search($ds, $dn, $dn, $justthese);\n$info = ldap_get_entries($ds, $sr);\necho $info[\"count\"].\" entries returned\n\";?>"    }  ],  "tip": ""}

example

answer

{quest: true, score: 5700}quest: truescore: 5700

# make random instead of api/poll/question/{id} ?

1

You can use python random2020欧洲杯手机版注册 library in your view:

from random import randintdef random_question(request):    i = randint(0, Question.objects.count() - 1)    question = Question.objects.all()[i]  # get question at random position    ....
  • Use "Question.objects.get(id=i)" instead of "Question.objects.all()[i]". – wfehr 2 days ago
  • 1
    @wfehr if you delete an instance of Question, that ID will be missing from the database, and if you try to access that ID you will get an exception. With my answer you don't need to worry about exceptions. – Pedro yesterday
  • Right, didn't think of that. – wfehr yesterday

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.