-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d079c5
commit 5c62567
Showing
21 changed files
with
412 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export S_KEY='not secret' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[[source]] | ||
url = "https://pypi.org/simple" | ||
verify_ssl = true | ||
name = "pypi" | ||
|
||
[packages] | ||
gunicorn = "==19.7.1" | ||
dj-database-url = "==0.4.2" | ||
"psycopg2-binary" = "==2.7.4" | ||
whitenoise = "==3.3.1" | ||
"Jinja2" = "==2.10" | ||
Django = "==1.11.9" | ||
djangorestframework = "*" | ||
|
||
[dev-packages] | ||
|
||
[requires] | ||
python_version = "3.6" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class ApiConfig(AppConfig): | ||
name = 'api' |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from rest_framework import serializers | ||
from main.models import Code, User | ||
|
||
|
||
class UserSer(serializers.ModelSerializer): | ||
class Meta: | ||
model = User | ||
fields = ('pk', | ||
'ip', | ||
'name', | ||
'clicks', | ||
) | ||
|
||
|
||
class CodeSer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Code | ||
fields = ('clicks', | ||
'prize', | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from django.conf.urls import url | ||
from django.contrib import admin | ||
from . import views | ||
|
||
urlpatterns = [ | ||
url(r'^click/', views.post_click), | ||
url(r'^click_data/', views.get_code_data), | ||
url(r'^user_data/', views.get_user_data), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from django.shortcuts import render | ||
from rest_framework import status | ||
from rest_framework.decorators import api_view | ||
from rest_framework.response import Response | ||
|
||
from main.models import Code, User | ||
from .serializers import CodeSer, UserSer | ||
|
||
|
||
@api_view(['GET', 'POST']) | ||
def post_click(request): | ||
name = request.GET.get('name') | ||
ip = get_client_ip(request) | ||
|
||
user = User.objects.filter(ip=ip).first() | ||
clicks = Code.objects.all().first() | ||
|
||
user.clicks = user.clicks + 1; | ||
user.save() | ||
|
||
clicks.clicks = clicks.clicks + 1; | ||
clicks.save() | ||
|
||
if name is not None and name != '/': | ||
user.name = name | ||
user.save() | ||
|
||
return Response() | ||
|
||
|
||
@api_view(['GET', 'POST']) | ||
def get_code_data(request): | ||
clicks = Code.objects.all().first() | ||
serializer = CodeSer(clicks) | ||
return Response(serializer.data, template_name="api.html") | ||
|
||
@api_view(['GET', 'POST']) | ||
def get_user_data(request): | ||
ip = get_client_ip(request) | ||
user = User.objects.filter(ip=ip).first() | ||
|
||
if user is None: | ||
user = User(ip=ip) | ||
user.save() | ||
|
||
serializer = UserSer(user) | ||
return Response(serializer.data) | ||
|
||
def get_client_ip(request): | ||
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') | ||
if x_forwarded_for: | ||
ip = x_forwarded_for.split(',')[0] | ||
else: | ||
ip = request.META.get('REMOTE_ADDR') | ||
return ip | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
""" | ||
Django settings for codeclicker project. | ||
Generated by 'django-admin startproject' using Django 1.11.7. | ||
For more information on this file, see | ||
https://docs.djangoproject.com/en/1.11/topics/settings/ | ||
For the full list of settings and their values, see | ||
https://docs.djangoproject.com/en/1.11/ref/settings/ | ||
""" | ||
|
||
import os | ||
import dj_database_url | ||
|
||
|
||
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) | ||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
|
||
|
||
# Quick-start development settings - unsuitable for production | ||
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ | ||
|
||
# SECURITY WARNING: keep the secret key used in production secret! | ||
SECRET_KEY = os.environ['S_KEY'] | ||
|
||
# SECURITY WARNING: don't run with debug turned on in production! | ||
DEBUG = True | ||
|
||
ALLOWED_HOSTS = ['.herokuapp.com', '127.0.0.1'] | ||
|
||
|
||
|
||
# Application definition | ||
|
||
INSTALLED_APPS = [ | ||
'django.contrib.admin', | ||
'django.contrib.auth', | ||
'django.contrib.contenttypes', | ||
'django.contrib.sessions', | ||
'django.contrib.messages', | ||
'django.contrib.staticfiles', | ||
'main' | ||
] | ||
|
||
MIDDLEWARE = [ | ||
'django.middleware.security.SecurityMiddleware', | ||
'django.contrib.sessions.middleware.SessionMiddleware', | ||
'django.middleware.common.CommonMiddleware', | ||
'django.middleware.csrf.CsrfViewMiddleware', | ||
'django.contrib.auth.middleware.AuthenticationMiddleware', | ||
'django.contrib.messages.middleware.MessageMiddleware', | ||
'django.middleware.clickjacking.XFrameOptionsMiddleware', | ||
'whitenoise.middleware.WhiteNoiseMiddleware', | ||
] | ||
|
||
ROOT_URLCONF = 'codeclicker.urls' | ||
|
||
TEMPLATES = [ | ||
{ | ||
'BACKEND': 'django.template.backends.django.DjangoTemplates', | ||
'DIRS': ['resources/templates'], | ||
'APP_DIRS': True, | ||
'OPTIONS': { | ||
'context_processors': [ | ||
'django.template.context_processors.debug', | ||
'django.template.context_processors.request', | ||
'django.contrib.auth.context_processors.auth', | ||
'django.contrib.messages.context_processors.messages', | ||
], | ||
}, | ||
}, | ||
] | ||
|
||
WSGI_APPLICATION = 'codeclicker.wsgi.application' | ||
|
||
|
||
# Database | ||
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases | ||
|
||
DATABASES = { | ||
'default': { | ||
'ENGINE': 'django.db.backends.postgresql', | ||
'NAME': '', | ||
'USER': '', | ||
'PASSWORD': '', | ||
'HOST': '', | ||
'PORT': '', | ||
} | ||
} | ||
|
||
db_from_env = dj_database_url.config() | ||
DATABASES['default'].update(db_from_env) | ||
|
||
|
||
# Password validation | ||
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators | ||
|
||
AUTH_PASSWORD_VALIDATORS = [ | ||
{ | ||
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', | ||
}, | ||
{ | ||
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', | ||
}, | ||
{ | ||
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', | ||
}, | ||
{ | ||
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', | ||
}, | ||
] | ||
|
||
|
||
# Internationalization | ||
# https://docs.djangoproject.com/en/1.11/topics/i18n/ | ||
|
||
LANGUAGE_CODE = 'en-us' | ||
|
||
TIME_ZONE = 'UTC' | ||
|
||
USE_I18N = True | ||
|
||
USE_L10N = True | ||
|
||
USE_TZ = True | ||
|
||
|
||
# Static files (CSS, JavaScript, Images) | ||
# https://docs.djangoproject.com/en/1.11/howto/static-files/ | ||
|
||
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') | ||
|
||
STATICFILES_DIRS = [ | ||
os.path.join(BASE_DIR, 'static'), | ||
#'/var/www/static/', | ||
] | ||
STATIC_URL = '/static/' | ||
|
||
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' |
Oops, something went wrong.