def setUp(self): ''' Build the necessary data''' # Instantiate idea self.idea = ideaModel(title="great idea", pub_date=datetime.datetime.utcnow(), description="here's a great idea", num_backers=1, likes=1, dislikes=0, classification="unclassified", headers="headers here;and here") self.idea.save() # Get hold of some test words testsPath = os.path.dirname(os.path.abspath(__file__)) nouns = open(os.path.join(testsPath, 'resource_nouns.txt'), 'r') self.words = [line.replace('\r\n', '') for line in nouns.readlines()] # Put every 50th word into a list (cross section of start letters) self.orderedWords = [self.words[i] for i in range(0,len(self.words)-1, 50)] self.shuffleWords = copy.copy(self.orderedWords) # Jumble the words into a new list random.shuffle(self.shuffleWords) # Save them against the current object for sWord in self.shuffleWords: self.idea.tags.add(sWord) self.idea.save()
def testdistinctTagsSortedCount(self): """ Test function to produce list of count, then alphabetically sorted tags """ # Instantiate idea idea = ideaModel(title="great idea 2", pub_date=datetime.datetime.utcnow(), description="here's a great idea 2", num_backers=1, likes=1, dislikes=0, classification="unclassified", headers="headers here;and here") idea.save() # Create a list that contains all of the ordered words and their initial count counts = [[wd, 1] for wd in self.orderedWords] # Loop through the first 10 items in the ordered list and add a tag for i in range(20): word = self.orderedWords[i] idea.tags.add(word) idea.save() for x in range(len(counts)-1): if counts[x][0] == word: counts[x][1] += 1 counts.sort(key=operator.itemgetter(1), reverse=True) # Call function countSortedTags = distinctTagsSortedCount() self.assertEquals(countSortedTags, counts)
def saveIdea(ideaTitle, ideaText, ideaClassification, ideaHeaders): ''' Processes idea form data and saves data ''' #import pdb #pdb.set_trace() out = ideaModel(idea_title = ideaTitle, pub_date = getDate(), idea_text = ideaText, num_backers = 1, idea_classification = ideaClassification, idea_headers = ideaHeaders) out.save() return
def saveIdea(ideaTitle, ideaText, ideaClassification, ideaHeaders): ''' Processes idea form data and saves data ''' #import pdb #pdb.set_trace() out = ideaModel(title = ideaTitle, pub_date = getDate(), description = ideaText, num_backers = 1, classification = ideaClassification, headers = ideaHeaders, likes=0.0, dislikes=0.0) out.save() return out
def submit(request): ''' Contact form rendering & submission. ''' c = {"classification":"unclassified", "page_title":"Share your idea"} c.update(csrf(request)) # Has the form been submitted? if request.method == 'POST': form = ideaForm(request.POST) if form.is_valid(): # Instantiate an idea idea = ideaModel() # Proper Header and django-based user headers = request.META #user = str(request.user) # Form content extracted cleanForm = form.cleaned_data ''' idea.idea_title = cleanForm['title'] idea.idea_text = cleanForm['description'] idea.idea_classification = cleanForm['cls'] ''' idea_headers = formatHttpHeaders(headers) res = saveIdea(cleanForm['title'],cleanForm['description'],cleanForm['cls'], idea_headers) #idea.email_starter = formatSubmitterEmail(user) # For the output page c['title'] = idea.idea_title c["description"] = idea.idea_text c['classification'] = idea.idea_classification return render_to_response('ideasapp/idea_thanks.html', c) else: logging.error("User failed to enter valid content into form.") c['form'] = form return render_to_response("ideasapp/idea_submit.html", c) else: form = ideaForm() c.update({"form":form}) return render_to_response("ideasapp/idea_submit.html", c)