Skip to content
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

Improve performance of updating with using model.save(update_fields=[...]) for Profile and User models #77

Open
wants to merge 2 commits into
base: develop
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: 2 additions & 2 deletions authnz/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ class UserAdmin(admin.ModelAdmin):
def calculate_review_rate(self, request, queryset):
for user in queryset.all():
user.profile.total_review, user.profile.rate_avg = handle_user_total_rate(user)
user.save()
user.profile.save(update_fields=["total_review", "rate_avg"])

calculate_review_rate.short_description = 'Calculate user total review rate'
calculate_review_rate.allowed_permissions = ('change',)

def change_jwt(self, request, queryset):
for user in queryset.all():
user.profile.jwt_secret = uuid_str()
user.save()
user.profile.save(update_fields=["jwt_secret"])

change_jwt.short_description = 'Change user jwt'
change_jwt.allowed_permissions = ('change',)
Expand Down
10 changes: 3 additions & 7 deletions authnz/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ def create_user_profile(sender, instance, created, **kwargs):
Profile.objects.create(user=instance)
cache.delete(settings.TOTAL_USER)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()

def hash_user_data(self):
if self.email:
self.email = md5(self.email.encode()).hexdigest()[:30]
Expand All @@ -59,6 +55,6 @@ def hash_user_data(self):
if self.user.email:
self.user.email = md5(self.user.email.encode()).hexdigest()[:30]
self.user.username = md5(self.user.username.encode()).hexdigest()[:30]
self.save()
self.user.profile.jwt_secret = uuid_str()
self.user.save()
self.jwt_secret = uuid_str()
self.user.save(update_fields=["first_name", "last_name", "email", "username"])
self.save(update_fields=["email", "mobile", "jwt_secret"])
3 changes: 2 additions & 1 deletion authnz/serialziers.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ def update(self, instance, validated_data):
instance.last_name = validated_data.get('last_name', instance.last_name)
instance.profile.profile_image = validated_data.get('profile_image', instance.profile.profile_image)
instance.profile.biography = validated_data.get('biography', instance.profile.biography)
instance.save()
instance.save(update_fields=["first_name", "last_name"])
instance.profile.save(update_fields=["nick_name", "profile_image", "biography"])
return instance

def to_representation(self, instance):
Expand Down
7 changes: 4 additions & 3 deletions authnz/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ def register_user_with_email_and_password(email, password):
user.set_password(password)
user.save()
user.profile.email = email
user.save()
user.profile.save(update_fields=["email"])
return user


@transaction.atomic
def change_user_password(user, password):
user.set_password(password)
user.profile.jwt_secret = utilities.uuid_str()
user.save()
user.save(update_fields=["password"])
user.profile.save(update_fields=["jwt_secret"])


@transaction.atomic
Expand All @@ -29,5 +30,5 @@ def open_auth_user_creator(email, first_name, last_name, profile_image):
user.profile.profile_image = utilities.file_uploader(user.profile.nick_name, profile_image)
user.profile.email = email
user.profile.email_confirmed = True
user.save()
user.profile.save(update_fields=["profile_image", "email", "email_confirmed"])
return user
6 changes: 3 additions & 3 deletions authnz/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def post(self, request):
elif user:
permissions.check_send_email_permission(email)
user.set_password(serialized_data.data['password'])
user.save()
user.save(update_fields=["password"])
utilities.send_email_confirm(user, request)
return responses.SuccessResponse().send()
else:
Expand Down Expand Up @@ -237,7 +237,7 @@ def get(self, request, uidb64, token):

if user is not None and utilities.account_activation_token.check_token(user, token):
user.profile.email_confirmed = True
user.profile.save()
user.profile.save(update_fields=["email_confirmed"])
return render(request, 'mail_confirmed.html', {'domain': get_current_site(request).domain,
'name': user.username})
else:
Expand Down Expand Up @@ -426,7 +426,7 @@ def post(self, request, backend, *args, **kwargs):
else:
request.user.profile.email = email
request.user.profile.email_confirmed = True
request.user.save()
request.user.profile.save(update_fields=["email", "email_confirmed"])
else:
raise authnz_exceptions.CustomException(detail=_('Wrong backend'))
return responses.SuccessResponse().send()
Expand Down
4 changes: 2 additions & 2 deletions review/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def save(self, *args, **kwargs):
cache.delete(settings.TOTAL_REVIEW)
super().save(*args, **kwargs)
self.creator.profile.total_review, self.creator.profile.rate_avg = handle_user_total_rate(self.creator)
self.creator.save()
self.creator.profile.save(update_fields=["total_review", "rate_avg"])


class Interview(models.Model):
Expand Down Expand Up @@ -160,7 +160,7 @@ def save(self, *args, **kwargs):
cache.delete(settings.TOTAL_INTERVIEW)
super().save(*args, **kwargs)
self.creator.profile.total_review, self.creator.profile.rate_avg = handle_user_total_rate(self.creator)
self.creator.save()
self.creator.profile.save(update_fields=["total_review", "rate_avg"])


class ReviewComment(models.Model):
Expand Down
2 changes: 1 addition & 1 deletion utilities/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def create_data(main_url):
user = User.objects.last()
user.is_staff = True
user.is_superuser = True
user.save()
user.save(update_fields=["is_staff", "is_superuser"])
ADMIN_HEADER = {
'Content-Type': 'application/json',
'Authorization': 'JWT ' + resp.json()['data']['token']
Expand Down