def test_setup(**kwargs): from random import choice from django.contrib.auth.models import User from snapboard.models import Thread, Post, Category, UserProfile from snapboard import sampledata if not settings.DEBUG: return if Thread.objects.all().count() > 0: # return, since there seem to already be threads in the database. return # ask for permission to create the test msg = """ You've installed SNAPboard with DEBUG=True, do you want to populate the board with random users/threads/posts to test-drive the application? (yes/no): """ populate = raw_input(msg).strip() while not (populate == "yes" or populate == "no"): populate = raw_input("\nPlease type 'yes' or 'no': ").strip() if populate == "no": return # create 10 random users users = ('john', 'sally', 'susan', 'amanda', 'bob', 'tully', 'fran') for u in users: user = User.objects.get_or_create(username=u) profile = UserProfile.objects.get_or_create(user=user[0], website='abc.com') # user.is_staff = True cats = ('Random Topics', 'Good Deals', 'Skiing in the Vermont Area', 'The Best Restaurants') for c in cats: cat = Category.objects.get_or_create(label=c) # create up to 30 posts tc = range(1, 50) for i in range(0, 35): print 'thread ', i, 'created' cat= choice(Category.objects.all()) subj = choice(sampledata.objects.split('\n')) thread = Thread(subject=subj, category=cat) thread.save() for j in range(0, choice(tc)): text = '\n\n'.join([sampledata.sample_data() for x in range(0, choice(range(2, 5)))]) # create a post post = Post( user=choice(User.objects.all()), thread=thread, text=text, ip='.'.join([str(choice(range(1,255))) for x in (1,2,3,4)]), ) # allows setting of arbitrary ip post.management_save()
def test_setup(**kwargs): from random import choice from django.contrib.auth.models import User from snapboard.models import Thread, Post, Category from snapboard import sampledata if not settings.DEBUG: return if Thread.objects.all().count() > 0: # return, since there seem to already be threads in the database. return # ask for permission to create the test msg = """ You've installed SNAPboard with DEBUG=True, do you want to populate the board with random users/threads/posts to test-drive the application? (yes/no): """ populate = raw_input(msg).strip() while not (populate == "yes" or populate == "no"): populate = raw_input("\nPlease type 'yes' or 'no': ").strip() if populate == "no": return # create 10 random users users = ('john', 'sally', 'susan', 'amanda', 'bob', 'tully', 'fran') for u in users: user = User.objects.get_or_create(username=u) # user.is_staff = True cats = ('Random Topics', 'Good Deals', 'Skiing in the Vermont Area', 'The Best Restaurants') for c in cats: cat = Category.objects.get_or_create(label=c) # create up to 30 posts tc = range(1, 50) for i in range(0, 35): print 'thread ', i, 'created' cat= choice(Category.objects.all()) subj = choice(sampledata.objects.split('\n')) thread = Thread(subject=subj, category=cat) thread.save() for j in range(0, choice(tc)): text = '\n\n'.join([sampledata.sample_data() for x in range(0, choice(range(2, 5)))]) # create a post post = Post( user=choice(User.objects.all()), thread=thread, text=text, ip='.'.join([str(choice(range(1,255))) for x in (1,2,3,4)]), ) # allows setting of arbitrary ip post.management_save()
def make_random_post_tree(parent_post, amount, thread, ratio=0.5): text = '\n\n'.join([sampledata.sample_data() for x in range(0, randrange(1, 6))]) # the post data postdata = { "user": choice(User.objects.all()), "thread": thread, "text": text, "ip": '.'.join([str(choice(range(2, 254))) for x in xrange(4)]), } # Create a post in tree. if parent_post is None: # Root node. post = Post.add_root(**postdata) else: post = parent_post.add_child(**postdata) # allows setting of arbitrary ip post.save() posts_remain = amount - 1 # Minus just-created one # ... got better ideas? # (got non-uniform random distributions?) # Anyway, ratio ~= depth/width. Don't delegate too few / too much to # each child depending on the ratio. fmin = lambda ratio, posts_remain: int(posts_remain * (ratio - 0.5) * 2) + 1 if ratio > 0.5 else 1 fmax = lambda ratio, posts_remain: int(posts_remain * ratio * 2) + 2 \ if ratio < 0.5 else posts_remain + 1 while posts_remain > 0: # Distribute remnants next_tree_posts = randrange(fmin(ratio, posts_remain), fmax(ratio, posts_remain)) #print(" D: delegating %d, %d remain " % (xx, x-xx)) make_random_post_tree(post, next_tree_posts, thread) posts_remain -= next_tree_posts
def ptpost(request, asin, boardid = 4): boardid = int(boardid) from awsProductCrawler import get_or_create_product, write_trace user = User.objects.get(username="******") p, created, pr, pr_created, pr_a, pr_a_created = get_or_create_product(asin, user, 'ASIN') price = None if pr_a and pr_a_created: price = pr_a elif pr_created: price = pr if not price: if p: if request.user.is_authenticated(): request.user.message_set.create(message="The price has not yet been changed") return HttpResponseRedirect(reverse('product_detail', args=(p.slug,))) else: raise Http404 from django.template import loader from templatetags.product_tags import price_post_body, price_post_subject from snapboard.models import Post, Board from snapboard.views import post_article_to_pt t = loader.get_template("product/price_post_body.html") ts = loader.get_template("product/price_post_subject.html") board = get_object_or_404(Board, pk=boardid) # create the post post = Post( subject = ts.render(RequestContext(request, price_post_subject(price))), user = user, board = board, text = t.render(RequestContext(request, price_post_body(price))), ) post.save() # post to P&T - commented due to imported article's user does not appear to match current user (level) category = 'Other' try: category = p.categories.all()[0].name except: pass post_article_to_pt(post, category, boardid) # redirect to new thread return HttpResponseRedirect(reverse('snapboard_post', args=(post.id,)))
def _make_testtrees(maxdepth=512, **kwargs): user = User.objects.all()[0] thr = Thread(subject="Test trees.", category=Category.objects.all()[0]) thr.save() top_post = Post.add_root(user=user, thread=thr, text="subj.") try: post = top_post print "lin" for curn in xrange(2, maxdepth): post = post.add_child(text="#1 at %d (lin)" % curn, user=post.user, thread=post.thread) print curn except Exception: traceback.print_exc() try: post = top_post print "dbl" for curn in xrange(2, maxdepth): post1 = post.add_child(text="#1 at %d (dbl)" % curn, user=post.user, thread=post.thread) post2 = post.add_child(text="#2 at %d (dbl)" % curn, user=post.user, thread=post.thread) post = post1 print curn except Exception: traceback.print_exc() try: post = top_post print "rnd" for curn in xrange(2, maxdepth): post1 = post.add_child(text="#1 at %d (rnd)" % curn, user=post.user, thread=post.thread) post2 = post.add_child(text="#2 at %d (rnd)" % curn, user=post.user, thread=post.thread) post1.save() post2.save() post = post1 if random.choice([True, False]) else post2 print curn except Exception: traceback.print_exc()