Skip to content

Commit

Permalink
Refactor atomic revisions
Browse files Browse the repository at this point in the history
Use `revision.create_revision(atomic=True)` instead of explicity
wrapping with ``transaction.atomic`
  • Loading branch information
kiritofeng authored and quantum5 committed May 8, 2021
1 parent d0fa9a5 commit 1e4765b
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 14 deletions.
3 changes: 1 addition & 2 deletions judge/social_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from django import forms
from django.contrib.auth.models import User
from django.db import transaction
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
Expand Down Expand Up @@ -92,7 +91,7 @@ def make_profile(backend, user, response, is_new=False, *args, **kwargs):
logger.info(data)
form = ProfileForm(data, instance=user.profile, user=user)
if form.is_valid():
with transaction.atomic(), revisions.create_revision():
with revisions.create_revision(atomic=True):
form.save()
revisions.set_user(user)
revisions.set_comment('Updated on registration')
Expand Down
4 changes: 2 additions & 2 deletions judge/views/comment.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
from django.core.exceptions import PermissionDenied
from django.db import IntegrityError, transaction
from django.db import IntegrityError
from django.db.models import F
from django.forms.models import ModelForm
from django.http import Http404, HttpResponse, HttpResponseBadRequest, HttpResponseForbidden
Expand Down Expand Up @@ -125,7 +125,7 @@ class CommentEditAjax(LoginRequiredMixin, CommentMixin, UpdateView):
form_class = CommentEditForm

def form_valid(self, form):
with transaction.atomic(), revisions.create_revision():
with revisions.create_revision(atomic=True):
revisions.set_comment(_('Edited from site'))
revisions.set_user(self.request.user)
return super(CommentEditAjax, self).form_valid(form)
Expand Down
4 changes: 2 additions & 2 deletions judge/views/contests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
from django.core.cache import cache
from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
from django.db import IntegrityError, transaction
from django.db import IntegrityError
from django.db.models import Case, Count, F, FloatField, IntegerField, Max, Min, Q, Sum, Value, When
from django.db.models.expressions import CombinedExpression
from django.http import Http404, HttpResponse, HttpResponseBadRequest, HttpResponseRedirect
Expand Down Expand Up @@ -261,7 +261,7 @@ def form_valid(self, form):
contest.user_count = 0
contest.locked_after = None
contest.key = form.cleaned_data['key']
with transaction.atomic(), revisions.create_revision():
with revisions.create_revision(atomic=True):
contest.save()
contest.tags.set(tags)
contest.organizations.set(organizations)
Expand Down
3 changes: 1 addition & 2 deletions judge/views/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from django.core.cache import cache
from django.core.cache.utils import make_template_fragment_key
from django.core.exceptions import PermissionDenied
from django.db import transaction
from django.db.models import Count, Q
from django.forms import Form, modelformset_factory
from django.http import Http404, HttpResponsePermanentRedirect, HttpResponseRedirect
Expand Down Expand Up @@ -294,7 +293,7 @@ def get_form(self, form_class=None):
return form

def form_valid(self, form):
with transaction.atomic(), revisions.create_revision():
with revisions.create_revision(atomic=True):
revisions.set_comment(_('Edited from site'))
revisions.set_user(self.request.user)
return super(EditOrganization, self).form_valid(form)
Expand Down
2 changes: 1 addition & 1 deletion judge/views/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ def form_valid(self, form):
problem.ac_rate = 0
problem.user_count = 0
problem.code = form.cleaned_data['code']
with transaction.atomic(), revisions.create_revision():
with revisions.create_revision(atomic=True):
problem.save()
problem.authors.add(self.request.profile)
problem.allowed_languages.set(languages)
Expand Down
9 changes: 4 additions & 5 deletions judge/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from django.contrib.contenttypes.models import ContentType
from django.core.cache import cache
from django.core.exceptions import PermissionDenied, ValidationError
from django.db import transaction
from django.db.models import Count, Max, Min
from django.db.models.fields import DateField
from django.db.models.functions import Cast, ExtractYear
Expand Down Expand Up @@ -361,7 +360,7 @@ def edit_profile(request):
if request.method == 'POST':
form = ProfileForm(request.POST, instance=request.profile, user=request.user)
if form.is_valid():
with transaction.atomic(), revisions.create_revision():
with revisions.create_revision(atomic=True):
form.save()
revisions.set_user(request.user)
revisions.set_comment(_('Updated on site'))
Expand Down Expand Up @@ -410,7 +409,7 @@ def edit_profile(request):
@login_required
def generate_api_token(request):
profile = request.profile
with transaction.atomic(), revisions.create_revision():
with revisions.create_revision(atomic=True):
revisions.set_user(request.user)
revisions.set_comment(_('Generated API token for user'))
return JsonResponse({'data': {'token': profile.generate_api_token()}})
Expand All @@ -420,7 +419,7 @@ def generate_api_token(request):
@login_required
def remove_api_token(request):
profile = request.profile
with transaction.atomic(), revisions.create_revision():
with revisions.create_revision(atomic=True):
profile.api_token = None
profile.save()
revisions.set_user(request.user)
Expand All @@ -432,7 +431,7 @@ def remove_api_token(request):
@login_required
def generate_scratch_codes(request):
profile = request.profile
with transaction.atomic(), revisions.create_revision():
with revisions.create_revision(atomic=True):
revisions.set_user(request.user)
revisions.set_comment(_('Generated scratch codes for user'))
return JsonResponse({'data': {'codes': profile.generate_scratch_codes()}})
Expand Down

0 comments on commit 1e4765b

Please sign in to comment.