Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
B
bookshare
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
30
Issues
30
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
Operations
Operations
Incidents
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
SRCT
bookshare
Commits
6e1e71f5
Commit
6e1e71f5
authored
Feb 24, 2020
by
Daniel W Bond
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
pulled global functions out of views file and put them in separate utils.py
parent
19acc4e5
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
72 additions
and
0 deletions
+72
-0
bookshare/trades/utils.py
bookshare/trades/utils.py
+72
-0
No files found.
bookshare/trades/utils.py
0 → 100644
View file @
6e1e71f5
import
requests
from
.models
import
Flag
,
Rating
,
BidFlag
# pulls worldcat metadata from ISBNs
def
ISBNMetadata
(
standardISBN
):
# passing in numbers starting with 0 throws "SyntaxError: invalid token"
url
=
"http://xisbn.worldcat.org/webservices/xid/isbn/"
+
\
str
(
standardISBN
)
+
\
"?method=getMetadata&format=json&fl=title,year,author,ed"
# In case the API fails to return, simply return None.
try
:
metadata
=
requests
.
get
(
url
,
timeout
=
3
)
except
requests
.
ConnectionError
:
return
None
# format into a dictionary
dejson
=
metadata
.
json
()
try
:
metadataDict
=
dejson
.
get
(
'list'
)
return
metadataDict
[
0
]
except
:
return
None
# flagging
# you can only flag a listing once...
def
can_flag
(
flagger
,
listing
):
user_flag_num
=
Flag
.
objects
.
filter
(
flagger
=
flagger
,
listing
=
listing
).
count
()
# we're assuming that this isn't going to go over 1
if
user_flag_num
:
return
False
else
:
return
True
def
can_flag_bid
(
flagger
,
bid
):
user_flag_num
=
BidFlag
.
objects
.
filter
(
flagger
=
flagger
,
bid
=
bid
).
count
()
if
user_flag_num
:
return
False
else
:
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
else
:
return
None
def
bid_flag_slug
(
flagger
,
bid
):
if
not
can_flag_bid
(
flagger
,
bid
):
return
BidFlag
.
objects
.
get
(
flagger
=
flagger
,
bid
=
bid
).
slug
else
:
return
None
# rating
# (basically) duplicated code!!!
def
can_rate
(
rater
,
listing
):
user_rate_num
=
Rating
.
objects
.
filter
(
rater
=
rater
,
listing
=
listing
).
count
()
# we're assuming that this isn't going to go over 1
if
user_rate_num
:
return
False
else
:
return
True
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment