Skip to content

Commit

Permalink
REST api and admin page (#28)
Browse files Browse the repository at this point in the history
* add django rest api

* add .DS_STORE to .gitignore

* move api into new project structure

* admin page

* fix syntax of line
  • Loading branch information
jdansey committed May 14, 2019
1 parent 04311c6 commit afa7b61
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -12,6 +12,7 @@ __pycache__/
__pycache__
db.sqlite3
media
.DS_Store

# Backup files #
*.bak
Expand Down
6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

4 changes: 3 additions & 1 deletion apps/civic_pulse/admin.py
@@ -1,3 +1,5 @@
from django.contrib import admin

from .models import Agency,Entry
# Register your models here.
admin.site.register(Agency)
admin.site.register(Entry)
Empty file.
14 changes: 14 additions & 0 deletions apps/civic_pulse/api/serializers.py
@@ -0,0 +1,14 @@
from rest_framework import serializers
from apps.civic_pulse.models import Agency, Entry


class AgencySerializer(serializers.ModelSerializer):
class Meta:
model = Agency
fields = ['id','name','website','twitter','facebook','phone_number','address','description','last_successful_scrape','scrape_counter']


class EntrySerializer(serializers.ModelSerializer):
class Meta:
model = Entry
fields = ['agency','https_enabled','has_privacy_policy','mobile_friendly','good_performance','has_social_media','has_contact_info']
14 changes: 14 additions & 0 deletions apps/civic_pulse/api/viewsets.py
@@ -0,0 +1,14 @@
from rest_framework import viewsets

from apps.civic_pulse.api.serializers import AgencySerializer,EntrySerializer
from apps.civic_pulse.models import Agency, Entry


class AgencyViewSet(viewsets.ModelViewSet):
queryset = Agency.objects.all()
serializer_class = AgencySerializer


class EntryViewSet(viewsets.ModelViewSet):
queryset = Entry.objects.all()
serializer_class = EntrySerializer
3 changes: 1 addition & 2 deletions apps/civic_pulse/views.py
@@ -1,5 +1,5 @@
from django.views import generic
from .models import *
from .models import Agency


class AgencyListView(generic.ListView):
Expand All @@ -20,4 +20,3 @@ def get_context_data(self, **kwargs):
agency = context['object']
context['last_entry'] = agency.entry_set.last()
return context

12 changes: 4 additions & 8 deletions config/settings/settings.py
Expand Up @@ -12,14 +12,9 @@

import os

# the project directory is obtained by finding the directory path two levels
# up the directory of this file. dirname is to get the directory, and then
# join and normpath functions generate and convert the path to the correct
# project directory
BASE_DIR = os.path.normpath(
os.path.join(
os.path.dirname(
os.path.abspath(__file__)), "..", ".."))
# 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/
Expand All @@ -43,6 +38,7 @@
'django.contrib.messages',
'django.contrib.staticfiles',
'apps.civic_pulse',
'rest_framework'
]

MIDDLEWARE = [
Expand Down
13 changes: 10 additions & 3 deletions config/urls.py
Expand Up @@ -13,12 +13,19 @@
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.conf.urls import url, include
from django.contrib import admin
from rest_framework import routers
from apps.civic_pulse.api.viewsets import *
from apps.civic_pulse.views import *

router = routers.DefaultRouter()
router.register(r'entries', EntryViewSet)
router.register(r'agencies', AgencyViewSet)

urlpatterns = [
# url(r'^admin/', admin.site.urls),
url(r'^admin/', admin.site.urls),
url(r'^$', AgencyListView.as_view(), name='index'),
url(r'^agency/(?P<pk>[0-9]+)/$',AgencyView.as_view(),name='agency-detail')
url(r'^agency/(?P<pk>[0-9]+)/$',AgencyView.as_view(),name='agency-detail'),
url(r'^api/', include(router.urls)),
]
2 changes: 1 addition & 1 deletion requirements.txt
@@ -1,6 +1,7 @@
beautifulsoup4==4.7.1
certifi==2019.3.9
chardet==3.0.4
djangorestframework==3.9.3
Django==2.2
idna==2.8
Pillow==6.0.0
Expand All @@ -9,4 +10,3 @@ requests==2.21.0
simplejson==3.16.0
sqlparse==0.3.0
urllib3==1.24.1
Pillow==6.0.0

0 comments on commit afa7b61

Please sign in to comment.