def detail(request): """Creates a prompt object using form input TODO - (defer) move post functionality into Prompt Form TODO - (defer) separate form processing into function """ template = "prompt/prompt_detail.html" tag_file = file(SITE_ROOT + '/prompt/tags.txt') tags = [tag.strip() for tag in tag_file] if request.method == 'POST': values = request.POST.copy() form = PromptForm(values, owner=request.user) if form.is_valid(): current_prompt=form.save() for tag in tags: if values.get(tag,None): current_prompt.tags.create(tag=tag) message = "%s\n%s\n%s\n%d" % (current_prompt.name, current_prompt.truth, current_prompt.dare, current_prompt.difficulty) # try to send mail. If it fails print out an error if not DEBUG: try: send_mail('Private Prompt Created', message, "*****@*****.**", [SERVER_EMAIL], fail_silently=False) except: print "Error: could not send mail to admins" return HttpResponseRedirect("/prompt/") else: errors=form.errors else: form = PromptForm() return locals()
def test_promptDuplicateName(self): """Tests that making two prompts with the same name gives an error """ form = PromptForm({'name': 'TestName', 'truth': 'TestTruth', 'dare': 'TestDare', 'difficulty': 1}, owner=self.user) if form.is_valid(): current_prompt=form.save(commit=False) current_prompt.owner=self.user current_prompt.save() prompt = Prompt.objects.get(name = "TestName") self.failUnlessEqual(prompt.truth, "TestTruth") self.failUnlessEqual(prompt.dare, "TestDare") self.failUnlessEqual(prompt.difficulty, 1) form = PromptForm({'name': 'TestName', 'truth': 'TestTruth', 'dare': 'TestDare', 'difficulty': 1}, owner=self.user) if form.is_valid(): self.assertRaises(forms.ValidationError, form.save)
def test_createOutOfBounds(self): """Test game creation with data out of valid bounds """ upper_bound = { 'name': 'Test Dare', 'truth': 'Tell something embarassing', 'dare': 'do something embarassing', 'difficulty': 11, 'owner': self.user, } lower_bound = upper_bound.copy() lower_bound['difficulty'] = 0 non_int_bound = upper_bound.copy() non_int_bound['difficulty'] = "bunny" bounds = [upper_bound, lower_bound, non_int_bound] for bound in bounds: form = PromptForm(bound, owner=self.user) self.failUnless(not form.is_valid())
def test_promptUniqueName(self): """Tests that making two prompts with different names does not give an error """ form = PromptForm({'name': 'TestName', 'truth': 'TestTruth', 'dare': 'TestDare', 'difficulty': 1}, owner=self.user) if form.is_valid(): current_prompt=form.save() prompt = Prompt.objects.get(name = "TestName") self.failUnlessEqual(prompt.truth, "TestTruth") self.failUnlessEqual(prompt.dare, "TestDare") self.failUnlessEqual(prompt.difficulty, 1) form = PromptForm({'name': 'TestName2', 'truth': 'TestTruth', 'dare': 'TestDare', 'difficulty': 1}, owner=self.user) if form.is_valid(): current_prompt=form.save() prompt = Prompt.objects.get(name = "TestName2") self.failUnlessEqual(prompt.truth, "TestTruth") self.failUnlessEqual(prompt.dare, "TestDare") self.failUnlessEqual(prompt.difficulty, 1)
sys.path.insert(0, dirname(os.getcwd())) os.environ["DJANGO_SETTINGS_MODULE"] = "tod.settings" from django.contrib.auth.models import User import csv from tod.prompt.forms import PromptForm from tod.prompt.models import Prompt reader = csv.DictReader(open("prompts.csv")) print Prompt.objects.all().count() tag_file = file("prompt/tags.txt") tags = [tag.strip() for tag in tag_file] me = User.objects.get(username="******") for row in reader: prompt_form = PromptForm(data=row, owner=me) # get a list of tags or an empty list, if no tag_list is provided tag_values = row["tag_list"].split(",") if row["tag_list"] else [] if prompt_form.is_valid(): # save the prompt if the form is valid current_prompt = prompt_form.save() # loop over the available tags and see if they are specified for tag in tags: if tag in tag_values: # if a tag is specified, add it to the prompt current_prompt.tags.create(tag=tag) else: print row print prompt_form.errors print Prompt.objects.all().count()