In [1]:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'stamped_project.settings'
from django.shortcuts import *
from stamped.models import *
In [2]:
from django.contrib.auth.models import User
In [3]:
from django.contrib.auth.models import Group, Permission
In [12]:
u = User.objects.get(pk=8)
u
Out[12]:
In [64]:
r = Restaurant.objects.get(pk=1)
In [71]:
r = Review.objects.get(pk=1)
In [74]:
r.restaurant.address
Out[74]:
In [76]:
c = Comment.objects.get(pk=1)
In [ ]:
c.review.restaurant
In [68]:
r.get_category_display()
Out[68]:
In [59]:
p = Permission.objects.get(codename='add_restaurant')
In [85]:
u.has_perm(Permission.objects.get(codename='add_restaurant'))
Out[85]:
In [91]:
Permission.permission_required
In [13]:
u.has_perm('stamped.add_restaurant')
Out[13]:
In [92]:
u.username
Out[92]:
In [62]:
u.user_permissions.add(p)
In [14]:
r = Restaurant.objects.get(pk=1)
In [14]:
Restaurant.objects.order_by("date_added")[:5]
Out[14]:
In [15]:
get_object_or_404(Restaurant, name=r.name, address=r.address)
Out[15]:
In [5]:
s_count = Restaurant.objects.get(pk=1).stamped_out_count
u_count = User.objects.count()
In [6]:
s_count
Out[6]:
In [7]:
u_count
Out[7]:
Decay fucntion:
In [ ]:
import random
In [86]:
choices = [[1,91], [0,9]]
In [99]:
def decay_choice(choices, val):
#for each additional 10,000 users, remove a percentage
#chance for a single user to get the add_restaurant
#privlage stops assigning admins at 1,000,000 users
#where app should be self sufficent
penalty = val // 10000
#control for values that might exceed 100
if penalty >= 100:
penalty = 100
print penalty
choices[0][1] -= penalty
choices[0][1] += penalty
#get a random number from a uniform distribution
r = random.uniform(0, 100)
if r <= choices[0][1]:
return choices[0][0]
elif r <= choices[1][1]:
return choices[1][0]
In [97]:
decay_choice(choices=choices, val=100000)
Out[97]:
In [67]:
Out[67]:
In [38]:
weighted_choice(choices)
Out[38]:
In [10]:
user.has_perm('add_resturant')
Out[10]:
In [20]:
r = Review.objects.get(pk='1')
r.avg_rating
Out[20]:
In [22]:
r = Restaurant.objects.get(pk=1)
In [23]:
r.stamped_out_count
Out[23]:
Basic Test Cases:
In [2]:
import datetime
d = datetime.datetime(2013,3,5)
m = {'d': d}
In [3]:
print m
In [4]:
import datetime
In [5]:
d = datetime.datetime.now() - datetime.datetime(2008,2,1)
Post Tests
In [8]:
from django.test.client import Client
c = Client()
In [25]:
response = c.post('/results/',{'name': 'Fisssh', 'address': '280 Bleecker St'})
In [26]:
response.status_code
Out[26]:
Restaurant Results View Rendering:
In [8]:
Restaurant.objects.filter(category=category[0]).order_by('_rating')[:5]
In [ ]:
Restaurant.objects.filter(name='Fissh', address='280 Bleecker St')
In [ ]:
restaurant = get_object_or_404(Restaurant, name='Fish', address='280 Bleecker St')
In [ ]:
restaurant
In [ ]:
restaur
In [ ]:
restaurant.name
In [ ]:
restaurant.profile_picture.url
In [ ]:
restaurant.address
In [ ]:
restaurant.date_added
In [ ]:
r = restaurant.review_set.all()
In [ ]:
r = r[0]
In [ ]:
r.content
In [ ]:
r.user.get_full_name()
In [ ]:
r.date_added
In [ ]:
r.user.user_meta.profile_picture.url
In [ ]:
r.rating #could make rating a custom tag as well
In [ ]:
c = r.comment_set.all()
In [ ]:
c = c[0]
In [ ]:
c.user.get_full_name()
In [ ]:
c.content
In [ ]:
c.date_added
Dashboard Tests;
In [ ]:
Restaurant.objects.order_by("date_added")
In [ ]:
Review.objects.order_by("date_added")
In [5]:
from django.contrib.auth import authenticate, login
In [6]:
username = 'agconti'
password = 'password'
In [7]:
user = authenticate(username=username, password=password)
Review.objects.filter(user=user).order_by("date_added")
Out[7]:
In [ ]:
#^will give reviews and resturants choiceset #addd categories with @
In [2]:
import random
In [3]:
category_choices = [
('asian', 'Asian'),
('american', 'American'),
('bar_food', 'Bar Food'),
('seafood', 'Seafood'),
('sandwiches', 'Sandwiches'),
('french', 'French'),
('comfortfood', 'Comfort Food'),
('suhi', 'Sushi Bar'),
('unknown_cat', 'To cool to be defined')
]
In [4]:
category_choices
Out[4]:
In [5]:
category = random.choice(category_choices)
top_5 = Restaurant.objects.filter(category=category[0]).order_by('rating')[:5]
print category
In [4]:
In [5]:
Out[5]:
In [17]:
In [18]:
_._rating
In [ ]:
## forgot asian resturants!!!
In [ ]:
r = top_5[0]
In [ ]:
str(r.category)
Calculated Model Tests:
In [ ]:
u = User.objects.get(pk=1)
In [ ]:
u_r = Review.objects.filter(user=u).aggregate(Avg('rating'))
In [ ]:
from django.db.models import Avg
u_r
In [ ]:
u_r
In [ ]:
Review.objects.filter(user=u)
In [ ]:
r = Restaurant.objects.get(pk=1)
In [ ]:
Review.objects.filter(restaurant=r).aggregate(Avg('rating'))['rating__avg']
Get Histogram
In [ ]:
Review.objects.filter(user=u).values_list('rating', flat=True).order_by('_rating')
In [ ]:
hist = np.histogram(a, normed=1)
In [ ]:
hist
In [ ]:
plt.scatter(hist[0], hist[1])
In [ ]:
plt.scatter(a, arange(len(a)))
In [ ]: