def index(request): verses = Verse.objects.all() if request.method == "POST": verse_data = request.POST['new_verse'] if request.user.is_authenticated() and request.user.author_profile.count() != 0: author = request.user.get_profile() verse = Verse() verse.author = author verse.verse_text = verse_data.replace(u' ',' ') # Replacing non-breaking space with space verse.save() return HttpResponseRedirect(reverse("verse_detail", args=[verse.id])) else: return HttpResponseNotAllowed(['GET']) return render_to_response('index.html', {'verses': verses}, context_instance = RequestContext(request))
def to_python(self, value): if value is None or type(value) is Verse: return value try: return Verse(value) except (RangeError, Exception) as err: raise forms.ValidationError(err.__str__())
def clean(self, value): """Form field for custom validation entering verses""" try: verse = Verse(value) except (RangeError, Exception) as err: raise forms.ValidationError(err.__str__()) # return the cleaned and processed data return str(verse)
def names(request): verses = Verse.category().instance.all() authors = Author.category().instance.all() years = Year.category().instance.all() auth_names = [author.name for author in Author.category().instance.all()] year_list = [year.year for year in Year.category().instance.all()] if request.method == 'GET': return render(request, 'jinja2/names.html', {'verses': verses, 'authors': authors, 'years': years}) elif request.method == 'POST': if request.POST.get('name') and request.POST.get('text'): verse = Verse( name=unicode(request.POST['name']), text=unicode(request.POST['text'])).save() if request.POST.get('author'): if unicode(request.POST.get('author')) in auth_names: verse.author.connect( Author.index.get(name=request.POST['author'])) else: Author(name=unicode(request.POST['author'])).save() verse.author.connect( Author.index.get(name=unicode(request.POST['author']))) if request.POST.get('year'): if int(request.POST.get('year')) in year_list: verse.year.connect( Year.index.get(year=int(request.POST['year']))) else: Year(year=request.POST['year']).save() verse.year.connect( Year.index.get(year=int(request.POST['year']))) verses = Verse.category().instance.all() authors = Author.category().instance.all() years = Year.category().instance.all() return render(request, 'jinja2/names.html', {'verses': verses, 'authors': authors, 'years': years})
def authors(request): verses = Verse.category().instance.all() authors = Author.category().instance.all() years = Year.category().instance.all() if request.method == 'GET': return render(request, 'jinja2/index.html', {'verses': verses, 'authors': authors, 'years': years}) elif request.method == 'POST': if request.POST.get('author_name'): Author(name=unicode(request.POST['author_name'])).save() authors = Author.category().instance.all() return render(request, 'jinja2/index.html', {'verses': verses, 'authors': authors, 'years': years})
def years(request): verses = Verse.category().instance.all() authors = Author.category().instance.all() years = Year.category().instance.all() if request.method == 'GET': years = Year.category().instance.all() return render(request, 'jinja2/years.html', {'verses': verses, 'authors': authors, 'years': years}) elif request.method == 'POST': if request.POST.get('year') is not None: Year(year=unicode(request.POST['year'])).save() years = Year.category().instance.all() return render(request, 'jinja2/years.html', {'verses': verses, 'authors': authors, 'years': years})
def test_set_relationship_with_verse(self): """ Test that the relationship between set and verse is correctly established """ my_set = Set.query.get(self.set1Id) verse1 = Verse.query.get(self.v1Id) verse2 = Verse.query.get(self.v2Id) self.assertIn(verse1, my_set.verses) self.assertIn(verse2, my_set.verses) verse3 = Verse(reference="ref3", verse="v3") my_set.verses.append(verse3) db.session.commit() self.assertEqual(len(my_set.verses), 3) self.assertIn(verse3, my_set.verses)
def index(request): verses = Verse.objects.all() if request.method == "POST": verse_data = request.POST['new_verse'] if request.user.is_authenticated( ) and request.user.author_profile.count() != 0: author = request.user.get_profile() verse = Verse() verse.author = author verse.verse_text = verse_data.replace( u' ', ' ') # Replacing non-breaking space with space verse.save() return HttpResponseRedirect( reverse("verse_detail", args=[verse.id])) else: return HttpResponseNotAllowed(['GET']) return render_to_response('index.html', {'verses': verses}, context_instance=RequestContext(request))
def setUp(self): """Create test client, add sample data.""" User.query.delete() Set.query.delete() Verse.query.delete() u1 = User(first_name="fn1", last_name="ln1", email="*****@*****.**", username="******", password="******") db.session.add(u1) db.session.commit() verse1 = Verse(reference="ref1", verse='v1') verse2 = Verse(reference="ref2", verse="v2") db.session.add(verse1, verse2) db.session.commit() # Set 1 belonging to a user set1 = Set( name="testset1", description="description1", user_id=u1.id, created_at=None, ) db.session.add(set1) db.session.commit() u1.sets.append(set1) db.session.commit() # Set2 that is favorited by the user set2 = Set( name="testset2", description="description2", user_id=u1.id, ) u1.sets.append(set2) u1.favorites.append(set2) db.session.commit() # Add the verses to the set set1.verses = [verse1, verse2] db.session.commit() # Put user, set and verse ids on self self.u1Id = u1.id self.set1Id = set1.id self.set2Id = set2.id self.v1Id = verse1.id self.v2Id = verse2.id self.client = app.test_client()