Skip to content

Commit

Permalink
Django 2 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
EmpireErwinKooi committed Apr 7, 2018
1 parent d17f966 commit 77d5e28
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 116 deletions.
36 changes: 18 additions & 18 deletions land/urls.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
from django.conf.urls import patterns, url
from django.conf.urls import url
from land import views

urlpatterns = patterns("",
url(
regex=r"^$",
view=views.index,
name="landing_page"
),
urlpatterns = [
url(
regex=r"^$",
view=views.index,
name="landing_page"
),

url(
regex=r"^about/$",
view=views.about,
name="landing_page"
),
url(
regex=r"^about/$",
view=views.about,
name="about_page"
),

url(
regex=r"^demos/$",
view=views.demos,
name="landing_page"
),
)
url(
regex=r"^demos/$",
view=views.demos,
name="demos_page"
),
]
19 changes: 16 additions & 3 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@
import os
import sys


if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "beer_store_api.settings")

from django.core.management import execute_from_command_line

try:
from django.core.management import execute_from_command_line
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
# exceptions on Python 2.
try:
import django
except ImportError:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
)
raise
execute_from_command_line(sys.argv)
17 changes: 10 additions & 7 deletions products/management/commands/get_beer_availability.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
# -*- coding: utf-8 -*-
import json
import urllib2
import requests
from django.core.management.base import BaseCommand
from django.template import Template, Context
from django.conf import settings
from lxml import etree
import pytz
from products.models import Store, Product
from url_settings import TOP_URL
from .url_settings import TOP_URL


class Command(BaseCommand):
def handle(self, *args, **options):
Expand All @@ -22,7 +19,13 @@ def handle(self, *args, **options):
for product in products:

# retrieve availability json
data = json.load(urllib2.urlopen(TOP_URL+'/json/stores-with-sku/storelocations.bpid.%s.json' % (product.product_id,)))
url = TOP_URL+'/json/stores-with-sku/storelocations.bpid.%s.json' % product.product_id
# print(url)
s = requests.get(url).content.decode()
if s[0] != '{':
continue
# print(s)
data = json.loads(s)
data = data['features']

# create a list of stores to reduce amount of DB queries
Expand Down
26 changes: 12 additions & 14 deletions products/management/commands/get_beers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import urllib2
import requests
from bs4 import BeautifulSoup
from django.core.management.base import BaseCommand
from django.template import Template, Context
from django.conf import settings
from products.models import Product
from url_settings import TOP_URL
from .url_settings import TOP_URL


class Command(BaseCommand):
Expand All @@ -13,25 +11,25 @@ def handle(self, *args, **options):
Scrapes and stores product information
"""
# get beer page html and make soup object
html = urllib2.urlopen(TOP_URL + "/beers/search")
soup_beers = BeautifulSoup(html)
html = requests.get(TOP_URL + "/beers/search").content
soup_beers = BeautifulSoup(html, "html.parser")

# find all beers
beers = soup_beers.find_all("a", "brand-link")

for beer in beers:
# get beer page and make soup object
beer_url = beer["href"]
beer_html = urllib2.urlopen(TOP_URL + beer_url)
soup_beer = BeautifulSoup(beer_html)
beer_html = requests.get(TOP_URL + beer_url).content
soup_beer = BeautifulSoup(beer_html, "html.parser")

# get sizes
beer_products = soup_beer.find_all("table", "brand-pricing")

# get propertis and valus and merge them into dict
labels = soup_beer.dl.find_all("dt")
details = soup_beer.dl.find_all("dd")
beer_details = dict(zip(labels,details))
beer_details = dict(zip(labels, details))

# get name and image
beer_name = soup_beer.find("div", "only-desktop").find("h1", "page-title").get_text()
Expand All @@ -57,18 +55,18 @@ def handle(self, *args, **options):
# get product information
beer_ids = beer_size.a["href"].split('=')[1]
beer_id = beer_ids.split('-')[0]
print beer_id
# print beer_id
beer_product_id = beer_ids.split('-')[1]

# Comment to disable monitoring
beer_product_size = beer_size.find("td","size").get_text()
beer_product_price = beer_size.find("td","price").get_text()
beer_product_size = beer_size.find("td", "size").get_text()
beer_product_price = beer_size.find("td", "price").get_text()

# check if product exists
# NOTE: used this custom solution because django get_or_create
# doesn't play nice with custom primary keys
try:
product_entry = Product.objects.get(product_id=int(beer_product_id.strip()))
product_entry = Product.objects.get(product_id=int(beer_product_id.strip()))
except:
product_entry = Product()

Expand All @@ -85,7 +83,7 @@ def handle(self, *args, **options):
# NOTE: this code was created befor the beer store redesign
# it still works but some items no longer exist so they were
# temporarily omitted from the serializer
for key, value in beer_details.iteritems():
for key, value in beer_details.items():
attr = key.get_text()[:-1]
val = value.get_text()

Expand Down
34 changes: 16 additions & 18 deletions products/management/commands/get_stores.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import json
import urllib2

import requests
from bs4 import BeautifulSoup
from products.models import Store
from django.core.management.base import BaseCommand
from django.template import Template, Context
from django.conf import settings
from url_settings import TOP_URL
from .url_settings import TOP_URL


class Command(BaseCommand):
Expand All @@ -14,12 +13,12 @@ def handle(self, *args, **options):
scrapes and stores store information
"""
# get location json
data = json.load(urllib2.urlopen(TOP_URL+'/storelocations.json'))
data = json.loads(requests.get(TOP_URL + '/storelocations.json').content.decode())
data = data['features']

# loop through each location
for d in data:

# get json properties
store = d['properties']
coords = d['geometry']['coordinates']
Expand All @@ -28,15 +27,14 @@ def handle(self, *args, **options):
link = store['description']

# go to store page
link_soup = BeautifulSoup(link)
link_soup = BeautifulSoup(link, "html.parser")
store_link = link_soup.a['href']
store_html = urllib2.urlopen(TOP_URL+store_link)

store_html = requests.get(TOP_URL + store_link).content

# get store info
store_soup = BeautifulSoup(store_html)
store_soup = BeautifulSoup(store_html, "html.parser")
brs = store_soup.find("div", "store-address-container").div.find_all('br')

store_address = brs[0].previous_sibling
store_city_postal = store_address.next_sibling.next_sibling
store_phone = store_city_postal.next_sibling.next_sibling
Expand All @@ -48,20 +46,20 @@ def handle(self, *args, **options):

# get hours
try:
store_table_hours = store_soup.find("table", "store-hours").tbody.find_all("tr")
hours = []
store_table_hours = store_soup.find("table", "store-hours").tbody.find_all("tr")
hours = []

for store_hour in store_table_hours:
hours.append(store_hour.find_all("td")[1].get_text())
hours.append(store_hour.find_all("td")[2].get_text())
hours.append(store_hour.find_all("td")[1].get_text())
hours.append(store_hour.find_all("td")[2].get_text())
# if store page is outdated (ie store doesn't exist) skip it
except:
continue

# make a dictionary of date and hours
days = ["monday-open", "monday-close",
"tuesday-open", "tuesday-close",
"wednesday-open", "wednesday-close",
days = ["monday-open", "monday-close",
"tuesday-open", "tuesday-close",
"wednesday-open", "wednesday-close",
"thursday-open", "thursday-close",
"friday-open", "friday-close",
"saturday-open", "saturday-close",
Expand Down
4 changes: 2 additions & 2 deletions products/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.core.urlresolvers import reverse
from django.db import models
from django.urls import reverse


class Store(models.Model):
Expand Down Expand Up @@ -31,7 +31,7 @@ class Store(models.Model):

def __unicode__(self):
return self.name

def get_absolute_url(self):
return reverse("store_detail", kwargs={"store_id": self.store_id})

Expand Down
108 changes: 54 additions & 54 deletions products/urls.py
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
from django.conf.urls import patterns, url
from django.conf.urls import url
from products import views

urlpatterns = patterns("",
urlpatterns = [

# /stores
url(
regex=r"^stores/$",
view=views.stores,
name="beer_store_api"
),
# /stores
url(
regex=r"^stores/$",
view=views.stores,
name="api-stores"
),

# /stores/{store_id}
url(
regex=r"^stores/(?P<store_id>[0-9]+)/$",
view=views.store_by_id,
name="beer_store_api"
),
# /stores/{store_id}
url(
regex=r"^stores/(?P<store_id>[0-9]+)/$",
view=views.store_by_id,
name="api-stores-id"
),

# /products
url(
regex=r"^products/$",
view=views.products,
name="beer_store_api"
),
# /products
url(
regex=r"^products/$",
view=views.products,
name="api-products"
),

# /beers
url(
regex=r"^beers/$",
view=views.beers,
name="beer_store_api"
),
url(
regex=r"^beers/$",
view=views.beers,
name="api-beers"
),

# /beers/{product_id}
url(
regex=r"^beers/(?P<beer_id>[0-9]+)/$",
view=views.beer_by_id,
name="beer_store_api"
),
url(
regex=r"^beers/(?P<beer_id>[0-9]+)/$",
view=views.beer_by_id,
name="api-beers-id"
),

# /beers/{product_id}
url(
regex=r"^beers/(?P<beer_id>[0-9]+)/products/$",
view=views.beer_products,
name="beer_store_api"
),
url(
regex=r"^beers/(?P<beer_id>[0-9]+)/products/$",
view=views.beer_products,
name="api-beers-products"
),

# /products/{product_id}
url(
regex=r"^products/(?P<product_id>[0-9]+)/$",
view=views.product_by_id,
name="beer_store_api"
),
# /products/{product_id}
url(
regex=r"^products/(?P<product_id>[0-9]+)/$",
view=views.product_by_id,
name="api-products-id"
),

# /stores/{store_id}/products
url(
regex=r"^stores/(?P<store_id>[0-9]+)/products/$",
view=views.products_at_store,
name="beer_store_api"
),
# /stores/{store_id}/products
url(
regex=r"^stores/(?P<store_id>[0-9]+)/products/$",
view=views.products_at_store,
name="api-stores-products"
),

# /products/{product_id}/stores
url(
regex=r"^products/(?P<product_id>[0-9]+)/stores/$",
view=views.stores_with_product,
name="beer_store_api"
),
)
# /products/{product_id}/stores
url(
regex=r"^products/(?P<product_id>[0-9]+)/stores/$",
view=views.stores_with_product,
name="api-products-stores"
),
]

0 comments on commit 77d5e28

Please sign in to comment.