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

Add options for enabling/disabling to register and enabling/disabling guest shortening #45

Open
wants to merge 5 commits 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
21 changes: 12 additions & 9 deletions pygmyui/pygmy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
INVALID_CUSTOM_CODE_ERROR = ("Invalid value. Length should be <= 8 and should"
" be a valid alphabat, digit or a mix of both")
VALID_INPUT_CHARS = string.ascii_letters + string.digits

GUEST_ACCESS = settings.GUEST_ACCESS

class URLForm(forms.Form):
long_url = forms.URLField(help_text='Long Link To Shorten',
Expand Down Expand Up @@ -189,16 +189,19 @@ def dashboard(request):

def index(request):
"""Index page"""
response = render(request, 'pygmy/index.html')
if (request.COOKIES.get(AUTH_COOKIE_NAME) and
request.COOKIES.get('refresh_token')):
pygmy_client = pygmy_client_object(settings, request)
access_token = pygmy_client.refresh_access_token()
response.set_cookie(
AUTH_COOKIE_NAME, access_token.get(AUTH_COOKIE_NAME))
access_token = request.COOKIES.get(AUTH_COOKIE_NAME)
if GUEST_ACCESS is True or access_token:
response = render(request, 'pygmy/index.html')
if (request.COOKIES.get(AUTH_COOKIE_NAME) and
request.COOKIES.get('refresh_token')):
pygmy_client = pygmy_client_object(settings, request)
access_token = pygmy_client.refresh_access_token()
response.set_cookie(
AUTH_COOKIE_NAME, access_token.get(AUTH_COOKIE_NAME))
else:
response = render(request, 'pygmy/sorry.html')
return response


def check_available(request):
custom_code = request.GET.get('custom_code')
if not custom_code:
Expand Down
4 changes: 4 additions & 0 deletions pygmyui/pygmyui/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@

HOSTNAME = '127.0.0.1:8000'

GUEST_ACCESS = True

PUBLIC_REGISTER = True

# Load tests if PYGMYUI_TEST environment variable is set
if os.environ.get('PYGMYUI_TEST') is not None:
from pygmyui.settings_tests import * # pylint: disable=W0614, E0611, E0401
4 changes: 3 additions & 1 deletion pygmyui/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
</ul>
<ul class="nav navbar-nav navbar-right">
{% if not request.COOKIES.refresh_token %}
{% if PUBLIC_REGISTER is True %}
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">SIGN UP <span class="caret"></span></a>
<ul class="dropdown-menu dropdown-lr" role="menu">
Expand Down Expand Up @@ -90,6 +91,7 @@ <h3><b>SIGN UP</b></h3>
</div>
</ul>
</li>
{% endif %}
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">LOGIN <span class="caret"></span></a>
<ul class="dropdown-menu dropdown-lr" role="menu">
Expand Down Expand Up @@ -152,4 +154,4 @@ <h3><b>LOGIN</b></h3>
</div>
</div>
</body>
</html>
</html>
5 changes: 5 additions & 0 deletions pygmyui/templates/notallow.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% extends "base.html" %}

{% block content %}
<h3 class="vcenter">Sorry, registration is not allowed by administrator.</h3>
{% endblock content %}
5 changes: 5 additions & 0 deletions pygmyui/templates/pygmy/sorry.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% extends "base.html" %}

{% block content %}
<h3 class="vcenter">Please login to shorten URL</h3>
{% endblock content %}
8 changes: 5 additions & 3 deletions pygmyui/user_auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

AUTH_COOKIE_NAME = settings.AUTH_COOKIE_NAME
REFRESH_COOKIE_NAME = settings.REFRESH_COOKIE_NAME

PUBLIC_REGISTER = settings.PUBLIC_REGISTER

class LoginForm(forms.Form):
email = forms.EmailField(label='Long URL', required=True,
Expand Down Expand Up @@ -86,7 +86,7 @@ def signup(request):
if request.method == 'GET':
return redirect('/')

if request.method == 'POST':
if request.method == 'POST' and PUBLIC_REGISTER is True:
pygmy_client = pygmy_client_object(settings, request)
form = SignUpForm(request.POST)
context = dict(form=form)
Expand All @@ -107,4 +107,6 @@ def signup(request):
k, v, expires=expires) for k, v in context.items()]
# access token lifetime till browser session
response.set_cookie(AUTH_COOKIE_NAME, user_obj['access_token'])
return response
else:
response = render(request, "notallow.html")
return response