Skip to content
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
Binary file modified db.sqlite3
Binary file not shown.
66 changes: 57 additions & 9 deletions mainapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
from PIL import Image
import base64
from css_html_js_minify import js_minify,process_single_js_file
# Create your views here.
# Create your views here.''
def home(request):
tools = models.Tool.objects.all()
categories = models.Tool.objects.order_by().values_list('category',flat=True).distinct()
context={'tools':tools,'categories':categories}
print(settings.MEDIA_URL)
return render(request,'general/home.html',context)

def tool(request,tool_name):
Expand All @@ -29,7 +28,6 @@ def user_profile(request,user_name):
def tags(request,tag_name):
tag = get_object_or_404(models.Tag,tag=tag_name)
tools = tag.tool_set.all()
print(tools)
return render(request,'general/tags.html',{'tools':tools})


Expand Down Expand Up @@ -59,7 +57,6 @@ def convert_file(request):
output = pypandoc.convert_file(input_file_path,convert_to,outputfile=output_file_path)

if os.path.exists(output_file_path):
print('exists')
with open(output_file_path, 'rb+') as fh:
response = HttpResponse(fh.read(), content_type="application/force-download")
response['Content-Disposition'] = 'inline; filename=' + os.path.basename(output_file_path)
Expand All @@ -80,11 +77,9 @@ def JpgToPng(request):
path = default_storage.save(input_file_path,ContentFile(file_to_convert.read()))
im = Image.open(input_file_path)
im.save(output_file_path)
png_img = Image.open(output_file_path)
print("saved file: ", png_img)
png_img = Image.open(output_file_path)

if os.path.exists(output_file_path):
print('exists')
with open(output_file_path, 'rb+') as fh:
response = HttpResponse(fh.read(), content_type="application/force-download")
response['Content-Disposition'] = 'inline; filename=' + os.path.basename(output_file_path)
Expand Down Expand Up @@ -114,7 +109,6 @@ def download_minified_file(request):
input_file_path = os.path.join(settings.MEDIA_ROOT,'files',request.FILES.get("file").name)
path = default_storage.save(input_file_path,ContentFile(request.FILES.get("file").read()))
z = process_single_js_file(input_file_path,overwrite=False)
print(z);
with open(z, 'rb+') as fh:
res = HttpResponse(fh.read(),content_type="application/js")
res['Content-Disposition'] = 'attachment; filename='+ os.path.basename(z)
Expand All @@ -126,7 +120,6 @@ def download_minified_file(request):
#sample download tool starts
def about_sample_file(request,format):
name = "sample."+format
print("i am working for ",name)
input_file_path = os.path.join(settings.MEDIA_ROOT,'sample',name)
with open(input_file_path, 'rb+') as fh:
size = os.path.getsize(input_file_path)/1024
Expand All @@ -146,3 +139,58 @@ def download_sample_file(request,format):
response['Content-Disposition'] = 'inline; filename=' + os.path.basename(input_file_path)
return response
return HttpResponse('Error while converting', status=404)



# authentication with discourse sso
import base64
import hmac
import hashlib
from urllib import parse

from django.contrib.auth.decorators import login_required
from django.http import HttpResponseBadRequest, HttpResponseRedirect
from django.conf import settings

@login_required
def sso(request):
payload = request.GET.get('sso')
signature = request.GET.get('sig')

if payload is None or signature is None:
return HttpResponseBadRequest('No SSO payload or signature. Please contact support if this problem persists.')

## Validate the payload

try:
payload = bytes(parse.unquote(payload), encoding='utf-8')
decoded = base64.decodestring(payload).decode('utf-8')
assert 'nonce' in decoded
assert len(payload) > 0
except AssertionError:
return HttpResponseBadRequest('Invalid payload. Please contact support if this problem persists.')

key = bytes(settings.DISCOURSE_SSO_SECRET, encoding='utf-8') # must not be unicode
h = hmac.new(key, payload, digestmod=hashlib.sha256)
this_signature = h.hexdigest()

if not hmac.compare_digest(this_signature, signature):
return HttpResponseBadRequest('Invalid payload. Please contact support if this problem persists.')

## Build the return payload

qs = parse.parse_qs(decoded)
params = {
'nonce': qs['nonce'][0],
'email': request.user.email,
'external_id': request.user.id,
'username': request.user.username,
'require_activation': 'true'
}

return_payload = base64.encodestring(bytes(parse.urlencode(params), 'utf-8'))
h = hmac.new(key, return_payload, digestmod=hashlib.sha256)
query_string = parse.urlencode({'sso': return_payload, 'sig': h.hexdigest()})

## Redirect back to Discourse
return HttpResponseRedirect('%s?%s' % (settings.DISCOURSE_BASE_URL, query_string))
2 changes: 1 addition & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<body>

<nav class="navbar navbar-expand-lg navbar-dark bg-dark ">
<a class="navbar-brand" href='{% url "mainapp_URLs:home" %}'>Home</a>
<a class="navbar-brand" href='{% url "mainapp:home" %}'>Home</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExample09" aria-controls="navbarsExample09" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
Expand Down
7 changes: 5 additions & 2 deletions tool/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@
USE_L10N = True

USE_TZ = True


LOGIN_URL = '/admin/login/'
LOGIN_REDIRECT_URL = '/'
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

Expand All @@ -129,3 +129,6 @@

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

DISCOURSE_SSO_SECRET = "sample_sso_discourse"
DISCOURSE_BASE_URL = 'https://discourse.opengenus.org/session/sso_provider'
4 changes: 2 additions & 2 deletions tool/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from django.conf.urls.static import static
from django.conf import settings


sitemaps = {
'Tools': ToolSitemap(),
'Users': UserSitemap(),
Expand All @@ -30,6 +29,7 @@
}

urlpatterns = [
url(r'discourse/sso$', views.sso),
url(r'^admin/', admin.site.urls, name = 'admin_URLs'),
url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),
url(r'^about_sample_file/(?P<format>[\w]+)/$', views.about_sample_file,name='about_sample_file'),
Expand All @@ -38,7 +38,7 @@
url(r'^u/(?P<user_name>[^/]+)/$',views.user_profile,name='user_profile'),
url(r'^tags/(?P<tag_name>[^/]+)/$',views.tags,name='tag'),
url(r'^category/(?P<category_name>[^/]+)/$',views.category,name='category'),
url(r'^',include(('mainapp.urls','mainapp'),namespace = 'mainapp_URLs')),
url(r'^',include(('mainapp.urls','mainapp'),namespace = 'mainapp')),
]

if settings.DEBUG is True:
Expand Down