Skip to content

Commit

Permalink
Set up api endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
armaangoel78 committed Aug 6, 2018
1 parent 2d079c5 commit 5c62567
Show file tree
Hide file tree
Showing 21 changed files with 412 additions and 13 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export S_KEY='not secret'
18 changes: 18 additions & 0 deletions Pipfile
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"
115 changes: 115 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added __init__.py
Empty file.
Empty file added api/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions api/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions api/apps.py
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 added api/migrations/__init__.py
Empty file.
20 changes: 20 additions & 0 deletions api/serializers.py
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',
)
3 changes: 3 additions & 0 deletions api/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
9 changes: 9 additions & 0 deletions api/urls.py
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),
]
57 changes: 57 additions & 0 deletions api/views.py
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


140 changes: 140 additions & 0 deletions codeclicker/production.py
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'
Loading

0 comments on commit 5c62567

Please sign in to comment.