코드리뷰[3]

BackEnd_Ash.log·2020년 3월 21일
0

카테고리 부분

class CategoryView(View):
    def get(self, request):
        category_list = [
            {       'main_category'         : main.name,
                    'icon_black_url'        : main.icon_black_url,
                    'icon_active_url'       : main.icon_active_url,
                    'subcategory'           : [{
                        'name'          : sub.name, 
                        'thumbnail_url' : sub.thumbnail_url
                    } for sub in SubCategory.objects.filter(maincategory = main.id).all()] 
                    }
            for main in MainCategory.objects.all()
        ]
        return JsonResponse({"data" : list(category_list)}, status = 200)
        

카테고리 view 부분이다 .
딱히 코드 수정이 없을것 같다.
수정을 해봐야 .. SubCateogry 테이블을 반복문 안에 넣지말고 ,
가독성을 위해 밖으로 빼는것이 더 좋을수도 있겠다는점 ??

회원가입


class SignUpView(View):
    def check_capital_area(self, area):
        for capital in ['서울', '경기', '인천']:
            if capital in area:
                return True
        return False

    def invalid_password(self, password):
        pattern1 = r"^(?=.*[\d])(?=.*[A-Za-z])(?=.*[!@#$%^&*()_+={}?:~\[\]])[A-Za-z\d!@#$%^&*()_+={}?:~\[\]]{10,}$"
        pattern2 = r"^(?=.*[\d])(?=.*[A-Za-z])[A-Za-z\d]{10,}$"
        pattern3 = r"^(?=.*[\d])(?=.*[!@#$%^&*()_+={}?:~\[\]])[\d!@#$%^&*()_+={}?:~\[\]]{10,}$"
        pattern4 = r"^(?=.*[A-Za-z])(?=.*[!@#$%^&*()_+={}?:~\[\]])[A-Za-z!@#$%^&*()_+={}?:~\[\]]{10,}$"

        if re.match(pattern1, password) or re.match(pattern2, password) or re.match(pattern3, password) \
           or re.match(pattern4, password):
            if re.search(r"^(\d)(\1{2})", password):
                return True
            return False
        
        return True


    def invalid_account(self, account):
        pattern1 = r"^(?=.*[\d])(?=.*[A-Za-z])[A-Za-z\d]{6,}$"
        pattern2 = r"^(?=.*[A-Za-z])[A-Za-z]{6,}$"

        if re.match(pattern1, account) or re.match(pattern2, account):
            return False
        return True

    def invalid_phone(self, phone):
        if re.match(r"^\d{3}?\d{4}?\d{4}$", phone):
            return False
        return True


    def post(self, request):
        user_data = json.loads(request.body)
        
        try:
            if User.objects.filter(account=user_data['account']).exists():
                return HttpResponse(status=400)
           
            if self.invalid_account(user_data['account']):
                return HttpResponse(status=400)

            validate_email(user_data['email'])

            if self.invalid_password(user_data['password']):
                return HttpResponse(status=400)

            if self.invalid_phone(user_data['phone']):
                return HttpResponse(status=400)
            
            if user_data['name'] is None:
                return HttpResponse(status=400)

            with transaction.atomic():
                password = bcrypt.hashpw(user_data['password'].encode('utf-8'), bcrypt.gensalt())

                user_model = User(
                    account=user_data['account'],
                    grade=Grade.objects.get(id=1),
                    password=password.decode(),
                    name=user_data['name'],
                    email=user_data['email'],
                    phone=user_data['phone'],
                    gender=Gender.objects.get(name=user_data['gender']),
                    birthday=user_data['birthday']
                )

                user_model.save()

                # capital area check
                Address(
                    user=User.objects.get(id=user_model.id),
                    address=user_data['address'],
                    is_capital_area=self.check_capital_area(user_data['address'])
                ).save()

                return HttpResponse(status=200)

        except KeyError:
            return HttpResponse(status=400)

        except ValidationError:
            return HttpResponse(status=400)

에서 ,

class SignUpView(View):
    def check_capital_area(self, area):
        return area in {'서울', '경기', '인천'} 

    def invalid_password(self, password):
        numer_alphabetic = r"^(?=.*[\d])(?=.*[A-Za-z])(?=.*[!@#$%^&*()_+={}?:~\[\]])[A-Za-z\d!@#$%^&*()_+={}?:~\[\]]{10,}$"
        pattern2 = r"^(?=.*[\d])(?=.*[A-Za-z])[A-Za-z\d]{10,}$"
        pattern3 = r"^(?=.*[\d])(?=.*[!@#$%^&*()_+={}?:~\[\]])[\d!@#$%^&*()_+={}?:~\[\]]{10,}$"
        pattern4 = r"^(?=.*[A-Za-z])(?=.*[!@#$%^&*()_+={}?:~\[\]])[A-Za-z!@#$%^&*()_+={}?:~\[\]]{10,}$"

        repeated_lass_than_twice = re.search(r"^(\d)(\1{2})", password) 
        password_validation      = (
            re.match(numer_alphabetic, password) or 
            re.match(pattern2, password) or 
            re.match(pattern3, password) or
            re.match(pattern4, password)
        )

        return password_validation and repeated_less_than_twice:
        
    def invalid_account(self, account):
        pattern1 = r"^(?=.*[\d])(?=.*[A-Za-z])[A-Za-z\d]{6,}$"
        pattern2 = r"^(?=.*[A-Za-z])[A-Za-z]{6,}$"

        return not (re.match(pattern1, account) or re.match(pattern2, account))

    def invalid_phone(self, phone):
        if re.match(r"^\d{3}?\d{4}?\d{4}$", phone):
            return False
        return True

    
    VALIDATION_RULES = {
        'account'  : self.validate_account,
        'password' : self.validate_password,
        'phone'    : self.validate_phone,
		'age'	   : self.vallidate_age
    }

    def post(self, request):
        user_data = json.loads(request.body)
        
        try:
            if User.objects.filter(account=user_data['account']).exists():
                return HttpResponse(status=400)

            validate_email(user_data['email'])

			for field, validator in VALIDATION_RULES.items():
				if validator(data[field]):
                	return HttpResponse(status=400)

            with transaction.atomic():
                password   = bcrypt.hashpw(user_data['password'].encode('utf-8'), bcrypt.gensalt())
                user_model = User(
                    account  = user_data['account'],
                    grade    = Grade.objects.get(id    = 1),
                    password = password.decode(),
                    name     = user_data['name'],
                    email    = user_data['email'],
                    phone    = user_data['phone'],
                    gender   = Gender.objects.get(name = user_data['gender']),
                    birthday = user_data['birthday']
                )
                user_model.save()

                # capital area check
                Address(
                    user            = User.objects.get(id = user_model.id),
                    address         = user_data['address'],
                    is_capital_area = self.check_capital_area(user_data['address'])
                ).save()

                return HttpResponse(status=200)
        except KeyError:
            return HttpResponse(status=400)
        except ValidationError:
            return HttpResponse(status=400)
         
profile
꾸준함이란 ... ?

0개의 댓글