Skip to content

Fix some django anti-patterns #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/courses/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ def get(self, request, course_id):
if request.user.is_authenticated():
if UserFavorite.objects.filter(user=request.user, fav_id=course.id, fav_type=1):
has_fav_course = True
if UserFavorite.objects.filter(user=request.user, fav_id=course.course_org.id, fav_type=2):
if UserFavorite.objects.filter(
user=request.user, fav_id=course.course_org_id, fav_type=2
):
has_fav_org = True

return render(request, 'course-detail.html', {
Expand Down
2 changes: 1 addition & 1 deletion apps/organization/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def get(self, request, teacher_id):
has_teacher_faved = True

has_org_faved = False
if UserFavorite.objects.filter(user=request.user, fav_type=2, fav_id=teacher.org.id):
if UserFavorite.objects.filter(user=request.user, fav_type=2, fav_id=teacher.org_id):
has_org_faved = True

return render(request, 'teacher-detail.html', {
Expand Down
14 changes: 8 additions & 6 deletions apps/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ def post(self, request):
register_form = RegisterForm(request.POST)
if register_form.is_valid():
email = request.POST.get('email', '')
if UserProfile.objects.filter(email=email):
return render(request, 'register.html', {'register_form': register_form, 'msg': '用户已经存在!'})
if UserProfile.objects.filter(email=email).exists():
return render(
request, 'register.html', {'register_form': register_form, 'msg': '用户已经存在!'}
)
password = request.POST.get('password', '')

user_profile = UserProfile()
Expand All @@ -122,7 +124,7 @@ class ActiveUserView(View):
def get(self, request, active_code):
# 为什么用 filter ? 因为用户可能注册了好多次,一个 email 对应了好多个 code
all_records = EmailVerifyRecord.objects.filter(code=active_code)
if all_records:
if all_records.exists():
for records in all_records:
email = records.email
user = UserProfile.objects.get(email=email)
Expand Down Expand Up @@ -152,7 +154,7 @@ class ResetView(View):
def get(self, request, active_code):
# 如果第二次进来,链接就失效
all_records = EmailVerifyRecord.objects.filter(code=active_code)
if all_records:
if all_records.exists():
for records in all_records:
email = records.email
return render(request, 'password_reset.html', {'email': email})
Expand Down Expand Up @@ -249,7 +251,7 @@ def get(self, request):
email = request.GET.get('email', '')

res = dict()
if UserProfile.objects.filter(email=email):
if UserProfile.objects.filter(email=email).exists():
res['email'] = '邮箱已注册'
return HttpResponse(json.dumps(res), content_type='application/json')
send_register_email(email, 'update_email')
Expand All @@ -266,7 +268,7 @@ def post(self, request):

existed_records = EmailVerifyRecord.objects.filter(email=email, code=code, send_type='update_email')
res = dict()
if existed_records:
if existed_records.exists():
user = request.user
user.email = email
user.save()
Expand Down