<commit_before>from django.shortcuts import render
from django.http import HttpResponse, JsonResponse, HttpResponseBadRequest, HttpResponseNotFound, HttpResponseNotAllowed
from rest_framework import viewsets
from rest_framework.views import APIView
from rest_framework.response import Response
from db.models import *
from db.serializers import *
from django.core import serializers
from django.core.serializers.json import DjangoJSONEncoder
from django.views.decorators.http import require_GET, require_POST
from datetime import datetime
from datetime import date
import time
from django.views.decorators.csrf import csrf_exempt
import json
from django.contrib.auth.hashers import BCryptPasswordHasher


@csrf_exempt
def authenticate(request):
    if request.method == "POST":
        request_params = json.loads(request.body)
        email = request_params.get("email", None)
        password = request_params.get("password", None)
        if not email or not password:
            return HttpResponseBadRequest("Missing email or password")

        user = User.objects.get(email=email)
        if not user:
            return HttpResponseNotFound("User does not exist")

        if BCryptPasswordHasher().verify(password, user.password):
            return HttpResponse("Security Token Authenticated")
        else:
            return HttpResponseNotAllowed("Invalid email/password")


@csrf_exempt
def users(request):
    if request.method == "GET":
        query = User.objects.all()
        request_params = request.GET

        email = request_params.get("email", None)
        if email:
            query = query.filter(email=email)

        serialized_json = serializers.serialize("json", query)

        return HttpResponse(serialized_json, content_type="application/json")
    elif request.method == "PUT":
        request_params = json.loads(request.body)
        first_name = request_params.get("first_name", "")
        last_name = request_params.get("last_name", "")
        address = request_params.get("address", "")
        gender = request_params.get("gender", "N")
        birthday = request_params.get("birthday", time.time())
        birthday = datetime.fromtimestamp(birthday)
        email = request_params.get("email", "")
        password = request_params.get("password", "<PASSWORD>")
        healthcard = request_params.get("healthcard", "")
        doctor_id = request_params.get("doctor_id", None)
        if not doctor_id:
            return HttpResponseBadRequest("Empty Doctor ID")
        doctor = Doctor.objects.get(id=doctor_id)
        if not doctor:
            return HttpResponseNotFound("Unable to find doctor")

        user = User(first_name=first_name, last_name=last_name, address=address, gender=gender, birthday=birthday,
                    email=email,
                    password=<PASSWORD>().encode(password=password, salt=BCryptPasswordHasher().salt()),
                    healthcard=healthcard, doctor=doctor)
        user.save()

        return HttpResponse(json.dumps(user.to_dict()), content_type="application/json")


@csrf_exempt
def users_by_id(request, user_id):
    if request.method == "GET":
        user = User.objects.get(id=user_id)
        if not user:
            return HttpResponseNotFound("User not found")
        return HttpResponse(json.dumps(user.to_dict()), content_type="application/json")
    elif request.method == "POST":
        request_params = json.loads(request.body)
        user = User.objects.get(id=user_id)
        if not user:
            return HttpResponseNotFound("Unable to find doctor")
        first_name = request_params.get("first_name", None)
        last_name = request_params.get("last_name", None)
        address = request_params.get("address", None)
        gender = request_params.get("gender", None)
        birthday = request_params.get("birthday", None)
        birthday = datetime.fromtimestamp(birthday)
        email = request_params.get("email", None)
        password = request_params.get("password", None)
        healthcard = request_params.get("healthcard", None)
        doctor_id = request_params.get("doctor_id", None)
        doctor = None
        if doctor_id:
            doctor = Doctor.objects.get(id=doctor_id)
            if not doctor:
                return HttpResponseNotFound("Unable to find doctor")
        if first_name:
            user.first_name = first_name
        if last_name:
            user.last_name = last_name
        if address:
            user.address = address
        if gender:
            user.gender = gender
        if birthday:
            user.birthday = birthday
        if email:
            user.email = email
        if password:
            user.password = password
        if healthcard:
            user.healthcard = healthcard
        if doctor:
            user.doctor = doctor

        user.save()

        return HttpResponse(json.dumps(user.to_dict()), content_type="application/json")


@csrf_exempt
def users_by_email(request, email):
    if request.method == "GET":
        user = User.objects.get(email=email)
        if not user:
            return HttpResponseNotFound("User not found")
        return HttpResponse(json.dumps(user.to_dict()), content_type="application/json")
    elif request.method == "POST":
        request_params = json.loads(request.body)
        user = User.objects.get(email=email)
        if not user:
            return HttpResponseNotFound("Unable to find doctor")
        first_name = request_params.get("first_name", None)
        last_name = request_params.get("last_name", None)
        address = request_params.get("address", None)
        gender = request_params.get("gender", None)
        birthday = request_params.get("birthday", None)
        email = request_params.get("email", None)
        password = request_params.get("password", None)<commit_msg>Fixed password hashing on post user<commit_after>from django.shortcuts import render
from django.http import HttpResponse, JsonResponse, HttpResponseBadRequest, HttpResponseNotFound, HttpResponseNotAllowed
from rest_framework import viewsets
from rest_framework.views import APIView
from rest_framework.response import Response
from db.models import *
from db.serializers import *
from django.core import serializers
from django.core.serializers.json import DjangoJSONEncoder
from django.views.decorators.http import require_GET, require_POST
from datetime import datetime
from datetime import date
import time
from django.views.decorators.csrf import csrf_exempt
import json
from django.contrib.auth.hashers import BCryptPasswordHasher


@csrf_exempt
def authenticate(request):
    if request.method == "POST":
        request_params = json.loads(request.body)
        email = request_params.get("email", None)
        password = request_params.get("password", None)
        if not email or not password:
            return HttpResponseBadRequest("Missing email or password")

        user = User.objects.get(email=email)
        if not user:
            return HttpResponseNotFound("User does not exist")

        if BCryptPasswordHasher().verify(password, user.password):
            return HttpResponse("Security Token Authenticated")
        else:
            return HttpResponseNotAllowed("Invalid email/password")


@csrf_exempt
def users(request):
    if request.method == "GET":
        query = User.objects.all()
        request_params = request.GET

        email = request_params.get("email", None)
        if email:
            query = query.filter(email=email)

        serialized_json = serializers.serialize("json", query)

        return HttpResponse(serialized_json, content_type="application/json")
    elif request.method == "PUT":
        request_params = json.loads(request.body)
        first_name = request_params.get("first_name", "")
        last_name = request_params.get("last_name", "")
        address = request_params.get("address", "")
        gender = request_params.get("gender", "N")
        birthday = request_params.get("birthday", time.time())
        birthday = datetime.fromtimestamp(birthday)
        email = request_params.get("email", "")
        password = request_params.get("password", "<PASSWORD>")
        healthcard = request_params.get("healthcard", "")
        doctor_id = request_params.get("doctor_id", None)
        if not doctor_id:
            return HttpResponseBadRequest("Empty Doctor ID")
        doctor = Doctor.objects.get(id=doctor_id)
        if not doctor:
            return HttpResponseNotFound("Unable to find doctor")

        user = User(first_name=first_name, last_name=last_name, address=address, gender=gender, birthday=birthday,
                    email=email,
                    password=BCryptPasswordHasher().encode(password=password, salt=BCryptPasswordHasher().salt()),
                    healthcard=healthcard, doctor=doctor)
        user.save()

        return HttpResponse(json.dumps(user.to_dict()), content_type="application/json")


@csrf_exempt
def users_by_id(request, user_id):
    if request.method == "GET":
        user = User.objects.get(id=user_id)
        if not user:
            return HttpResponseNotFound("User not found")
        return HttpResponse(json.dumps(user.to_dict()), content_type="application/json")
    elif request.method == "POST":
        request_params = json.loads(request.body)
        user = User.objects.get(id=user_id)
        if not user:
            return HttpResponseNotFound("Unable to find doctor")
        first_name = request_params.get("first_name", None)
        last_name = request_params.get("last_name", None)
        address = request_params.get("address", None)
        gender = request_params.get("gender", None)
        birthday = request_params.get("birthday", None)
        birthday = datetime.fromtimestamp(birthday)
        email = request_params.get("email", None)
        password = request_params.get("password", None)
        healthcard = request_params.get("healthcard", None)
        doctor_id = request_params.get("doctor_id", None)
        doctor = None
        if doctor_id:
            doctor = Doctor.objects.get(id=doctor_id)
            if not doctor:
                return HttpResponseNotFound("Unable to find doctor")
        if first_name:
            user.first_name = first_name
        if last_name:
            user.last_name = last_name
        if address:
            user.address = address
        if gender:
            user.gender = gender
        if birthday:
            user.birthday = birthday
        if email:
            user.email = email
        if password:
            user.password = BCryptPasswordHasher().encode(password=password, salt=BCryptPasswordHasher().salt())
        if healthcard:
            user.healthcard = healthcard
        if doctor:
            user.doctor = doctor

        user.save()

        return HttpResponse(json.dumps(user.to_dict()), content_type="application/json")


@csrf_exempt
def users_by_email(request, email):
    if request.method == "GET":
        user = User.objects.get(email=email)
        if not user:
            return HttpResponseNotFound("User not found")
        return HttpResponse(json.dumps(user.to_dict()), content_type="application/json")
    elif request.method == "POST":
        request_params = json.loads(request.body)
        user = User.objects.get(email=email)
        if not user:
            return HttpResponseNotFound("Unable to find doctor")
        first_name = request_params.get("first_name", None)
        last_name = request_params.get("last_name", None)
        address = request_params.get("address", None)
        gender = request_params.get("gender", None)
        birthday = request_params.get("birthday", None)
        email = request_params.get("email", None)
        password = request_params.get("password", None)
{'fraction_non_alphanumeric': 0.09989866576591792, 'fraction_numerical': 0.0, 'mean_word_length': 2.894442617560013, 'pattern_counts': {'":': 14, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4061581', 'n_tokens_mistral': 3172, 'n_tokens_neox': 3129, 'n_words': 735}