diff --git a/.env b/.env index 9163205..714efbd 100644 --- a/.env +++ b/.env @@ -1,5 +1,5 @@ -DATABASE_NAME=screencast -DATABASE_USER=screencastadmin@screencast2020 -DATABASE_PASSWORD=screencast2020! -DATABASE_HOST=screencast2020.postgres.database.azure.com +DATABASE_NAME=screencast +DATABASE_USER=screencastadmin@screencast2020 +DATABASE_PASSWORD=screencast2020! +DATABASE_HOST=screencast2020.postgres.database.azure.com DATABASE_PORT=5432 \ No newline at end of file diff --git a/.gitignore b/.gitignore index db82364..3091144 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,8 @@ __pycache__ myvenv db.sqlite3 /static -.DS_Store \ No newline at end of file +.DS_Store +# Elastic Beanstalk Files +.elasticbeanstalk/* +!.elasticbeanstalk/*.cfg.yml +!.elasticbeanstalk/*.global.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a6bdaff --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3 +ENV PYTHONUNBUFFERED 1 +RUN python -m pip install --upgrade pip +RUN mkdir /code +WORKDIR /code +COPY requirements.txt /code/ +RUN pip install -r requirements.txt +COPY . /code/ + diff --git a/Procfile b/Procfile index 1b048d1..98ea3c3 100644 --- a/Procfile +++ b/Procfile @@ -1,2 +1 @@ -web: gunicorn screencast --log-file - -web: python manage.py runserver 0.0.0.0:5000 \ No newline at end of file +web: gunicorn screencast.wsgi diff --git a/api/serializers.py b/api/serializers.py index 65aa532..28f5775 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -35,7 +35,7 @@ def validate(self,data): "result":result } - + class SocialSerializer(serializers.Serializer): """ Serializer which accepts an OAuth2 access token and provider. diff --git a/api/urls.py b/api/urls.py index 2c786d9..628f309 100644 --- a/api/urls.py +++ b/api/urls.py @@ -18,4 +18,5 @@ path('refresh',TokenRefreshView.as_view(),name='token_refresh'), path('question',views.getquestion.as_view(),name='question api'), path('facebooklogin',views.facebooklogin.as_view(),name='facebooklogin'), + path('status',views.configstatus,name='quiz_status'), ] \ No newline at end of file diff --git a/api/views.py b/api/views.py index 49db2b7..0f19b48 100644 --- a/api/views.py +++ b/api/views.py @@ -19,8 +19,9 @@ from social_django.utils import load_strategy, load_backend from social_core.backends.oauth import BaseOAuth2 from social_core.exceptions import MissingBackend, AuthTokenError, AuthForbidden +import time # Create your views here. - +import requests as r @api_view(['GET']) @@ -52,6 +53,23 @@ def get(self,request): } return Response(response) +import pytz +utc=pytz.UTC +@api_view(['GET']) +def configstatus(request): + configs=config.objects.all() + if configs: + response={ + "current_day":configs[0].current_day, + "start_time":configs[0].quiz_start.replace(tzinfo=utc), + "end_time":configs[0].quiz_endtime.replace(tzinfo=utc) + } + return Response(response) + response={ + "status":404, + "message":"no confings founnd" + } + return Response(response) class Answer(APIView): permission_classes=(IsAuthenticated,) @@ -120,8 +138,10 @@ def post(self, request): class facebooklogin(APIView): def post(self,request): + print(request.data) accesstoken=request.data.get('accesstoken') expiration_time=request.data.get('expiration_time') + print(expiration_time) userID=request.data.get('userID') if(int(expiration_time) < int(time.time())): content= {"status": 404} @@ -138,7 +158,7 @@ def post(self,request): username= idInfo['name'], image= idInfo['picture']['data']['url'], try: - user = User.objects.get(email=data['email']) + user = User.objects.get(email=email) except User.DoesNotExist: user = User() user.username = username diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..8075c50 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,23 @@ +version: '3' + +services: + db: + image: postgres + environment: + - POSTGRES_DB=postgres + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + web: + build: . + command: gunicorn screencast.wsgi:application --bind 0.0.0.0:8000 + ports: + - "8000:8000" + depends_on: + - db + - migration + + migration: + build: . + command: python manage.py migrate --noinput + depends_on: + - db \ No newline at end of file diff --git a/quiz/migrations/0001_initial.py b/quiz/migrations/0001_initial.py index 578eef7..62e5ec6 100644 --- a/quiz/migrations/0001_initial.py +++ b/quiz/migrations/0001_initial.py @@ -1,58 +1,58 @@ -# Generated by Django 3.0.5 on 2020-06-19 07:29 - -from django.conf import settings -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - initial = True - - dependencies = [ - migrations.swappable_dependency(settings.AUTH_USER_MODEL), - ] - - operations = [ - migrations.CreateModel( - name='config', - fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('current_day', models.IntegerField()), - ('q_no', models.IntegerField()), - ('quiz_start', models.DateTimeField()), - ('quiz_endtime', models.DateTimeField()), - ], - ), - migrations.CreateModel( - name='Question', - fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('question', models.CharField(max_length=550)), - ('day', models.IntegerField()), - ('question_no', models.IntegerField()), - ('answer', models.CharField(max_length=100)), - ('audio', models.FileField(blank=True, upload_to='media/audios')), - ('image', models.ImageField(blank=True, upload_to='media/images')), - ('hint', models.CharField(default='na', max_length=555)), - ], - options={ - 'ordering': ['day', 'question_no'], - }, - ), - migrations.CreateModel( - name='UserScore', - fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('name', models.CharField(max_length=55, null=True)), - ('score', models.IntegerField(default=0)), - ('rank', models.IntegerField(null=True)), - ('current_question', models.IntegerField()), - ('last_modified', models.DateTimeField(auto_now=True)), - ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), - ], - options={ - 'ordering': ['-score', 'last_modified'], - }, - ), - ] +# Generated by Django 3.0.5 on 2020-06-19 07:29 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='config', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('current_day', models.IntegerField()), + ('q_no', models.IntegerField()), + ('quiz_start', models.DateTimeField()), + ('quiz_endtime', models.DateTimeField()), + ], + ), + migrations.CreateModel( + name='Question', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('question', models.CharField(max_length=550)), + ('day', models.IntegerField()), + ('question_no', models.IntegerField()), + ('answer', models.CharField(max_length=100)), + ('audio', models.FileField(blank=True, upload_to='media/audios')), + ('image', models.ImageField(blank=True, upload_to='media/images')), + ('hint', models.CharField(default='na', max_length=555)), + ], + options={ + 'ordering': ['day', 'question_no'], + }, + ), + migrations.CreateModel( + name='UserScore', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=55, null=True)), + ('score', models.IntegerField(default=0)), + ('rank', models.IntegerField(null=True)), + ('current_question', models.IntegerField()), + ('last_modified', models.DateTimeField(auto_now=True)), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + options={ + 'ordering': ['-score', 'last_modified'], + }, + ), + ] diff --git a/quiz/models.py b/quiz/models.py index b9f3f93..1527853 100644 --- a/quiz/models.py +++ b/quiz/models.py @@ -76,8 +76,9 @@ def quiz_active(self): return False return True def save(self, force_insert=False, force_update=False, *args, **kwargs): - players=UserScore.objects.all() - for player in players: - player.current_question=1 - player.save() - super(config, self).save(force_insert, force_update, *args, **kwargs) + if not self.pk: + players=UserScore.objects.all() + for player in players: + player.current_question=1 + player.save() + super(config, self).save(force_insert, force_update, *args, **kwargs) diff --git a/requirements.txt b/requirements.txt index cae7ca3..3b8ac89 100644 Binary files a/requirements.txt and b/requirements.txt differ diff --git a/screencast/settings.py b/screencast/settings.py index 5dbe632..786c4fc 100644 --- a/screencast/settings.py +++ b/screencast/settings.py @@ -12,6 +12,7 @@ import os from datetime import timedelta +import django_heroku # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -20,12 +21,12 @@ # See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'k(o5900674725b3)1w5(lgz$9ckubfetysox12!(3h4=73+@^&' +SECRET_KEY = os.environ.get('SECRET_KEY','k(o5900674725b3)1w5(lgz$9ckubfetysox12!(3h4=73+@^&') # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = ['screencast20.azurewebsites.net','127.0.0.1','.herokuapp.com','.pythonanywhere.com'] +ALLOWED_HOSTS = [] # Application definition @@ -41,7 +42,7 @@ 'account', 'api', 'rest_framework', - #'corsheaders', + 'corsheaders', 'rest_framework_social_oauth2', 'oauth2_provider', 'social_django', @@ -94,11 +95,6 @@ WSGI_APPLICATION = 'screencast.wsgi.application' CORS_ORIGIN_ALLOW_ALL = True -CORS_ORIGIN_WHITELIST = ( - 'http//:localhost:8000', - # add your frontend server site here -) - # Database # https://docs.djangoproject.com/en/3.0/ref/settings/#databases @@ -109,20 +105,29 @@ #'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), # } #} -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.postgresql_psycopg2', - 'NAME': 'screencast', - 'USER': 'screencastadmin@screencast2020', - 'PASSWORD': 'screencast2020!', - 'HOST': 'screencast2020.postgres.database.azure.com', - - 'OPTIONS': { - #'sslmode': 'require', +if 'RDS_DB_NAME' in os.environ: + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'NAME': os.environ['RDS_DB_NAME'], + 'USER': os.environ['RDS_USERNAME'], + 'PASSWORD': os.environ['RDS_PASSWORD'], + 'HOST': os.environ['RDS_HOSTNAME'], + 'PORT': os.environ['RDS_PORT'], } } +else : + DATABASES = { # GALAXYZPJ'S LOCAL INSTANCE + 'default': { + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': 'postgres', + 'USER': 'postgres', + 'PASSWORD': 'postgres', + 'HOST': 'postgres', + 'PORT': '5432', + } } - #DATABASES = { + #DATABpippiASES = { # 'default': { # 'ENGINE': 'django.db.backends.postgresql_psycopg2', # 'NAME': 'd8s04nkvqhp0h4', @@ -230,4 +235,6 @@ 'SLIDING_TOKEN_REFRESH_EXP_CLAIM': 'refresh_exp', 'SLIDING_TOKEN_LIFETIME': timedelta(minutes=30), 'SLIDING_TOKEN_REFRESH_LIFETIME': timedelta(days=1), -} \ No newline at end of file +} + +django_heroku.settings(locals()) \ No newline at end of file