# Path: sitegate/decorators.py
# def sitegate_view(*args_dec, **kwargs_dec):
#     """Decorator to mark views used both for signup & sign in."""
#     if len(args_dec):  # simple decoration w/o parameters
#         return signup_view(signin_view(redirect_signedin(*args_dec, **kwargs_dec)))
# 
#     signin = signin_view(**kwargs_dec)
#     signup = signup_view(**kwargs_dec)
# 
#     return lambda *args, **kwargs: signup(signin(redirect_signedin(*args, **kwargs)))
# 
# def signin_view(*args, **kwargs):
#     """Decorator to mark views used for sign in."""
#     return FlowBuilder(ModernSignin, args, kwargs)
# 
# def signup_view(*args, **kwargs):
#     """Decorator to mark views used for signup."""
#     return FlowBuilder(ModernSignup, args, kwargs)
# 
# def redirect_signedin(*args, **kwargs):
#     """Decorator to mark views which should not be accessed by signed in users.
#     Example: sign in & sign up pages.
# 
#     """
#     return RedirectBuilder(args, kwargs)
# 
# Path: sitegate/views.py
# def verify_email(request: HttpRequest, code: str, redirect_to: str = None) -> HttpResponse:
#     """Verifies an account activation code a user received by e-mail.
# 
#     Requires Messages Django Contrib.
# 
#     :param request:
#     :param code:
#     :param redirect_to:
# 
#     """
#     success = False
# 
#     valid_code = EmailConfirmation.is_valid(code)
#     if valid_code:
#         valid_code.activate()
#         success = True
# 
#     if success:
#         messages.success(request, SIGNUP_VERIFY_EMAIL_SUCCESS_TEXT, 'success')
#     else:
#         messages.error(request, SIGNUP_VERIFY_EMAIL_ERROR_TEXT, 'danger error')
# 
#     if redirect_to is None:
#         redirect_to = '/'
# 
#     return redirect(redirect_to)
# 
# @requires_csrf_token
# def remote_auth(request: HttpRequest, alias: str) -> HttpResponse:
#     """Performs an authorization using data from a remote.
#     
#     For a user which is anonymous or not yet linked to target remote.
# 
#     :param request:
#     :param alias: remote service alias
# 
#     """
#     remote = get_registered_remotes().get(alias)
# 
#     if not remote:
#         return HttpResponseBadRequest()
# 
#     if request.method == 'POST':
#         data = request.POST.dict()
#         code = remote.get_code_from_data(data)
# 
#         if not code:
#             return remote.redirect()
# 
#         manager = RemoteRecord.objects
# 
#         remote_record = manager.filter(
#             code=code,
#             remote=alias,
#         ).select_related('user').first()
# 
#         if not remote_record:
#             return remote.redirect()
# 
#         user = remote_record.user
#         request_user = request.user
#         request_user = None if request_user.is_anonymous else request_user
# 
#         if user != request_user:
#             # in case someone tries to mess with us
#             return remote.redirect()
# 
#         user_data = remote.get_user_data(request, data=data)
# 
#         if not user_data:
#             return remote.redirect()
# 
#         # try to find previous records for this remote_id
#         previous_record = manager.filter(
#             remote=alias,
#             remote_id=user_data.remote_id,
#         ).select_related('user').first()
# 
#         if previous_record:
#             user = previous_record.user
#             remote_record = previous_record
# 
#         return remote.auth_finish(
#             request,
#             user_data=user_data,
#             remote_record=remote_record,
#             user=user,
#         )
# 
#     # this is GET
#     return remote.auth_continue(request)
# 
# def remote_auth_start(request: HttpRequest, alias: str) -> HttpResponse:
#     """Performs a redirect to another service for remote auth.
# 
#     :param request:
#     :param alias: remote service alias
# 
#     """
#     if not request.method == 'POST':
#         return HttpResponseNotAllowed(['post'])
# 
#     remote = get_registered_remotes().get(alias)
# 
#     if not remote:
#         return HttpResponseBadRequest()
# 
#     user = request.user
# 
#     if user.is_anonymous:
#         user = None
# 
#     else:
#         if RemoteRecord.objects.filter(user=user, remote=alias).exists():
#             # local user profile is already signe in and linked to this remote
#             return remote.redirect()
# 
#     # create a record bound to an unregistered or unlinked user
#     record = RemoteRecord.add(
#         remote=alias,
#         user=user,
#     )
# 
#     return remote.auth_start(request, ticket=record.code)
# 
# Path: sitegate/utils.py
# def register_remotes(*remotes: 'Remote'):
#     """Registers (configures) remotes.
# 
#     :param remotes: Remote heirs instances.
# 
#     """
#     global _REMOTES_REGISTRY
# 
#     for remote in remotes:
#         _REMOTES_REGISTRY[remote.alias] = remote


from django.urls import re_path
from .decorators import sitegate_view, signin_view, signup_view, redirect_signedin  # noqa
from .views import verify_email, remote_auth, remote_auth_start
from .utils import register_remotes  # noqa



def get_sitegate_urls() -> list:
    """Returns sitegate urlpatterns, that can be attached
    to urlpatterns of a project:

        # Example from urls.py.

        from sitegate.toolbox import get_sitegate_urls

        urlpatterns = patterns('',
            ...
            path('login/', 'apps.views.login', name='login'),
            ...
        ) + get_sitegate_urls()  # Attach.

    """
    urls = [

# Path: sitegate/decorators.py
# def sitegate_view(*args_dec, **kwargs_dec):
#     """Decorator to mark views used both for signup & sign in."""
#     if len(args_dec):  # simple decoration w/o parameters
#         return signup_view(signin_view(redirect_signedin(*args_dec, **kwargs_dec)))
# 
#     signin = signin_view(**kwargs_dec)
#     signup = signup_view(**kwargs_dec)
# 
#     return lambda *args, **kwargs: signup(signin(redirect_signedin(*args, **kwargs)))
# 
# def signin_view(*args, **kwargs):
#     """Decorator to mark views used for sign in."""
#     return FlowBuilder(ModernSignin, args, kwargs)
# 
# def signup_view(*args, **kwargs):
#     """Decorator to mark views used for signup."""
#     return FlowBuilder(ModernSignup, args, kwargs)
# 
# def redirect_signedin(*args, **kwargs):
#     """Decorator to mark views which should not be accessed by signed in users.
#     Example: sign in & sign up pages.
# 
#     """
#     return RedirectBuilder(args, kwargs)
# 
# Path: sitegate/views.py
# def verify_email(request: HttpRequest, code: str, redirect_to: str = None) -> HttpResponse:
#     """Verifies an account activation code a user received by e-mail.
# 
#     Requires Messages Django Contrib.
# 
#     :param request:
#     :param code:
#     :param redirect_to:
# 
#     """
#     success = False
# 
#     valid_code = EmailConfirmation.is_valid(code)
#     if valid_code:
#         valid_code.activate()
#         success = True
# 
#     if success:
#         messages.success(request, SIGNUP_VERIFY_EMAIL_SUCCESS_TEXT, 'success')
#     else:
#         messages.error(request, SIGNUP_VERIFY_EMAIL_ERROR_TEXT, 'danger error')
# 
#     if redirect_to is None:
#         redirect_to = '/'
# 
#     return redirect(redirect_to)
# 
# @requires_csrf_token
# def remote_auth(request: HttpRequest, alias: str) -> HttpResponse:
#     """Performs an authorization using data from a remote.
#     
#     For a user which is anonymous or not yet linked to target remote.
# 
#     :param request:
#     :param alias: remote service alias
# 
#     """
#     remote = get_registered_remotes().get(alias)
# 
#     if not remote:
#         return HttpResponseBadRequest()
# 
#     if request.method == 'POST':
#         data = request.POST.dict()
#         code = remote.get_code_from_data(data)
# 
#         if not code:
#             return remote.redirect()
# 
#         manager = RemoteRecord.objects
# 
#         remote_record = manager.filter(
#             code=code,
#             remote=alias,
#         ).select_related('user').first()
# 
#         if not remote_record:
#             return remote.redirect()
# 
#         user = remote_record.user
#         request_user = request.user
#         request_user = None if request_user.is_anonymous else request_user
# 
#         if user != request_user:
#             # in case someone tries to mess with us
#             return remote.redirect()
# 
#         user_data = remote.get_user_data(request, data=data)
# 
#         if not user_data:
#             return remote.redirect()
# 
#         # try to find previous records for this remote_id
#         previous_record = manager.filter(
#             remote=alias,
#             remote_id=user_data.remote_id,
#         ).select_related('user').first()
# 
#         if previous_record:
#             user = previous_record.user
#             remote_record = previous_record
# 
#         return remote.auth_finish(
#             request,
#             user_data=user_data,
#             remote_record=remote_record,
#             user=user,
#         )
# 
#     # this is GET
#     return remote.auth_continue(request)
# 
# def remote_auth_start(request: HttpRequest, alias: str) -> HttpResponse:
#     """Performs a redirect to another service for remote auth.
# 
#     :param request:
#     :param alias: remote service alias
# 
#     """
#     if not request.method == 'POST':
#         return HttpResponseNotAllowed(['post'])
# 
#     remote = get_registered_remotes().get(alias)
# 
#     if not remote:
#         return HttpResponseBadRequest()
# 
#     user = request.user
# 
#     if user.is_anonymous:
#         user = None
# 
#     else:
#         if RemoteRecord.objects.filter(user=user, remote=alias).exists():
#             # local user profile is already signe in and linked to this remote
#             return remote.redirect()
# 
#     # create a record bound to an unregistered or unlinked user
#     record = RemoteRecord.add(
#         remote=alias,
#         user=user,
#     )
# 
#     return remote.auth_start(request, ticket=record.code)
# 
# Path: sitegate/utils.py
# def register_remotes(*remotes: 'Remote'):
#     """Registers (configures) remotes.
# 
#     :param remotes: Remote heirs instances.
# 
#     """
#     global _REMOTES_REGISTRY
# 
#     for remote in remotes:
#         _REMOTES_REGISTRY[remote.alias] = remote


# Path: sitegate/toolbox.py
from django.urls import re_path
from .decorators import sitegate_view, signin_view, signup_view, redirect_signedin  # noqa
from .views import verify_email, remote_auth, remote_auth_start
from .utils import register_remotes  # noqa




def get_sitegate_urls() -> list:
    """Returns sitegate urlpatterns, that can be attached
    to urlpatterns of a project:

        # Example from urls.py.

        from sitegate.toolbox import get_sitegate_urls

        urlpatterns = patterns('',
            ...
            path('login/', 'apps.views.login', name='login'),
            ...
        ) + get_sitegate_urls()  # Attach.

    """
    urls = [

        re_path(r'^verify_email/(?P<code>\S+)/$', verify_email, name='verify_email'),