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
81 changes: 63 additions & 18 deletions ADFS/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,26 @@
from django.http import HttpResponse, HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import redirect
from django.conf import settings

from .models import Attention, ADFSUser
from .forms import ContactForm
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth import authenticate, login

from base64 import b64decode
import base64
from django.core.files.base import ContentFile

import requests
from urlparse import parse_qs
import json
# Create your views here.

try:
import urlparse
from urllib import urlencode
except:
import urllib.parse as urlparse
from urllib.parse import urlencode


def decode_base64(data):
"""Decode base64, padding being optional.
Expand All @@ -26,14 +33,16 @@ def decode_base64(data):
"""
missing_padding = len(data) % 4
if missing_padding != 0:
data += b'='* (4 - missing_padding)
data += b'=' * (4 - missing_padding)
return base64.decodestring(data)


def survey(request):
template = loader.get_template('base_react.html')
context = RequestContext(request, {})
return HttpResponse(template.render(context))


def regl(request):
template = loader.get_template('reglam.html')
context = RequestContext(request, {})
Expand All @@ -59,6 +68,7 @@ def reglament(request):
context = RequestContext(request, {})
return HttpResponse(template.render(context))


def register_attention(request):
form = ContactForm()
if request.method == 'POST':
Expand All @@ -76,6 +86,7 @@ def register_attention(request):
return HttpResponseRedirect('/attention/%i' % a.id)
return render(request, 'attention.html', {'form': form})


@csrf_exempt
def register(request):
data = request.POST.dict()
Expand All @@ -84,11 +95,11 @@ def register(request):

try:
user = ADFSUser.objects.create_user(
username=data['login'],
email=data['email'],
password=data['password'])
username=data['login'],
email=data['email'],
password=data['password'])

if data.get('avatar', None) != None:
if data.get('avatar', None):
user.avatar = ContentFile(b64decode(data['avatar']), 'rosimka.png')

user.save()
Expand All @@ -102,6 +113,7 @@ def register(request):
content_type='application/json',
status=201)


@csrf_exempt
def autorisation(request):
if request.method == 'GET':
Expand All @@ -123,7 +135,9 @@ def autorisation(request):
print("The password is valid, but the account is disabled!")
else:
print("The username and password were incorrect.")
return HttpResponse(json.dumps({ 'error': 'Incorrect login or password' }), status=403)
return HttpResponse(
json.dumps({'error': 'Incorrect login or password'}),
status=403)

if user is not None:
context = RequestContext(request, {
Expand All @@ -133,20 +147,50 @@ def autorisation(request):
else:
context = RequestContext(request, {})
template = loader.get_template('gratulations.html')
return HttpResponse(json.dumps({ 'login': user.username, 'active': t }))
return HttpResponse(json.dumps({'login': user.username, 'active': t}))


@csrf_exempt
def autorisation_vk(request):
code = request.GET['code']
query_string = urllib.urlencode({
'client_id': settings.OAUTH_PUBLIC_CONFIGS['vk']['client_id'],
'code': code,
'client_secret': settings.OAUTH_PRIVATE_CONFIGS['vk']['client_secret'],
'redirect_uri': settings.OAUTH_PUBLIC_CONFIGS['vk']['redirect_uri'],
})

r = requests.get("https://oauth.vk.com/access_token?%s" % query_string)
params = json.loads(r.text)
access_token = params['access_token']


@csrf_exempt
def autorisation_github(request):
try:
code = request.GET['code']
r = requests.post("https://github.com/login/oauth/access_token",
data = {'client_id':'335f38f2aab459864d81', 'client_secret': '1aba874073116d193e0f324e1382df5c4a25b8d3', 'code': code, 'accept': 'application/json'})
client_id = settings.OAUTH_PUBLIC_CONFIGS['github']['client_id']
client_secret = \
settings.OAUTH_PRIVATE_CONFIGS['github']['client_secret']

params = parse_qs(r.text)
r = requests.post("https://github.com/login/oauth/access_token",
data={
'client_id': client_id,
'client_secret': client_secret,
'code': code,
'accept': 'application/json',
})

params = urlparse.parse_qs(r.text)
access_token = params['access_token']
response = requests.get("https://api.github.com/user/emails?access_token=%s" % access_token[0]).json()
response_user = requests.get("https://api.github.com/user?access_token=%s" % access_token[0]).json()
response = requests.get(
"https://api.github.com/user/emails?access_token=%s" %
access_token[0]).json()

response_user = requests.get(
"https://api.github.com/user?access_token=%s" %
access_token[0]).json()

emails = []
for email in response:
if email['verified']:
Expand All @@ -162,9 +206,9 @@ def autorisation_github(request):
return HttpResponse(template.render(context))

user = ADFSUser.objects.create_user(
username=response_user['login'],
email=response[0]['email'],
password='rasim')
username=response_user['login'],
email=response[0]['email'],
password='rasim')

new_user = authenticate(username=user.username, api=True)
login(request, new_user)
Expand All @@ -174,5 +218,6 @@ def autorisation_github(request):
except Exception:
return redirect('/')


def is_gast(request):
return not request.user.is_authenticated()
13 changes: 13 additions & 0 deletions config/oathConfigs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"github": {
"client_id": "335f38f2aab459864d81"
},
"facebook": {
"client_id": "524576674585233"
},
"vk": {
"client_id": "6292497",
"scope": "4259840",
"redirect_uri": "https://adf-saratov.ru/vk"
}
}
12 changes: 12 additions & 0 deletions config/oauthConfigsSecret.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"github": {
"client_secret": "1aba874073116d193e0f324e1382df5c4a25b8d3"
},
"vk": {
"client_secret": "vjUMBpqya7XshaEcbcDP",
"service_secret": "0c96887c0c96887c0c96887c000cf68c6d00c960c96887c56ba56c8f1cf765dcaf9408b"
},
"facebook": {
"client_secret": "rasim"
}
}
5 changes: 5 additions & 0 deletions static/css/main.less
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
@import "matchPage.less";
@import "articles.less";

.item-wrapper {
display: inline-flex;
align-items: center;
}

.social-label {
width: 100%;
padding-top: 7px;
Expand Down
6 changes: 4 additions & 2 deletions static/js/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ReactDOM from 'react-dom';
import ReactCSSTransitionGroup from 'react-addons-css-transition-group';
import { RegisterForm } from './components/registerForm';
import configs from './config';
import oauthConfigs from '../../config/oathConfigs.json';

const FontAwesome = require('react-fontawesome');

Expand Down Expand Up @@ -96,8 +97,9 @@ export const renderLoginForm = () => {
</div>
<div className="social-label">Войти через социальные сети</div>
<div className="social-icons">
<a className="btn btn-default" href="https://github.com/login/oauth/authorize?scope=user:email&client_id=335f38f2aab459864d81"><span className="fa fa-github" aria-hidden="true"></span></a>
<a className="btn btn-default" href="https://www.facebook.com/v2.11/dialog/oauth?client_id=524576674585233&redirect_uri=https%3A%2F%2Fadf-saratov.ru%2Ffacebook"><span className="fa fa-facebook-square"></span></a>
<a className="btn btn-default" href={`https://github.com/login/oauth/authorize?scope=user:email&client_id=${oauthConfigs.github.client_id}`}><span className="fa fa-github" aria-hidden="true"></span></a>
<a className="btn btn-default" href={`https://www.facebook.com/v2.11/dialog/oauth?client_id=${oauthConfigs.facebook.client_id}&redirect_uri=https%3A%2F%2Fadf-saratov.ru%2Ffacebook`}><span className="fa fa-facebook-square"></span></a>
<a className="btn btn-default" href={`https://oauth.vk.com/authorize?client_id=${oauthConfigs.vk.client_id}&redirect_uri=http%3A%2F%2Flocalhost:8080%2Fvk&response_type=code&scope=4259840`}><span className="fa fa-vk"></span></a>
</div>
</form>
</div>
Expand Down
12 changes: 5 additions & 7 deletions templates/teamlogic/stadions.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@
<h2> Стадионы </h2>
<div class="list-objects">
{% for stadion in object_list %}
<div class="col-md-12">
<div class="row team-wrapper">
<div class="col-md-1">
<div class="team-wrapper item-wrapper">
<div class="col-md-1" style="float: left;">
{% thumbnail stadion.image "64x64" crop="center" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}" class="media-object" float="left">
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}" class="media-object" float="left" style="display: block;">
{% endthumbnail %}
</div>
<div class="col-md-6">
<br> <strong> <a href="{{stadion.get_absolute_url}}"> {{ stadion }} </a> </strong>
<div style="float: right;">
<strong> <a href="{{stadion.get_absolute_url}}"> {{ stadion }} </a> </strong>
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock %}
8 changes: 3 additions & 5 deletions templates/teamlogic/teams.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@
<h2> Команды </h2>
<div class="list-objects">
{% for team in object_list %}
<div class="col-md-12">
<div class="row team-wrapper">
<div class="col-md-1">
<div class="team-wrapper item-wrapper">
<div class="col-md-1" style="float: left;">
{% thumbnail team.image "64x64" crop="center" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}" class="media-object" float="left">
{% endthumbnail %}
</div>
<div class="col-md-6">
<div style="float: right;">
<div class="media-body">
<h5 class="media-title"><strong> <a href="{{team.get_absolute_url}}"> {{team}} </a> </strong></h5>
</div>
</div>
</div>
</div>
{% endfor %}
</div>
{% pager %}
Expand Down
12 changes: 12 additions & 0 deletions untitled1/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
import json

BASE_DIR = os.path.dirname(os.path.dirname(__file__))


Expand Down Expand Up @@ -136,6 +138,16 @@
# pagination
PAGINATE_BY = 10

# oath configs for 3-rd party services (github, facebook, vk)
OAUTH_PUBLIC_CONFIGS = None
OAUTH_PRIVATE_CONFIGS = None

with open("./config/oathConfigs.json") as f:
OAUTH_PUBLIC_CONFIGS = json.loads(f.read())

with open("./config/oauthConfigsSecret.json") as f:
OAUTH_PRIVATE_CONFIGS = json.loads(f.read())

try:
from settings_local import *
except:
Expand Down
1 change: 1 addition & 0 deletions untitled1/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class UserViewSet(viewsets.ModelViewSet):
url(r'^tinymce/', include('tinymce.urls')),
url(r'logic/', include(teamlogic_urls)),
url(r'^login/github', views.autorisation_github),
url(r'^vk', views.autorisation_vk),
url(r'^login/', views.autorisation),
url(r'^register/', views.register),
url(r'^survey', views.survey),
Expand Down