def new(request): """ Create a new Pad, with 1 initial "TextArea" for a given choice. """ if request.method == 'POST': pad_form = PadForm(request.POST) if pad_form.is_valid(): pad_inst = pad_form.save(commit=False) title = pad_form.cleaned_data["title"] # TODO is there a reason that this is not factored out to the # forms.py? pad_inst.creator = request.user pad_inst.guid = create_pad_guid(title) pad_inst.save() textarea_inst = TextArea(pad=pad_inst, editor=request.user ) textarea_inst.save() return HttpResponseRedirect( pad_inst.get_absolute_url() ) # Redirect after POST else: pad_form = PadForm() args = {"pad_form":pad_form, "user":request.user} return create_object( request, form_class = PadForm, template_name = 'pads/new.html', extra_context = args, #login_required = True, )
def add(request, app_label, model_name, id): '''Add a pad to another object''' try: typ = ContentType.objects.get(app_label=app_label, model=model_name) obj = typ.get_object_for_this_type(id=id) except: return HttpResponseNotFound() if request.method == 'POST': pad_form = PadForm(request.POST) if pad_form.is_valid(): pad_inst = pad_form.save(commit=False) pad_inst.creator = request.user title = pad_form.cleaned_data["title"] pad_inst.guid = create_pad_guid(title) pad_inst.content_type = typ pad_inst.object_id = id pad_inst.save() textarea_inst = TextArea(pad=pad_inst, editor=request.user ) textarea_inst.save() return HttpResponseRedirect( pad_inst.get_absolute_url() ) else: pad_form = PadForm() return create_object( request, form_class = PadForm, template_name = 'pads/new.html', extra_context = locals(), )
def detail(request, creator=None, slug=None, guid=None): '''The Pad object_detail ''' #TODO: check if user is logged in, then enable chat, etc. if not guid: guid = create_pad_guid(slug) try: pad = Pad.objects.get(guid=guid) except Pad.DoesNotExist: raise Http404 #TODO: I want to enable multiple pads eventually. textareas = TextArea.objects.filter(pad=pad) if not textareas: textarea_a = TextArea( pad=pad, content="", editor=request.user ) textarea_a.save() else: textarea_a = textareas[0] #TODO: too tightly coupled to contrib.auth, clumsy ... if request.user == User.objects.get(username=creator): user_is_creator = True else: user_is_creator = False args = {"textarea_a":textarea_a, "user":request.user, 'textareas': textareas, "STOMP_PORT":settings.STOMP_PORT, "CHANNEL_NAME": guid, "HOST":settings.INTERFACE, "SESSION_COOKIE_NAME": settings.SESSION_COOKIE_NAME, "user_is_creator":user_is_creator, } return object_detail( request, queryset = Pad.objects.all(), object_id = pad.id, template_object_name = 'pad', template_name = 'pads/pad.html', extra_context = args, )
class PadCreateTestCase(unittest.TestCase): def setUp(self): self.user = User.objects.create_user("user1", "*****@*****.**", password="******") self.user.save() self.second_user = User.objects.create_user("user2", "*****@*****.**", password="******") self.second_user.save() self._question = "Is Foo the new Bar?" self._guid = create_pad_guid(self._question) self._content = "No, Foo will always be tops!" self.pad = Pad(guid=self._guid, creator=self.user, question=self._question) self.pad.save() self.textarea = TextArea(pad=self.pad, content=self._content, editor=self.user) self.textarea.save() def tearDown(self): User.objects.all().delete() def test_GetPad(self): testslug = "is-foo-the-new-bar" testguid = create_pad_guid(testslug) pad = Pad.objects.get(guid=testguid) textarea = pad.textarea_set.get(editor=self.user) self.assertEquals(self._question, pad.question) self.assertEquals(self.user, pad.creator) self.assertEquals(self._content, textarea.content) def test_AddNewTextAreabyNewUser(self): getpad = Pad.objects.get(guid=self._guid) new_textarea = TextArea(pad=getpad, editor=self.second_user, content="Bar will rise and defeat Foo!") new_textarea.save() def test_editTextArea(self): textarea = TextArea.objects.get(pad__guid=self._guid ) textarea.content = "Foo is best eva." textarea.save() revs = TextAreaRevision.objects.all() self.assertEquals(len(revs), 2) self.assertEquals(revs[0].content, textarea.content) self.assertEquals(revs[1].content, self._content)
def setUp(self): self.user = User.objects.create_user("user1", "*****@*****.**", password="******") self.user.save() self.second_user = User.objects.create_user("user2", "*****@*****.**", password="******") self.second_user.save() self._question = "Is Foo the new Bar?" self._guid = create_pad_guid(self._question) self._content = "No, Foo will always be tops!" self.pad = Pad(guid=self._guid, creator=self.user, question=self._question) self.pad.save() self.textarea = TextArea(pad=self.pad, content=self._content, editor=self.user) self.textarea.save()
def test_AddNewTextAreabyNewUser(self): getpad = Pad.objects.get(guid=self._guid) new_textarea = TextArea(pad=getpad, editor=self.second_user, content="Bar will rise and defeat Foo!") new_textarea.save()