def new_thread(request, forum, user): """ Given a POST dict, create a new thread, then pass whats left to the new_post function and then create the op. This function returns any errors, it returns None if there are no errors. """ threadform = ThreadForm(request.POST) result = Result() result.threadform = threadform captcha_error = get_captcha_error(request) if captcha_error: result.captcha_error = captcha_error result.postform = PostForm(request.POST) return result else: result.captcha_success = True if threadform.is_valid(): data = threadform.cleaned_data thread = Thread(title=data['title'], forum=forum) thread.save() result.thread = thread else: # error occured, return the form and the captcha error # (already set to result object) don't bother to try to add the post return result # all is valid, now make the op, skip captcha part # because we already checked it return new_post(request, thread, user, result=result)
def save(self, request): event = Event() start_date = self.cleaned_data['start_date'] start_time = self.cleaned_data['start_time'] end_time = self.cleaned_data['end_time'] event.start_time = datetime.combine(start_date, start_time) event.end_time = datetime.combine(start_date, end_time) event.event_name = self.cleaned_data['event_name'] event.event_location = self.cleaned_data['event_location'] event.event_organizer = self.cleaned_data['event_organizer'] event.event_description = self.cleaned_data['event_description'] event.event_website = self.cleaned_data['event_website'] event.save() acl = ACLUserEvent() acl.user = request.user acl.event = event acl.save() discussiondefs = ( ('PR', _(u'Discussion of the upcoming %s'), _(u'Discuss the upcoming event %s before it actually happens.')), ('LI', _(u'Live discussion of %s'), _(u'Discuss the ongoing event %s live.')), ('PO', _(u'Post-hoc discussion of %s'), _(u'Discuss %s after the facts.')) ) for s in discussiondefs: thread = Thread() thread.time = datetime.now() thread.user = request.user thread.event = event thread.thread_type = s[0]; thread.title = s[1] % (event.event_name) thread.description = s[2] % (event.event_name) thread.save()
def new_thread(request, directory_id): form = NewThreadForm(request.POST) if request.method == 'POST': if form.is_valid(): title = form.cleaned_data['title'] body = form.cleaned_data['body'] directory = Directory.objects.get(pk=directory_id) new_thread = Thread(name=title, creator=request.user, directory=directory) new_thread.save() new_post = Post(body=body, creator=request.user, index=1, thread=new_thread) #new_thread.latest_post_ref = new_post new_post.save() new_thread.directory = directory new_thread.latest_post = new_post new_thread.post_count = 1 new_thread.save() return HttpResponseRedirect(reverse('main:view_thread', args=[new_thread.pk])) return render(request, 'main/new_thread.html', {'form': form, 'directory_id': directory_id})
def test_setup(**kwargs): from django.contrib.auth.models import User from models import Thread, Post, Category from random import choice import chomsky 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(chomsky.objects.split("\n")) thread = Thread(subject=subj, category=cat) thread.save() for j in range(0, choice(tc)): text = "\n\n".join([chomsky.chomsky() 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 create_thread(request): newThread = Thread(); newThread.message = request.POST['message'] newThread.name = request.POST['title'] newThread.created_by = request.user forum_name = decode_name(request.POST['forum_name']) newThread.forum = Forum.objects.filter(name=forum_name)[0] #newThread.topic = Forum.objects.filter(name=str(thread_name).replace ("_", " ").replace (".qm.", "?"))[0] newThread.save() args = {} #return render_to_response('view_thread.html', args, context_instance=RequestContext(request)) return HttpResponseRedirect(request.POST['current_url'])
def save(self, request): event = Event() start_date = self.cleaned_data['start_date'] start_time = self.cleaned_data['start_time'] end_time = self.cleaned_data['end_time'] event.start_time = datetime.combine(start_date, start_time) event.end_time = datetime.combine(start_date, end_time) event.event_name = self.cleaned_data['event_name'] event.event_location = self.cleaned_data['event_location'] event.event_organizer = self.cleaned_data['event_organizer'] event.event_description = self.cleaned_data['event_description'] event.event_website = self.cleaned_data['event_website'] event.save() acl = ACLUserEvent() acl.user = request.user acl.event = event acl.save() discussiondefs = (( 'PR', _(u'Discussion of the upcoming %s'), _(u'Discuss the upcoming event %s before it actually happens.')), ('LI', _(u'Live discussion of %s'), _(u'Discuss the ongoing event %s live.')), ('PO', _(u'Post-hoc discussion of %s'), _(u'Discuss %s after the facts.'))) for s in discussiondefs: thread = Thread() thread.time = datetime.now() thread.user = request.user thread.event = event thread.thread_type = s[0] thread.title = s[1] % (event.event_name) thread.description = s[2] % (event.event_name) thread.save()
class NewThreadForm(forms.Form): error_css_class = 'in-error' thread_min_len = utils.get_config('min_thread_title_chars') post_min_len = utils.get_config('min_post_chars') title = forms.CharField(label='Title', max_length=1000, min_length=thread_min_len) content = forms.CharField(label='Post Body', min_length=post_min_len, widget=forms.Textarea()) forum = forms.ModelChoiceField(queryset=Forum.objects.all(), widget=forms.HiddenInput()) def __init__(self, *args, **kwargs): super(NewThreadForm, self).__init__(*args, **kwargs) self.thread = None self.post = None @transaction.atomic def save(self, author): self.thread = Thread(title=self.cleaned_data['title'], forum=self.cleaned_data['forum']) self.thread.save() self.post = Post(thread=self.thread, content=self.cleaned_data['content'], author=author) self.post.save() return self.thread
def ask(request): """ Request handler when someone posts a question 1. Add question content to the database 2. Select random active answerer 3. Put the question in the answerer's update stack 4. Send push notification to the answerer's device to retrieve updates """ if request.method == 'POST': json_data = json.loads(request.body) try: question_content = json_data['content'] asker_device_id = json_data['device_id'] max_new_threads = json_data['max_new_threads'] except KeyError: print "Error: A posted question did not have a JSON object with the required properties" else: # then add question to database question = Question(asker_device_id=asker_device_id, content=question_content) question.save() # We are going to start one or more threads with random devices, put the critical information about the thread in this array # and send it back to the device new_thread_ids = []; responder_ids = []; # then select a random device to send the question to all_devices = Device.objects.all() asker_device = all_devices.filter(device_id=asker_device_id)[0]; print "Found asker device" print asker_device print max_new_threads reasonable_number_of_tries = 0 while len(new_thread_ids) < max_new_threads: random_device = random.choice(all_devices) if len(all_devices) > 1 else None print "Chosing random device" print random_device # ensure that we've a valid answerer device if random_device is None: return while len(all_devices) > 1 and random_device.device_id == asker_device_id or random_device.device_id in responder_ids : if reasonable_number_of_tries < 5: random_device = random.choice(all_devices) reasonable_number_of_tries = reasonable_number_of_tries + 1 else: break if reasonable_number_of_tries >= 5: break print "Chose another random device" print random_device responder_ids.append(random_device.device_id) print "But I am" print asker_device_id # find a unique thread id # Start the thread between the asker device and the random device response_thread = Thread(question_id=question.id, asker_device=asker_device, answerer_device=random_device) response_thread.save() new_thread_ids.append(response_thread.id) print "response thread with id: " + str(response_thread.id) # add question to answerer_device update stack QuestionUpdates.add_update(random_device, question) ThreadUpdates.add_update(random_device, response_thread) new_threads = [] for i in range(0, len(new_thread_ids)): new_threads.append({'thread_id':new_thread_ids[i], 'responder_device_id':responder_ids[i]}) return HttpResponse(json.dumps({ 'question_id' : question.id, 'threads' : new_threads }), content_type="application/json")