unity user login

unity user login

 
 

pythonCopy code

def client_login(request): user = authenticate(request, username=request.POST.get("username"), password=request.POST.get("password")) if not user: return JsonResponse(data={ "success": False, "errorcode": 2, }) data = { "success": True, "sessionId": str(user.id), "user": { "uuid": str(user.id), "a02": user.last_name, # 昵称x "a03": user.username, # 用户名 "a05": user.information.get_sex_display(), # 性别 "a06": str((datetime.date.today() - user.information.birth).days / 365), # 年龄 "a07": user.information.jiguan or "", # 籍贯 "a08": user.information.education or "", # 文化程度 "a09": user.information.date_joined or "", # 入伍时间 "a10": user.information.level or "", # 等级 } } print(data) return JsonResponse(data=data)

This piece of code is a backend interface called "client_login" that handles user login requests.

Here is an explanation of the code:

  1. Defines an interface function named "client_login", which receives a request object.
  2. Use the authenticate() method to verify whether the user's username and password are correct. Use the request.POST.get() method to get the "username" and "password" values ​​in the request parameters and pass them to the function.
  3. If the user authentication fails (that is, the authentication fails), a JSON response containing "success" as False and "errorcode" as 2 is returned, indicating that the login failed.
  4. If user authentication is successful, do the following:
    • Create a dictionary named "data" to save the data returned to the front end after successful login.
    • Set the key-value pairs of the "data" dictionary, including "success" as True, "sessionId" as the user's unique identifier (converted to a string using str(user.id)), and other user-related information.
  5. Finally, use JsonResponse to return the "data" dictionary to the front end as a JSON response, indicating that the login is successful.

The function of this interface is to receive the login request sent by the front end, and verify the user's username and password. If the verification is successful, return the relevant information of the user, including sessionId and basic user information; if the verification fails, return an error message.

Click on Intuitive Decision Making (Exercise) to the left

 
 

pythonCopy code

def get_topic(request): return JsonResponse(data={ "data": [ { "a03": q.text, "a04": 1 if q.answer else 0, "a05": "a05", "a06": "", "a07": "", } for q in Question.objects.filter().order_by("?")[:1] ] })

This piece of code is a backend interface named "get_topic", its function is to get a topic for evaluation.

Explain the meaning of this code as follows:

  1. An interface function named "get_topic" is defined, which receives a request object.
  2. In the interface function, the question is obtained by querying the Question model. Here the Question.objects.filter() method is used to get all the records in the Question table, and a question is randomly selected by .order_by("?")[:1].
  3. Use list comprehension to process and assemble the obtained questions, and use the text (q.text) of each question as the value of the key "a03". If the answer to the question (q.answer) is judged to be true, assign 1, otherwise assign 0. Also, there are some other keys (a05, a06, a07) temporarily assigned fixed values.
  4. Finally, use JsonResponse to return the processed question data to the front end and respond in JSON format.

The function of this piece of code is to randomly obtain a question from the database question table, assemble the question text and answer into a JSON response in a specific format, and return it to the front end for evaluation.

If you have any other questions about this code, feel free to ask.

Click on the right side of intuitive decision (assessment)

 
 

pythonCopy code

def get_right_topic(request): return JsonResponse(data={ "data": [ { "a03": q.text, "a04": 1, "a05": "a05", "a06": "", "a07": "", } for q in Question.objects.filter(answer=True).order_by("?")[:0] ] })

This piece of code is a backend interface called "get_right_topic". Compared with the previous code, it gets a correct topic for evaluation.

Here is an explanation of this code:

  1. An interface function named "get_right_topic" is defined, which receives a request object.
  2. In the interface function, the question with the correct answer is obtained through the query operation on the Question model. Here, the Question.objects.filter(answer=True) method is used to obtain the true records in the Question table, and 0 questions are selected through .order_by("?")[:0], that is, no questions are queried.
  3. Use list comprehension to process and assemble the obtained questions, use the text of each question (q.text) as the value of the key "a03", the answer is fixed as 1, and other keys (a05, a06, a07) are temporarily given fixed value.
  4. Finally, use JsonResponse to return the processed question data to the front end and respond in JSON format.

Compared with the previous code, the function of this code is to obtain a question with the correct answer for evaluation, instead of randomly obtaining questions. It may be to test the processing of the correct answer in a specific scenario, or other purposes.

If you have any other questions about this code, feel free to ask.

After choosing whether to destroy, go to the interface

 
 

pythonCopy code

def save_grade(request): uuid = request.POST.get("sessionId") print(uuid) grade = Grade.objects.create( user=User.objects.get(id=uuid), duration=request.POST.get("a07"), grade_type=request.POST.get("a08"), ) for topic in json.loads(request.POST.get("topicList")): Answer.objects

Guess you like

Origin blog.csdn.net/weixin_42759398/article/details/131740704