diff --git a/greenmine/base/auth/api.py b/greenmine/base/auth/api.py index ea9ef562..db6750e6 100644 --- a/greenmine/base/auth/api.py +++ b/greenmine/base/auth/api.py @@ -3,6 +3,7 @@ from django.db.models.loading import get_model from django.contrib.auth import logout, login, authenticate from django.shortcuts import get_object_or_404 +from django.utils.translation import ugettext_lazy as _ from rest_framework.response import Response from rest_framework.permissions import AllowAny @@ -48,7 +49,7 @@ class AuthViewSet(viewsets.ViewSet): def _public_register(self, request): if not request.domain.public_register: - raise exc.BadRequest("Public register is disabled for this domain.") + raise exc.BadRequest(_("Public register is disabled for this domain.")) serializer = PublicRegisterSerializer(data=request.DATA) if not serializer.is_valid(): @@ -87,7 +88,7 @@ class AuthViewSet(viewsets.ViewSet): try: membership = membership_model.objects.get(token=base_serializer.data["token"]) except membership_model.DoesNotExist as e: - raise exc.BadRequest("Invalid token") from e + raise exc.BadRequest(_("Invalid token")) from e if base_serializer.data["existing"]: serializer = PrivateRegisterExistingSerializer(data=request.DATA) @@ -96,7 +97,7 @@ class AuthViewSet(viewsets.ViewSet): user = get_object_or_404(User, username=serializer.data["username"]) if not user.check_password(serializer.data["password"]): - raise exc.BadRequest({"password": "Incorrect password"}) + raise exc.BadRequest({"password": _("Incorrect password")}) else: serializer = PrivateRegisterSerializer(data=request.DATA) @@ -129,7 +130,7 @@ class AuthViewSet(viewsets.ViewSet): elif type == "private": return self._private_register(request) - raise exc.BadRequest("invalid register type") + raise exc.BadRequest(_("invalid register type")) def create(self, request, **kwargs): username = request.DATA.get('username', None) @@ -138,10 +139,10 @@ class AuthViewSet(viewsets.ViewSet): try: user = User.objects.get(username=username) except User.DoesNotExist: - raise exc.BadRequest("Invalid username or password") + raise exc.BadRequest(_("Invalid username or password")) if not user.check_password(password): - raise exc.BadRequest("Invalid username or password") + raise exc.BadRequest(_("Invalid username or password")) response_data = self._create_response(user) return Response(response_data, status=status.HTTP_200_OK) diff --git a/greenmine/base/domains/__init__.py b/greenmine/base/domains/__init__.py index 24623f53..1de10b19 100644 --- a/greenmine/base/domains/__init__.py +++ b/greenmine/base/domains/__init__.py @@ -5,6 +5,7 @@ from threading import local from django.db.models import get_model from django.core.exceptions import ImproperlyConfigured +from django.utils.translation import ugettext_lazy as _ from .. import exceptions as exc @@ -50,7 +51,7 @@ def get_domain_for_domain_name(domain): domain = model_cls.objects.get(domain=domain) except model_cls.DoesNotExist: log.warning("Domain does not exist for domain: {}".format(domain)) - raise DomainNotFound("domain not found") + raise DomainNotFound(_("domain not found")) else: cache[domain] = domain diff --git a/greenmine/base/exceptions.py b/greenmine/base/exceptions.py index eaee687e..da1301b3 100644 --- a/greenmine/base/exceptions.py +++ b/greenmine/base/exceptions.py @@ -5,6 +5,7 @@ from rest_framework import status from rest_framework.response import Response from django.core.exceptions import PermissionDenied as DjangoPermissionDenied +from django.utils.translation import ugettext_lazy as _ from django.http import Http404 from .utils.json import to_json @@ -12,7 +13,7 @@ from .utils.json import to_json class BaseException(exceptions.APIException): status_code = status.HTTP_400_BAD_REQUEST - default_detail = 'Unexpected error' + default_detail = _('Unexpected error') def __init__(self, detail=None): self.detail = detail or self.default_detail @@ -24,7 +25,7 @@ class NotFound(BaseException): """ status_code = status.HTTP_404_NOT_FOUND - default_detail = 'Not found.' + default_detail = _('Not found.') class BadRequest(BaseException): @@ -32,7 +33,7 @@ class BadRequest(BaseException): Exception used on bad arguments detected on api view. """ - default_detail = 'Wrong arguments.' + default_detail = _('Wrong arguments.') class WrongArguments(BaseException): @@ -40,7 +41,7 @@ class WrongArguments(BaseException): Exception used on bad arguments detected on service. This is same as `BadRequest`. """ - default_detail = 'Wrong arguments.' + default_detail = _('Wrong arguments.') class PermissionDenied(exceptions.PermissionDenied): @@ -55,7 +56,7 @@ class PreconditionError(BaseException): """ Error raised on precondition method on viewset. """ - default_detail = "Precondition error" + default_detail = _("Precondition error") class InternalError(BaseException): @@ -63,7 +64,7 @@ class InternalError(BaseException): Exception for internal errors. """ status_code = status.HTTP_500_INTERNAL_SERVER_ERROR - default_detail = "Internal server error" + default_detail = _("Internal server error") class NotAuthenticated(exceptions.NotAuthenticated): @@ -113,11 +114,11 @@ def exception_handler(exc): return Response(detail, status=exc.status_code, headers=headers) elif isinstance(exc, Http404): - return Response({'_error_message': 'Not found'}, + return Response({'_error_message': _('Not found')}, status=status.HTTP_404_NOT_FOUND) elif isinstance(exc, DjangoPermissionDenied): - return Response({'_error_message': 'Permission denied'}, + return Response({'_error_message': _('Permission denied')}, status=status.HTTP_403_FORBIDDEN) # Note: Unhandled exceptions will raise a 500 error. diff --git a/greenmine/base/users/api.py b/greenmine/base/users/api.py index a0c4f63f..159e02d7 100644 --- a/greenmine/base/users/api.py +++ b/greenmine/base/users/api.py @@ -5,6 +5,7 @@ import uuid from django.db.models.loading import get_model from django.db.models import Q from django.contrib.auth import logout, login, authenticate +from django.utils.translation import ugettext_lazy as _ from rest_framework.decorators import list_route, action from rest_framework.response import Response @@ -41,14 +42,14 @@ class UsersViewSet(ModelCrudViewSet): username_or_email = request.DATA.get('username', None) if not username_or_email: - raise exc.WrongArguments("Invalid username or email") + raise exc.WrongArguments(_("Invalid username or email")) try: queryset = User.objects.all() user = queryset.get(Q(username=username_or_email) | Q(email=username_or_email)) except User.DoesNotExist: - raise exc.WrongArguments("Invalid username or email") + raise exc.WrongArguments(_("Invalid username or email")) user.token = str(uuid.uuid1()) user.save(update_fields=["token"]) @@ -57,7 +58,7 @@ class UsersViewSet(ModelCrudViewSet): email = mbuilder.password_recovery(user.email, {"user": user}) email.send() - return Response({"detail": "Mail sended successful!"}) + return Response({"detail": _("Mail sended successful!")}) @list_route(permission_classes=[AllowAny], methods=["POST"]) def change_password_from_recovery(self, request, pk=None): @@ -66,7 +67,7 @@ class UsersViewSet(ModelCrudViewSet): """ serializer = RecoverySerializer(data=request.DATA, many=False) if not serializer.is_valid(): - raise exc.WrongArguments("Token is invalid") + raise exc.WrongArguments(_("Token is invalid")) user = User.objects.get(token=serializer.data["token"]) user.set_password(serializer.data["password"]) @@ -83,10 +84,10 @@ class UsersViewSet(ModelCrudViewSet): password = request.DATA.get("password") if not password: - raise exc.WrongArguments("incomplete argiments") + raise exc.WrongArguments(_("incomplete argiments")) if len(password) < 6: - raise exc.WrongArguments("invalid password length") + raise exc.WrongArguments(_("invalid password length")) request.user.set_password(password) request.user.save(update_fields=["password"]) diff --git a/greenmine/base/users/serializers.py b/greenmine/base/users/serializers.py index b22d5aec..5311c52e 100644 --- a/greenmine/base/users/serializers.py +++ b/greenmine/base/users/serializers.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- +from django.utils.translation import ugettext_lazy as _ + from rest_framework import serializers from .models import User, Role @@ -28,7 +30,7 @@ class RecoverySerializer(serializers.Serializer): try: user = User.objects.get(token=token) except User.DoesNotExist: - raise serializers.ValidationError("invalid token") + raise serializers.ValidationError(_("invalid token")) return attrs diff --git a/greenmine/projects/api.py b/greenmine/projects/api.py index 85e636de..7551737f 100644 --- a/greenmine/projects/api.py +++ b/greenmine/projects/api.py @@ -3,6 +3,7 @@ import uuid from django.db.models import Q +from django.utils.translation import ugettext_lazy as _ from rest_framework.permissions import IsAuthenticated, AllowAny from rest_framework.response import Response @@ -85,7 +86,7 @@ class MembershipViewSet(ModelCrudViewSet): Q(project_id=serializer.data["project"], email=serializer.data["email"])) if qs.count() > 0: - raise exc.WrongArguments("Already exist user with specified email address.") + raise exc.WrongArguments(_("Already exist user with specified email address.")) self.pre_save(serializer.object) self.object = serializer.save(force_insert=True) diff --git a/greenmine/projects/issues/api.py b/greenmine/projects/issues/api.py index 4a0dfafe..04034204 100644 --- a/greenmine/projects/issues/api.py +++ b/greenmine/projects/issues/api.py @@ -2,6 +2,7 @@ import reversion from django.contrib.contenttypes.models import ContentType +from django.utils.translation import ugettext_lazy as _ from rest_framework.permissions import IsAuthenticated from rest_framework.decorators import list_route @@ -78,22 +79,22 @@ class IssueViewSet(NotificationSenderMixin, ModelCrudViewSet): if (obj.project.owner != self.request.user and obj.project.memberships.filter(user=self.request.user).count() == 0): - raise exc.PreconditionError("You must not add a new issue to this project.") + raise exc.PreconditionError(_("You must not add a new issue to this project.")) if obj.milestone and obj.milestone.project != obj.project: - raise exc.PreconditionError("You must not add a new issue to this milestone.") + raise exc.PreconditionError(_("You must not add a new issue to this milestone.")) if obj.status and obj.status.project != obj.project: - raise exc.PreconditionError("You must not use a status from other project.") + raise exc.PreconditionError(_("You must not use a status from other project.")) if obj.severity and obj.severity.project != obj.project: - raise exc.PreconditionError("You must not use a severity from other project.") + raise exc.PreconditionError(_("You must not use a severity from other project.")) if obj.priority and obj.priority.project != obj.project: - raise exc.PreconditionError("You must not use a priority from other project.") + raise exc.PreconditionError(_("You must not use a priority from other project.")) if obj.type and obj.type.project != obj.project: - raise exc.PreconditionError("You must not use a type from other project.") + raise exc.PreconditionError(_("You must not use a type from other project.")) def post_save(self, obj, created=False): with reversion.create_revision(): @@ -127,5 +128,4 @@ class IssueAttachmentViewSet(ModelCrudViewSet): if (obj.project.owner != self.request.user and obj.project.memberships.filter(user=self.request.user).count() == 0): - raise exc.PreconditionError("You must not add a new issue attachment " - "to this project.") + raise exc.PreconditionError(_("You must not add a new issue attachment to this project.")) diff --git a/greenmine/projects/milestones/api.py b/greenmine/projects/milestones/api.py index 40048b27..5c96d36f 100644 --- a/greenmine/projects/milestones/api.py +++ b/greenmine/projects/milestones/api.py @@ -34,7 +34,7 @@ class MilestoneViewSet(NotificationSenderMixin, ModelCrudViewSet): if (obj.project.owner != self.request.user and obj.project.memberships.filter(user=self.request.user).count() == 0): - raise exc.PreconditionError("You must not add a new milestone to this project.") + raise exc.PreconditionError(_("You must not add a new milestone to this project.")) def pre_save(self, obj): if not obj.id: diff --git a/greenmine/projects/tasks/api.py b/greenmine/projects/tasks/api.py index e6dc6524..5cdf4e30 100644 --- a/greenmine/projects/tasks/api.py +++ b/greenmine/projects/tasks/api.py @@ -44,8 +44,7 @@ class TaskAttachmentViewSet(ModelCrudViewSet): if (obj.project.owner != self.request.user and obj.project.memberships.filter(user=self.request.user).count() == 0): - raise exc.PreconditionError("You must not add a new task attachment " - "to this project.") + raise exc.PreconditionError(_("You must not add a new task attachment to this project.")) @@ -72,16 +71,16 @@ class TaskViewSet(NotificationSenderMixin, ModelCrudViewSet): if (obj.project.owner != self.request.user and obj.project.memberships.filter(user=self.request.user).count() == 0): - raise exc.PreconditionError("You must not add a new task to this project.") + raise exc.PreconditionError(_("You must not add a new task to this project.")) if obj.milestone and obj.milestone.project != obj.project: - raise exc.PreconditionError("You must not add a task to this milestone.") + raise exc.PreconditionError(_("You must not add a task to this milestone.")) if obj.user_story and obj.user_story.project != obj.project: - raise exc.PreconditionError("You must not add a task to this user story.") + raise exc.PreconditionError(_("You must not add a task to this user story.")) if obj.status and obj.status.project != obj.project: - raise exc.PreconditionError("You must not use a status from other project.") + raise exc.PreconditionError(_("You must not use a status from other project.")) def post_save(self, obj, created=False): with reversion.create_revision(): diff --git a/greenmine/projects/userstories/api.py b/greenmine/projects/userstories/api.py index d2754ddf..32ca2faf 100644 --- a/greenmine/projects/userstories/api.py +++ b/greenmine/projects/userstories/api.py @@ -52,8 +52,7 @@ class UserStoryAttachmentViewSet(ModelCrudViewSet): if (obj.project.owner != self.request.user and obj.project.memberships.filter(user=self.request.user).count() == 0): - raise exc.PreconditionError("You must not add a new user story attachment " - "to this project.") + raise exc.PreconditionError(_("You must not add a new user story attachment to this project.")) class UserStoryViewSet(NotificationSenderMixin, ModelCrudViewSet): @@ -72,16 +71,16 @@ class UserStoryViewSet(NotificationSenderMixin, ModelCrudViewSet): def bulk_create(self, request, **kwargs): bulk_stories = request.DATA.get('bulkStories', None) if bulk_stories is None: - raise exc.BadRequest(detail='You need bulkStories data') + raise exc.BadRequest(detail=_('You need bulkStories data')) project_id = request.DATA.get('projectId', None) if project_id is None: - raise exc.BadRequest(detail='You need projectId data') + raise exc.BadRequest(detail=_('You need projectId data')) project = get_object_or_404(Project, id=project_id) if not has_project_perm(request.user, project, 'add_userstory'): - raise exc.PermissionDenied("You don't have permision to create user stories") + raise exc.PermissionDenied(_("You don't have permision to create user stories")) service = services.UserStoriesService() service.bulk_insert(project, request.user, bulk_stories) @@ -123,15 +122,13 @@ class UserStoryViewSet(NotificationSenderMixin, ModelCrudViewSet): if (obj.project.owner != self.request.user and obj.project.memberships.filter(user=self.request.user).count() == 0): - raise exc.PreconditionError("You must not add a new user story to this " - "project.") + raise exc.PreconditionError(_("You must not add a new user story to this project.")) if obj.milestone and obj.milestone.project != obj.project: - raise exc.PreconditionError("You must not add a new user story to this " - "milestone.") + raise exc.PreconditionError(_("You must not add a new user story to this milestone.")) if obj.status and obj.status.project != obj.project: - raise exc.PreconditionError("You must not use a status from other project.") + raise exc.PreconditionError(_("You must not use a status from other project.")) def post_save(self, obj, created=False): with reversion.create_revision(): diff --git a/greenmine/projects/wiki/api.py b/greenmine/projects/wiki/api.py index d46bc227..cbb2dc55 100644 --- a/greenmine/projects/wiki/api.py +++ b/greenmine/projects/wiki/api.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from django.contrib.contenttypes.models import ContentType +from django.utils.translation import ugettext_lazy as _ from rest_framework.permissions import IsAuthenticated @@ -35,8 +36,7 @@ class WikiAttachmentViewSet(ModelCrudViewSet): if (obj.project.owner != self.request.user and obj.project.memberships.filter(user=self.request.user).count() == 0): - raise exc.PreconditionError("You must not add a new wiki page to this " - "project.") + raise exc.PreconditionError(_("You must not add a new wiki page to this project.")) def pre_save(self, obj): if not obj.id: @@ -61,8 +61,7 @@ class WikiViewSet(ModelCrudViewSet): if (obj.project.owner != self.request.user and obj.project.memberships.filter(user=self.request.user).count() == 0): - raise exc.PreconditionError("You must not add a new wiki page to this " - "project.") + raise exc.PreconditionError(_("You must not add a new wiki page to this project.")) def pre_save(self, obj): if not obj.owner: