diff --git a/.gitignore b/.gitignore index 26748be..802fe5a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,8 +3,12 @@ */migrations/* media/history/* spotifyvis/static/graphs/sass/* +static/admin/ +static/django_tables2/ +static/graphs/ *.bak +*.css *.log *.map *.orig diff --git a/README.md b/README.md index 39f539a..d45e191 100644 --- a/README.md +++ b/README.md @@ -19,35 +19,35 @@ Before starting the setup, make sure Python 3 and PostgreSQL is installed on you ``` pip install --user pipenv ``` - 2. Create a virtual environment. ``` python3 -m venv /path/to/new/virtual/environment ``` - -3. `cd` into the directory you just created a virtual environment in, and clone the GitHub repo: +3. `cd` into the directory you just created a virtual environment in, +and clone the GitHub repo: ``` +cd /path/to/new/virtual/environment git clone https://github.com/Kevin-Mok/spotify-lib-vis ``` - 4. Activate the virtual environment from the command line. ``` +# activate.fish if you're using fish shell source bin/activate ``` - -5. `cd` into the repository root directory and install all dependencies using `pip`. +5. `cd` into the repository root directory and install all +dependencies using `pip`. ``` +cd spotify-lib-vis pip install -r requirements.txt ``` - 6. Run `reset_db.sh` to create the database and start the server. ``` -cd src && ./reset_db.sh +./scripts/reset_db.sh ``` diff --git a/login/views.py b/login/views.py index 226bc1c..17c78e1 100644 --- a/login/views.py +++ b/login/views.py @@ -16,6 +16,8 @@ from .utils import * TIME_FORMAT = '%Y-%m-%d-%H-%M-%S' TRACKS_TO_QUERY = 200 AUTH_SCOPE = ['user-library-read', 'user-read-recently-played', ] +# ROOT_URL = 'http://localhost:8000' +ROOT_URL = 'http://spotify-lib-vis.ml' # index {{{ # @@ -37,7 +39,7 @@ def spotify_login(request): payload = { 'client_id': os.environ['SPOTIFY_CLIENT_ID'], 'response_type': 'code', - 'redirect_uri': 'http://localhost:8000/login/callback', + 'redirect_uri': ROOT_URL + '/login/callback', 'state': state_str, 'scope': " ".join(AUTH_SCOPE), 'show_dialog': False @@ -64,7 +66,7 @@ def callback(request): payload = { 'grant_type': 'authorization_code', 'code': code, - 'redirect_uri': 'http://localhost:8000/login/callback', + 'redirect_uri': ROOT_URL + '/login/callback', 'client_id': os.environ['SPOTIFY_CLIENT_ID'], 'client_secret': os.environ['SPOTIFY_CLIENT_SECRET'], } diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..cfea621 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,40 @@ +astroid==2.2.5 +backports.csv==1.0.7 +certifi==2019.6.16 +chardet==3.0.4 +defusedxml==0.6.0 +Django==2.2.4 +django-appconf==1.0.3 +django-compressor==2.3 +django-crispy-forms==1.7.2 +django-filter==2.2.0 +django-sass-processor==0.7.3 +django-tables2==2.1.0 +djangorestframework==3.10.2 +et-xmlfile==1.0.1 +idna==2.8 +isort==4.3.21 +jdcal==1.4.1 +lazy-object-proxy==1.4.2 +libsass==0.19.2 +mccabe==0.6.1 +odfpy==1.4.0 +openpyxl==2.6.3 +psycopg2==2.8.3 +psycopg2-binary==2.8.3 +pylint==2.3.1 +python-dateutil==2.8.0 +pytz==2019.2 +PyYAML==5.1.2 +rcssmin==1.0.6 +requests==2.22.0 +rjsmin==1.1.0 +six==1.12.0 +sqlparse==0.3.0 +tablib==0.13.0 +typed-ast==1.4.0 +unicodecsv==0.14.1 +urllib3==1.25.3 +wrapt==1.11.2 +xlrd==1.2.0 +xlwt==1.3.0 diff --git a/scripts/create_user.sql b/scripts/create_user.sql new file mode 100644 index 0000000..ecfc749 --- /dev/null +++ b/scripts/create_user.sql @@ -0,0 +1,2 @@ +DROP ROLE IF EXISTS spotifyvis_user; +CREATE ROLE spotifyvis_user LOGIN PASSWORD 'spotifyvis_user'; diff --git a/scripts/history.sql b/scripts/queries/history.sql similarity index 60% rename from scripts/history.sql rename to scripts/queries/history.sql index 6968b05..cd43756 100644 --- a/scripts/history.sql +++ b/scripts/queries/history.sql @@ -1,5 +1,6 @@ -- select t.name as track, a.name as artist -select timestamp, substring(t.name for 50) as track, a.name as artist +select distinct on (1) +timestamp, substring(t.name for 50) as track, a.name as artist from api_history as h join api_track as t on h.track_id = t.id @@ -7,5 +8,6 @@ join api_track_artists ta on ta.track_id = t.id join api_artist a on a.id = ta.artist_id --- limit 10; -; +order by timestamp desc +-- ; +limit 20; diff --git a/scripts/most-played.sql b/scripts/queries/most-played.sql similarity index 100% rename from scripts/most-played.sql rename to scripts/queries/most-played.sql diff --git a/scripts/requirements.txt b/scripts/requirements.txt deleted file mode 100644 index 5c55b86..0000000 --- a/scripts/requirements.txt +++ /dev/null @@ -1,40 +0,0 @@ -astroid==2.2.5 -backports.csv==1.0.7 -certifi==2019.3.9 -chardet==3.0.4 -defusedxml==0.5.0 -Django==2.2 -django-appconf==1.0.3 -django-compressor==2.2 -django-crispy-forms==1.7.2 -django-filter==2.1.0 -django-sass-processor==0.7.3 -django-tables2==2.0.6 -djangorestframework==3.9.2 -et-xmlfile==1.0.1 -idna==2.8 -isort==4.3.17 -jdcal==1.4 -lazy-object-proxy==1.3.1 -libsass==0.18.0 -mccabe==0.6.1 -odfpy==1.4.0 -openpyxl==2.6.2 -psycopg2==2.8.2 -psycopg2-binary==2.8.2 -pylint==2.3.1 -python-dateutil==2.8.0 -pytz==2019.1 -PyYAML==5.1 -rcssmin==1.0.6 -requests==2.21.0 -rjsmin==1.1.0 -six==1.12.0 -sqlparse==0.3.0 -tablib==0.13.0 -typed-ast==1.3.1 -unicodecsv==0.14.1 -urllib3==1.24.1 -wrapt==1.11.1 -xlrd==1.2.0 -xlwt==1.3.0 diff --git a/scripts/reset_db.sh b/scripts/reset_db.sh index 9405ebf..3fa6b5b 100755 --- a/scripts/reset_db.sh +++ b/scripts/reset_db.sh @@ -1,15 +1,16 @@ -# check if in virtual environment -# https://stackoverflow.com/questions/15454174/how-can-a-shell-function-know-if-it-is-running-within-a-virtualenv/15454916 +#!/bin/bash -# python -c 'import sys; print(sys.real_prefix)' 2>/dev/null && INVENV=1 || INVENV=0 -# INVENV=$(python -c 'import sys; print ("1" if hasattr(sys, "real_prefix") else "0")') +# check if in virtual environment +# https://stackoverflow.com/a/15454284/8811872 +[[ "$VIRTUAL_ENV" == "" ]]; INVENV=$? # if $INVENV is 1, then in virtualenv # echo $INVENV -# if [ $INVENV -eq 1 ]; then +if [ $INVENV -eq 1 ]; then rm login/migrations/0* api/migrations/0* - sudo -u postgres psql -f reset_db.sql - python manage.py makemigrations login api - python manage.py migrate --fake - python manage.py runserver -# fi + sudo -u postgres psql -f ./scripts/reset_db.sql + python3 manage.py makemigrations login api + python3 manage.py migrate --fake + python3 manage.py runserver +fi + diff --git a/scripts/reset_db.sql b/scripts/reset_db.sql index 7a99492..871a514 100644 --- a/scripts/reset_db.sql +++ b/scripts/reset_db.sql @@ -1,3 +1,3 @@ -DROP DATABASE spotifyvis; +DROP DATABASE IF EXISTS spotifyvis; CREATE DATABASE spotifyvis; -GRANT ALL PRIVILEGES ON DATABASE spotifyvis TO django; +GRANT ALL PRIVILEGES ON DATABASE spotifyvis TO spotifyvis_user; diff --git a/spotifyvis/settings.py b/spotifyvis/settings.py index dfbd4aa..51c1501 100644 --- a/spotifyvis/settings.py +++ b/spotifyvis/settings.py @@ -25,7 +25,7 @@ SECRET_KEY = 'm(gh1&063)haj#^t+h-qh@o6#%w=ruqi_#=6yzi=#@q6#0p*gv' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = ['spotify-lib-vis.ml'] # Application definition @@ -86,8 +86,8 @@ DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'spotifyvis', - 'USER': 'django', - 'PASSWORD': 'django', + 'USER': 'spotifyvis_user', + 'PASSWORD': 'spotifyvis_user', 'HOST': 'localhost', 'PORT': '', } @@ -130,7 +130,7 @@ USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/2.0/howto/static-files/ STATIC_URL = '/static/' -STATIC_ROOT = os.path.join(BASE_DIR, "spotifyvis/static") +STATIC_ROOT = os.path.join(BASE_DIR, "static/") STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static/"), ] diff --git a/spotifyvis/urls.py b/spotifyvis/urls.py index ec4528d..3556d36 100644 --- a/spotifyvis/urls.py +++ b/spotifyvis/urls.py @@ -19,6 +19,7 @@ from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('login.urls', namespace="main")), + path('login/', include('login.urls', namespace="main")), path('api/', include('api.urls', namespace="api")), path('graphs/', include('graphs.urls', namespace="graphs")), ] diff --git a/static/todo.md b/static/todo.md index 566aa5a..355f690 100644 --- a/static/todo.md +++ b/static/todo.md @@ -5,3 +5,4 @@ - loading/progress indicator - change js tabs to 2 spaces - Heroku? +- send out weekly email for history stats