Commit fe6b657f authored by Daniel W Bond's avatar Daniel W Bond
Browse files

remove functions from top of file, and import from newly created utils

parent ee60b4f0
......@@ -13,7 +13,6 @@ from django.template.loader import get_template
from django.template import Context
from django.utils.safestring import mark_safe
# third party imports
import requests
from PIL import Image
from braces.views import LoginRequiredMixin, SuperuserRequiredMixin
from braces.views import FormValidMessageMixin
......@@ -22,79 +21,11 @@ from ratelimit.decorators import ratelimit
from .models import Listing, Bid, Flag, BidFlag, Rating
from .forms import ListingForm, BidForm, FlagForm, BidFlagForm,\
ExchangeListingForm, UnExchangeListingForm, RatingForm
from .utils import ISBNMetadata, can_flag, can_flag_bid, flag_slug,\
bid_flag_slug, can_rate
from core.models import Student
# pulls worldcat metadata from ISBNs
def ISBNMetadata(standardISBN):
# passing in numbers starting with 0 throws "SyntaxError: invalid token"
url = "" +\
str(standardISBN) +\
# In case the API fails to return, simply return None.
metadata = requests.get(url, timeout=3)
except requests.ConnectionError:
return None
# format into a dictionary
dejson = metadata.json()
metadataDict = dejson.get('list')
return metadataDict[0]
return None
# flagging
# you can only flag a listing once...
def can_flag(flagger, listing):
user_flag_num = Flag.objects.filter(flagger=flagger,
# we're assuming that this isn't going to go over 1
if user_flag_num:
return False
return True
def can_flag_bid(flagger, bid):
user_flag_num = BidFlag.objects.filter(flagger=flagger,
if user_flag_num:
return False
return True
# get the listing's slug to pass to the create flag page
def flag_slug(flagger, listing):
if not can_flag(flagger, listing):
return Flag.objects.get(flagger=flagger, listing=listing).slug
return None
def bid_flag_slug(flagger, bid):
if not can_flag_bid(flagger, bid):
return BidFlag.objects.get(flagger=flagger, bid=bid).slug
return None
# rating
# (basically) duplicated code!!!
def can_rate(rater, listing):
user_rate_num = Rating.objects.filter(rater=rater,
# we're assuming that this isn't going to go over 1
if user_rate_num:
return False
return True
class ListListings(LoginRequiredMixin, ListView):
model = Listing
context_object_name = 'listings'
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment