def setUp( self ): self.database = Database( Connection_wrapper( sqlite.connect( ":memory:", detect_types = sqlite.PARSE_DECLTYPES, check_same_thread = False ) ), cache = Stub_cache(), ) self.database.execute_script( file( "model/schema.sqlite" ).read(), commit = True ) self.username = u"mulder" self.password = u"trustno1" self.email_address = u"*****@*****.**" self.user = User.create( self.database.next_id( User ), self.username, self.password, self.email_address ) self.database.save( self.user, commit = False ) self.trash = Notebook.create( self.database.next_id( Notebook ), u"trash" ) self.database.save( self.trash, commit = False ) self.notebook = Notebook.create( self.database.next_id( Notebook ), u"notebook", self.trash.object_id, user_id = self.user.object_id ) self.database.save( self.notebook, commit = False ) note_id = self.database.next_id( Note ) self.note1 = Note.create( note_id, u"<h3>my title</h3>blah", notebook_id = self.notebook.object_id, startup = True, user_id = self.user.object_id ) self.database.save( self.note1, commit = False ) note_id = self.database.next_id( Note ) self.note2 = Note.create( note_id, u"<h3>other title</h3>whee", notebook_id = self.notebook.object_id, user_id = self.user.object_id ) self.database.save( self.note2, commit = False )
def setUp( self ): Test_controller.setUp( self ) self.notebook = Notebook.create( self.database.next_id( Notebook ), u"my notebook", trash_id = u"foo" ) self.database.save( self.notebook ) self.anon_notebook = Notebook.create( self.database.next_id( Notebook ), u"Luminotes" ) self.database.save( self.anon_notebook ) self.anon_note = Note.create( self.database.next_id( Note ), u"<h3>my note</h3>", notebook_id = self.anon_notebook.object_id, ) self.database.save( self.anon_note ) self.login_note = Note.create( self.database.next_id( Note ), u"<h3>login</h3>", notebook_id = self.anon_notebook.object_id, ) self.database.save( self.login_note ) self.blog_notebook = Notebook.create( self.database.next_id( Notebook ), u"Luminotes blog" ) self.database.save( self.blog_notebook ) self.blog_note = Note.create( self.database.next_id( Note ), u"<h3>my blog entry</h3>", notebook_id = self.blog_notebook.object_id, ) self.database.save( self.blog_note ) self.guide_notebook = Notebook.create( self.database.next_id( Notebook ), u"Luminotes user guide" ) self.database.save( self.guide_notebook ) self.guide_note = Note.create( self.database.next_id( Note ), u"<h3>it's all self-explanatory</h3>", notebook_id = self.guide_notebook.object_id, ) self.database.save( self.guide_note ) self.privacy_notebook = Notebook.create( self.database.next_id( Notebook ), u"Luminotes privacy policy" ) self.database.save( self.privacy_notebook ) self.privacy_note = Note.create( self.database.next_id( Note ), u"<h3>yay privacy</h3>", notebook_id = self.privacy_notebook.object_id, ) self.database.save( self.privacy_note ) self.username = u"mulder" self.password = u"trustno1" self.email_address = u"*****@*****.**" self.user = None self.session_id = None self.user = User.create( self.database.next_id( User ), self.username, self.password, self.email_address ) self.database.save( self.user ) self.database.execute( self.user.sql_save_notebook( self.notebook.object_id ) ) self.anonymous = User.create( self.database.next_id( User ), u"anonymous" ) self.database.save( self.anonymous ) self.database.execute( self.anonymous.sql_save_notebook( self.anon_notebook.object_id, read_write = False, owner = False, rank = 0 ) ) self.database.execute( self.anonymous.sql_save_notebook( self.blog_notebook.object_id, read_write = False, owner = False, rank = 1 ) ) self.database.execute( self.anonymous.sql_save_notebook( self.guide_notebook.object_id, read_write = False, owner = False, rank = 2 ) ) self.database.execute( self.anonymous.sql_save_notebook( self.privacy_notebook.object_id, read_write = False, owner = False, rank = 3 ) )
def get_and_send_best_note(klass): notes = klass.get_notes_near_current_location() if notes: note = notes[0] # send SMS re note resp = TwilioHelper.send_sms_for_note(note.get(Note.A_TEXT)) if resp: note[Note.A_DT_LAST_SENT] = datetime.datetime.now() Note.create_or_update_note(note)
def test_export_html(self): note3 = Note.create("55", u"<h3>blah</h3>foo", notebook_id=self.notebook.object_id) self.database.save(note3) response_headers = {} expected_notes = (self.note1, self.note2, note3) result = invoke( "export", "html", self.database, self.notebook, expected_notes, response_headers, ) # response headers should be unchanged assert response_headers == {} notes = result.get("notes") assert len(notes) == len(expected_notes) # assert that the notes are in the expected order for (note, expected_note) in zip(notes, expected_notes): assert note.object_id == expected_note.object_id assert note.revision == expected_note.revision assert note.title == expected_note.title assert note.contents == expected_note.contents assert note.notebook_id == expected_note.notebook_id assert note.startup == expected_note.startup assert note.deleted_from_id == expected_note.deleted_from_id assert note.rank == expected_note.rank assert note.user_id == expected_note.user_id assert note.creation == expected_note.creation
def create_main_notebook( self ): # create the main notebook main_notebook_id = self.database.next_id( Notebook ) self.main_notebook = Notebook.create( main_notebook_id, u"Luminotes", rank = 0 ) self.database.save( self.main_notebook, commit = False ) # no need to create default notes for the desktop version if self.desktop is True: return # create an id for each note note_ids = {} for ( filename, startup ) in self.NOTE_FILES: note_ids[ filename ] = self.database.next_id( Note ) rank = 0 for ( filename, startup ) in self.NOTE_FILES: full_filename = os.path.join( self.HTML_PATH, filename ) contents = fix_note_contents( file( full_filename ).read(), main_notebook_id, note_ids, self.settings ) if startup: rank += 1 note = Note.create( note_ids[ filename ], contents, notebook_id = self.main_notebook.object_id, startup = startup, rank = startup and rank or None ) self.database.save( note, commit = False )
def create_thread( self, user_id ): """ Create a new forum thread with a blank post, and give the thread a default name. Then redirect to that new thread. @type user_id: unicode or NoneType @param user_id: id of current logged-in user (if any) @rtype dict @return { 'redirect': new_notebook_url } @raise Access_error: the current user doesn't have access to create a post @raise Validation_error: one of the arguments is invalid """ if user_id is None: raise Access_error() user = self.__database.load( User, user_id ) if user is None or not user.username or user.username == "anonymous": raise Access_error() anonymous = self.__database.select_one( User, User.sql_load_by_username( u"anonymous" ), use_cache = True ) if anonymous is None: raise Access_error() # for now, crappy hard-coding to prevent just anyone from creating a blog thread if self.__name == u"blog" and user.username != u"witten": raise Access_error() # create the new notebook thread thread_id = self.__database.next_id( Notebook, commit = False ) thread = Notebook.create( thread_id, self.DEFAULT_THREAD_NAME, user_id = user.object_id ) self.__database.save( thread, commit = False ) # associate the forum tag with the new notebook thread tag = self.__database.select_one( Tag, Tag.sql_load_by_name( u"forum", user_id = anonymous.object_id ) ) self.__database.execute( anonymous.sql_save_notebook_tag( thread_id, tag.object_id, value = self.__name ), commit = False, ) # give the anonymous user access to the new notebook thread self.__database.execute( anonymous.sql_save_notebook( thread_id, read_write = True, owner = False, own_notes_only = True ), commit = False, ) # create a blank post in which the user can start off the thread note_id = self.__database.next_id( Notebook, commit = False ) note = Note.create( note_id, u"<h3>", notebook_id = thread_id, startup = True, rank = 0, user_id = user_id ) self.__database.save( note, commit = False ) self.__database.commit() if self.__name == "blog": return dict( redirect = u"/blog/%s" % thread_id, ) return dict( redirect = u"/forums/%s/%s" % ( self.__name, thread_id ), )
def test_export_html(self): note3 = Note.create("55", u"<h3>blah</h3>foo", notebook_id=self.notebook.object_id) self.database.save(note3) response_headers = {} expected_notes = (self.note1, self.note2, note3) result = invoke("export", "html", self.database, self.notebook, expected_notes, response_headers) # response headers should be unchanged assert response_headers == {} notes = result.get("notes") assert len(notes) == len(expected_notes) # assert that the notes are in the expected order for (note, expected_note) in zip(notes, expected_notes): assert note.object_id == expected_note.object_id assert note.revision == expected_note.revision assert note.title == expected_note.title assert note.contents == expected_note.contents assert note.notebook_id == expected_note.notebook_id assert note.startup == expected_note.startup assert note.deleted_from_id == expected_note.deleted_from_id assert note.rank == expected_note.rank assert note.user_id == expected_note.user_id assert note.creation == expected_note.creation
def create_main_notebook(self): # create the main notebook main_notebook_id = self.database.next_id(Notebook) self.main_notebook = Notebook.create(main_notebook_id, u"Luminotes", rank=0) self.database.save(self.main_notebook, commit=False) # no need to create default notes for the desktop version if self.desktop is True: return # create an id for each note note_ids = {} for (filename, startup) in self.NOTE_FILES: note_ids[filename] = self.database.next_id(Note) rank = 0 for (filename, startup) in self.NOTE_FILES: full_filename = os.path.join(self.HTML_PATH, filename) contents = fix_note_contents( file(full_filename).read(), main_notebook_id, note_ids, self.settings) if startup: rank += 1 note = Note.create(note_ids[filename], contents, notebook_id=self.main_notebook.object_id, startup=startup, rank=startup and rank or None) self.database.save(note, commit=False)
def make_thread( self ): title = u"Welcome to the Luminotes %s forum!" % self.forum_name # create a notebook thread to go in the forum notebook_id = self.database.next_id( Notebook, commit = False ) thread_notebook = Notebook.create( notebook_id, title, ) self.database.save( thread_notebook, commit = False ) anonymous = self.database.select_one( User, User.sql_load_by_username( u"anonymous" ) ) # add a single welcome note to the new thread note_id = self.database.next_id( Note, commit = False ) note = Note.create( note_id, u""" <h3>%s</h3> You can discuss any Luminotes %s topics here. This is a public discussion forum, so please keep that in mind when posting. And have fun. """ % ( title, self.forum_name ), notebook_id, startup = True, rank = 0, user_id = anonymous.object_id, creation = datetime.now(), ) self.database.save( note, commit = False ) # load the forum tag, or create one if it doesn't exist tag = self.database.select_one( Tag, Tag.sql_load_by_name( u"forum", user_id = anonymous.object_id ) ) if not tag: tag_id = self.database.next_id( Tag, commit = False ) tag = Tag.create( tag_id, notebook_id = None, # this tag is not in the namespace of a single notebook user_id = anonymous.object_id, name = u"forum", description = u"discussion forum threads" ) self.database.save( tag, commit = False ) # associate the forum tag with the previously created notebook thread, and set that # association's value to forum_name self.database.execute( anonymous.sql_save_notebook_tag( notebook_id, tag.object_id, value = self.forum_name ), commit = False, ) # give the anonymous user access to the new notebook thread self.database.execute( anonymous.sql_save_notebook( notebook_id, read_write = True, owner = False, own_notes_only = True ), commit = False, )
def add_note(klass, lat, lon, text, max_distance_in_meters=None, resend_interval=None): doc = { Note.A_LAT:float(lat), Note.A_LON:float(lon), Note.A_TEXT:text } if max_distance_in_meters: doc[Note.A_REQ_MAX_DISTANCE] = float(max_distance_in_meters)/1000.0 if resend_interval: doc[Note.A_RESEND_INTERVAL] = resend_interval return Note.create_or_update_note(doc)
def setUp(self): self.database = Database( Connection_wrapper( sqlite.connect(":memory:", detect_types=sqlite.PARSE_DECLTYPES, check_same_thread=False)), cache=Stub_cache(), ) self.database.execute_script(file("model/schema.sqlite").read(), commit=True) self.username = u"mulder" self.password = u"trustno1" self.email_address = u"*****@*****.**" self.user = User.create(self.database.next_id(User), self.username, self.password, self.email_address) self.database.save(self.user, commit=False) self.trash = Notebook.create(self.database.next_id(Notebook), u"trash") self.database.save(self.trash, commit=False) self.notebook = Notebook.create(self.database.next_id(Notebook), u"notebook", self.trash.object_id, user_id=self.user.object_id) self.database.save(self.notebook, commit=False) note_id = self.database.next_id(Note) self.note1 = Note.create(note_id, u"<h3>my title</h3>blah", notebook_id=self.notebook.object_id, startup=True, user_id=self.user.object_id) self.database.save(self.note1, commit=False) note_id = self.database.next_id(Note) self.note2 = Note.create(note_id, u"<h3>other title</h3>whee", notebook_id=self.notebook.object_id, user_id=self.user.object_id) self.database.save(self.note2, commit=False)
def __make_notes( self ): note_id = self.database.next_id( Note, commit = False ) note = Note.create( note_id, u"<h3>foo</h3>bar", self.general_thread.object_id, startup = True, rank = 0, user_id = self.anonymous.object_id, creation = datetime.now(), ) self.database.save( note, commit = False ) self.note = note note_id = self.database.next_id( Note, commit = False ) note = Note.create( note_id, u"<h3>bar</h3>baz", self.general_thread.object_id, startup = True, rank = 0, user_id = self.anonymous.object_id, creation = datetime.now(), ) self.database.save( note, commit = False ) note_id = self.database.next_id( Note, commit = False ) note = Note.create( note_id, u"<h3>baz</h3>quux", self.general_thread.object_id, startup = True, rank = 0, user_id = self.anonymous.object_id, creation = datetime.now(), ) self.database.save( note, commit = False ) self.database.commit()
def __make_notes(self): note_id = self.database.next_id(Note, commit=False) note = Note.create( note_id, u"<h3>foo</h3>bar", self.general_thread.object_id, startup=True, rank=0, user_id=self.anonymous.object_id, creation=datetime.now(), ) self.database.save(note, commit=False) self.note = note note_id = self.database.next_id(Note, commit=False) note = Note.create( note_id, u"<h3>bar</h3>baz", self.general_thread.object_id, startup=True, rank=0, user_id=self.anonymous.object_id, creation=datetime.now(), ) self.database.save(note, commit=False) note_id = self.database.next_id(Note, commit=False) note = Note.create( note_id, u"<h3>baz</h3>quux", self.general_thread.object_id, startup=True, rank=0, user_id=self.anonymous.object_id, creation=datetime.now(), ) self.database.save(note, commit=False) self.database.commit()
def setUp( self ): self.object_id = u"17" self.title = u"title goes here" self.contents = u"<h3>%s</h3>blah" % self.title self.summary = None self.notebook_id = u"18" self.startup = False self.rank = 17.5 self.user_id = u"me" self.username = u"myname" self.creation = datetime.now() self.delta = timedelta( seconds = 1 ) self.note = Note.create( self.object_id, self.contents, self.notebook_id, self.startup, self.rank, self.user_id, self.username, self.creation, self.summary )
def update_note(self, filename, startup, main_notebook, note_ids, note=None): full_filename = os.path.join(self.HTML_PATH, filename) contents = fix_note_contents(file(full_filename).read(), main_notebook.object_id, note_ids, self.settings) if note: if note.contents == contents: return note.contents = contents # if for some reason the note isn't present, create it else: next_id = self.database.next_id(Note) note = Note.create(next_id, contents, notebook_id=main_notebook.object_id, startup=startup) self.database.save(note, commit=False)
def setUp( self ): self.object_id = u"17" self.title = None self.contents = None self.summary = None self.notebook_id = None self.startup = False self.rank = None self.user_id = None self.username = None self.creation = None self.delta = timedelta( seconds = 1 ) self.note = Note.create( self.object_id )
def setUp( self ): self.object_id = "17" self.trash_id = "18" self.name = u"my notebook" self.trash_name = u"trash" self.user_id = u"me" self.delta = timedelta( seconds = 1 ) self.read_write = Notebook.READ_WRITE self.owner = False self.rank = 17.5 self.trash = Notebook.create( self.trash_id, self.trash_name, read_write = Notebook.READ_ONLY, deleted = False, user_id = self.user_id ) self.notebook = Notebook.create( self.object_id, self.name, trash_id = self.trash.object_id, deleted = False, user_id = self.user_id, read_write = self.read_write, owner = self.owner, rank = self.rank ) self.note = Note.create( "19", u"<h3>title</h3>blah" )
def get_notes_near_location(klass, lat, lon, radius=None): notes = Note.find_notes_near_location(lat, lon, radius) print "found %s notes" % len(notes) filtered_notes = [] for n in notes: print "note: %s" % n.get(Note.A_TEXT) append = True # take out inactive notes if append: if n.get(Note.A_INACTIVE): append = False print "inactive, skipping" # has the note been sent within the last day? if append: dt_last_sent = n.get(Note.A_DT_LAST_SENT) if dt_last_sent: i = n.get( Note.A_RESEND_INTERVAL) or Note.DEFAULT_RESEND_INTERVAL resend_window = datetime.datetime.now( ) - datetime.timedelta(hours=i) if dt_last_sent > resend_window: append = False print "too old, skipping" # is the note within an acceptable distance from the current point? if append: nloc = n.get(Note.A_LOC) nlat = nloc[0] nlon = nloc[1] distance = geo_utils.distance_between_two_points((lat, lon), (nlat, nlon)) print "distance is: %s" % distance n[Note.A_DISTANCE] = distance req_max_distance = n.get(Note.A_REQ_MAX_DISTANCE) print "max_distance is: %s" % req_max_distance if req_max_distance: if distance > req_max_distance: append = False print "too far, skipping" print "append: %s" % append if append: filtered_notes.append(n) return filtered_notes
def add_note(klass, lat, lon, text, max_distance_in_meters=None, resend_interval=None): doc = { Note.A_LAT: float(lat), Note.A_LON: float(lon), Note.A_TEXT: text } if max_distance_in_meters: doc[Note. A_REQ_MAX_DISTANCE] = float(max_distance_in_meters) / 1000.0 if resend_interval: doc[Note.A_RESEND_INTERVAL] = resend_interval return Note.create_or_update_note(doc)
def get_notes_near_location(klass, lat, lon, radius=None): notes = Note.find_notes_near_location(lat, lon, radius) print "found %s notes" % len(notes) filtered_notes = [] for n in notes: print "note: %s" % n.get(Note.A_TEXT) append = True # take out inactive notes if append: if n.get(Note.A_INACTIVE): append = False print "inactive, skipping" # has the note been sent within the last day? if append: dt_last_sent = n.get(Note.A_DT_LAST_SENT) if dt_last_sent: i = n.get(Note.A_RESEND_INTERVAL) or Note.DEFAULT_RESEND_INTERVAL resend_window = datetime.datetime.now() - datetime.timedelta(hours=i) if dt_last_sent > resend_window: append = False print "too old, skipping" # is the note within an acceptable distance from the current point? if append: nloc = n.get(Note.A_LOC) nlat = nloc[0] nlon = nloc[1] distance = geo_utils.distance_between_two_points((lat, lon), (nlat, nlon)) print "distance is: %s" % distance n[Note.A_DISTANCE] = distance req_max_distance = n.get(Note.A_REQ_MAX_DISTANCE) print "max_distance is: %s" % req_max_distance if req_max_distance: if distance > req_max_distance: append = False print "too far, skipping" print "append: %s" % append if append: filtered_notes.append(n) return filtered_notes
def update_note(self, filename, startup, main_notebook, note_ids, note=None): full_filename = os.path.join(self.HTML_PATH, filename) contents = fix_note_contents( file(full_filename).read(), main_notebook.object_id, note_ids, self.settings) if note: if note.contents == contents: return note.contents = contents # if for some reason the note isn't present, create it else: next_id = self.database.next_id(Note) note = Note.create(next_id, contents, notebook_id=main_notebook.object_id, startup=startup) self.database.save(note, commit=False)
def make_thread(self): title = u"Welcome to the Luminotes %s forum!" % self.forum_name # create a notebook thread to go in the forum notebook_id = self.database.next_id(Notebook, commit=False) thread_notebook = Notebook.create( notebook_id, title, ) self.database.save(thread_notebook, commit=False) anonymous = self.database.select_one( User, User.sql_load_by_username(u"anonymous")) # add a single welcome note to the new thread note_id = self.database.next_id(Note, commit=False) note = Note.create( note_id, u""" <h3>%s</h3> You can discuss any Luminotes %s topics here. This is a public discussion forum, so please keep that in mind when posting. And have fun. """ % (title, self.forum_name), notebook_id, startup=True, rank=0, user_id=anonymous.object_id, creation=datetime.now(), ) self.database.save(note, commit=False) # load the forum tag, or create one if it doesn't exist tag = self.database.select_one( Tag, Tag.sql_load_by_name(u"forum", user_id=anonymous.object_id)) if not tag: tag_id = self.database.next_id(Tag, commit=False) tag = Tag.create( tag_id, notebook_id= None, # this tag is not in the namespace of a single notebook user_id=anonymous.object_id, name=u"forum", description=u"discussion forum threads") self.database.save(tag, commit=False) # associate the forum tag with the previously created notebook thread, and set that # association's value to forum_name self.database.execute( anonymous.sql_save_notebook_tag(notebook_id, tag.object_id, value=self.forum_name), commit=False, ) # give the anonymous user access to the new notebook thread self.database.execute( anonymous.sql_save_notebook(notebook_id, read_write=True, owner=False, own_notes_only=True), commit=False, )
def setUp( self ): Test_controller.setUp( self ) self.notebook = Notebook.create( self.database.next_id( Notebook ), u"my notebook", trash_id = u"foo" ) self.database.save( self.notebook ) self.anon_notebook = Notebook.create( self.database.next_id( Notebook ), u"Luminotes", trash_id = u"bar" ) self.database.save( self.anon_notebook ) self.anon_note = Note.create( self.database.next_id( Note ), u"<h3>my note</h3>", notebook_id = self.anon_notebook.object_id, ) self.database.save( self.anon_note ) self.login_note = Note.create( self.database.next_id( Note ), u"<h3>login</h3>", notebook_id = self.anon_notebook.object_id, ) self.database.save( self.login_note ) self.username = u"mulder" self.password = u"trustno1" self.email_address = u"*****@*****.**" self.user = None self.session_id = None self.user = User.create( self.database.next_id( User ), self.username, self.password, self.email_address ) self.database.save( self.user ) self.database.execute( self.user.sql_save_notebook( self.notebook.object_id ) ) self.anonymous = User.create( self.database.next_id( User ), u"anonymous" ) self.database.save( self.anonymous ) self.database.execute( self.anonymous.sql_save_notebook( self.anon_notebook.object_id ) ) self.blog_user = User.create( self.database.next_id( User ), u"witten", self.password, self.email_address ) self.database.save( self.blog_user ) tag_id = self.database.next_id( Tag ) self.forum_tag = Tag.create( tag_id, notebook_id = None, # this tag is not in the namespace of a single notebook user_id = self.anonymous.object_id, name = u"forum", description = u"a discussion forum" ) self.database.save( self.forum_tag ) self.general_thread = Notebook.create( self.database.next_id( Notebook ), u"Welcome to the general forum!" ) self.database.save( self.general_thread ) self.database.execute( self.anonymous.sql_save_notebook( self.general_thread.object_id, read_write = Notebook.READ_WRITE_FOR_OWN_NOTES ), ) self.database.execute( self.anonymous.sql_save_notebook_tag( self.general_thread.object_id, self.forum_tag.object_id, value = u"general" ), ) self.support_thread = Notebook.create( self.database.next_id( Notebook ), u"Welcome to the support forum!" ) self.database.save( self.support_thread ) self.database.execute( self.anonymous.sql_save_notebook( self.support_thread.object_id, read_write = Notebook.READ_WRITE_FOR_OWN_NOTES ), ) self.database.execute( self.anonymous.sql_save_notebook_tag( self.support_thread.object_id, self.forum_tag.object_id, value = u"support" ), ) self.blog_thread = Notebook.create( self.database.next_id( Notebook ), u"Welcome to the blog!" ) self.database.save( self.blog_thread ) self.database.execute( self.anonymous.sql_save_notebook( self.blog_thread.object_id, read_write = Notebook.READ_WRITE_FOR_OWN_NOTES ), ) self.database.execute( self.blog_user.sql_save_notebook( self.blog_thread.object_id, read_write = Notebook.READ_WRITE ), ) self.database.execute( self.anonymous.sql_save_notebook_tag( self.blog_thread.object_id, self.forum_tag.object_id, value = u"forum" ), )
def create_thread(self, user_id): """ Create a new forum thread with a blank post, and give the thread a default name. Then redirect to that new thread. @type user_id: unicode or NoneType @param user_id: id of current logged-in user (if any) @rtype dict @return { 'redirect': new_notebook_url } @raise Access_error: the current user doesn't have access to create a post @raise Validation_error: one of the arguments is invalid """ if user_id is None: raise Access_error() user = self.__database.load(User, user_id) if user is None or not user.username or user.username == "anonymous": raise Access_error() anonymous = self.__database.select_one( User, User.sql_load_by_username(u"anonymous"), use_cache=True) if anonymous is None: raise Access_error() # for now, crappy hard-coding to prevent just anyone from creating a blog thread if self.__name == u"blog" and user.username != u"witten": raise Access_error() # create the new notebook thread thread_id = self.__database.next_id(Notebook, commit=False) thread = Notebook.create(thread_id, self.DEFAULT_THREAD_NAME, user_id=user.object_id) self.__database.save(thread, commit=False) # associate the forum tag with the new notebook thread tag = self.__database.select_one( Tag, Tag.sql_load_by_name(u"forum", user_id=anonymous.object_id)) self.__database.execute( anonymous.sql_save_notebook_tag(thread_id, tag.object_id, value=self.__name), commit=False, ) # give the anonymous user access to the new notebook thread self.__database.execute( anonymous.sql_save_notebook(thread_id, read_write=True, owner=False, own_notes_only=True), commit=False, ) # create a blank post in which the user can start off the thread note_id = self.__database.next_id(Notebook, commit=False) note = Note.create(note_id, u"<h3>", notebook_id=thread_id, startup=True, rank=0, user_id=user_id) self.__database.save(note, commit=False) self.__database.commit() if self.__name == "blog": return dict(redirect=u"/blog/%s" % thread_id, ) return dict(redirect=u"/forums/%s/%s" % (self.__name, thread_id), )
def setUp(self): Test_controller.setUp(self) self.notebook = Notebook.create(self.database.next_id(Notebook), u"my notebook", trash_id=u"foo") self.database.save(self.notebook) self.anon_notebook = Notebook.create(self.database.next_id(Notebook), u"Luminotes") self.database.save(self.anon_notebook) self.anon_note = Note.create( self.database.next_id(Note), u"<h3>my note</h3>", notebook_id=self.anon_notebook.object_id, ) self.database.save(self.anon_note) self.login_note = Note.create( self.database.next_id(Note), u"<h3>login</h3>", notebook_id=self.anon_notebook.object_id, ) self.database.save(self.login_note) self.blog_notebook = Notebook.create(self.database.next_id(Notebook), u"Luminotes blog") self.database.save(self.blog_notebook) self.blog_note = Note.create( self.database.next_id(Note), u"<h3>my blog entry</h3>", notebook_id=self.blog_notebook.object_id, ) self.database.save(self.blog_note) self.guide_notebook = Notebook.create(self.database.next_id(Notebook), u"Luminotes user guide") self.database.save(self.guide_notebook) self.guide_note = Note.create( self.database.next_id(Note), u"<h3>it's all self-explanatory</h3>", notebook_id=self.guide_notebook.object_id, ) self.database.save(self.guide_note) self.privacy_notebook = Notebook.create( self.database.next_id(Notebook), u"Luminotes privacy policy") self.database.save(self.privacy_notebook) self.privacy_note = Note.create( self.database.next_id(Note), u"<h3>yay privacy</h3>", notebook_id=self.privacy_notebook.object_id, ) self.database.save(self.privacy_note) self.username = u"mulder" self.password = u"trustno1" self.email_address = u"*****@*****.**" self.user = None self.session_id = None self.user = User.create(self.database.next_id(User), self.username, self.password, self.email_address) self.database.save(self.user) self.database.execute( self.user.sql_save_notebook(self.notebook.object_id)) self.anonymous = User.create(self.database.next_id(User), u"anonymous") self.database.save(self.anonymous) self.database.execute( self.anonymous.sql_save_notebook(self.anon_notebook.object_id, read_write=False, owner=False, rank=0)) self.database.execute( self.anonymous.sql_save_notebook(self.blog_notebook.object_id, read_write=False, owner=False, rank=1)) self.database.execute( self.anonymous.sql_save_notebook(self.guide_notebook.object_id, read_write=False, owner=False, rank=2)) self.database.execute( self.anonymous.sql_save_notebook(self.privacy_notebook.object_id, read_write=False, owner=False, rank=3))
def test_export_csv(self, note_contents=None, expected_contents=None): if not note_contents: note_contents = u"<h3>blah</h3>foo" note3 = Note.create(self.database.next_id(Note), note_contents, notebook_id=self.notebook.object_id, user_id=self.user.object_id) self.database.save(note3) response_headers = {} expected_notes = (self.note1, self.note2, note3) result = invoke( "export", "csv", self.database, self.notebook, expected_notes, response_headers, ) assert response_headers assert response_headers[u"Content-Type"] == u"text/csv;charset=utf-8" assert response_headers[ u"Content-Disposition"] == 'attachment; filename=%s.csv' % self.notebook.friendly_id assert isinstance(result, types.GeneratorType) pieces = [] for piece in result: pieces.append(piece) csv_data = "".join(pieces) reader = csv.reader(StringIO(csv_data)) row = reader.next() expected_header = [ u"contents", u"title", u"note_id", u"startup", u"username", u"revision_date" ] assert row == expected_header note_count = 0 # assert that startup notes come first, then normal notes in descending revision order for row in reader: assert len(row) == len(expected_header) (contents, title, note_id, startup, username, revision_date) = row assert note_count < len(expected_notes) expected_note = expected_notes[note_count] assert expected_note if expected_contents and note_id == note3.object_id: assert contents.decode("utf8") == expected_contents.replace( "\n", " ").strip() else: assert contents.decode( "utf8") == expected_note.contents.replace("\n", " ").strip() if expected_note.title: assert title.decode("utf8") == expected_note.title.strip() else: assert not title assert note_id.decode("utf8") == expected_note.object_id assert startup.decode( "utf8") == expected_note.startup and u"1" or "0" assert username.decode("utf8") == (expected_note.user_id and self.user.username or u"") assert revision_date.decode("utf8") == unicode( expected_note.revision) note_count += 1 assert note_count == len(expected_notes)
def test_export_csv( self, note_contents = None, expected_contents = None ): if not note_contents: note_contents = u"<h3>blah</h3>foo" note3 = Note.create( self.database.next_id( Note ), note_contents, notebook_id = self.notebook.object_id, user_id = self.user.object_id ) self.database.save( note3 ) response_headers = {} expected_notes = ( self.note1, self.note2, note3 ) result = invoke( "export", "csv", self.database, self.notebook, expected_notes, response_headers, ) assert response_headers assert response_headers[ u"Content-Type" ] == u"text/csv;charset=utf-8" assert response_headers[ u"Content-Disposition" ] == 'attachment; filename=%s.csv' % self.notebook.friendly_id assert isinstance( result, types.GeneratorType ) pieces = [] for piece in result: pieces.append( piece ) csv_data = "".join( pieces ) reader = csv.reader( StringIO( csv_data ) ) row = reader.next() expected_header = [ u"contents", u"title", u"note_id", u"startup", u"username", u"revision_date" ] assert row == expected_header note_count = 0 # assert that startup notes come first, then normal notes in descending revision order for row in reader: assert len( row ) == len( expected_header ) ( contents, title, note_id, startup, username, revision_date ) = row assert note_count < len( expected_notes ) expected_note = expected_notes[ note_count ] assert expected_note if expected_contents and note_id == note3.object_id: assert contents.decode( "utf8" ) == expected_contents.replace( "\n", " " ).strip() else: assert contents.decode( "utf8" ) == expected_note.contents.replace( "\n", " " ).strip() if expected_note.title: assert title.decode( "utf8" ) == expected_note.title.strip() else: assert not title assert note_id.decode( "utf8" ) == expected_note.object_id assert startup.decode( "utf8" ) == expected_note.startup and u"1" or "0" assert username.decode( "utf8" ) == ( expected_note.user_id and self.user.username or u"" ) assert revision_date.decode( "utf8" ) == unicode( expected_note.revision ) note_count += 1 assert note_count == len( expected_notes )
def setUp(self): Test_controller.setUp(self) self.notebook = Notebook.create(self.database.next_id(Notebook), u"my notebook", trash_id=u"foo") self.database.save(self.notebook) self.anon_notebook = Notebook.create(self.database.next_id(Notebook), u"Luminotes", trash_id=u"bar") self.database.save(self.anon_notebook) self.anon_note = Note.create( self.database.next_id(Note), u"<h3>my note</h3>", notebook_id=self.anon_notebook.object_id, ) self.database.save(self.anon_note) self.login_note = Note.create( self.database.next_id(Note), u"<h3>login</h3>", notebook_id=self.anon_notebook.object_id, ) self.database.save(self.login_note) self.username = u"mulder" self.password = u"trustno1" self.email_address = u"*****@*****.**" self.user = None self.session_id = None self.user = User.create(self.database.next_id(User), self.username, self.password, self.email_address) self.database.save(self.user) self.database.execute( self.user.sql_save_notebook(self.notebook.object_id)) self.anonymous = User.create(self.database.next_id(User), u"anonymous") self.database.save(self.anonymous) self.database.execute( self.anonymous.sql_save_notebook(self.anon_notebook.object_id)) self.blog_user = User.create(self.database.next_id(User), u"witten", self.password, self.email_address) self.database.save(self.blog_user) tag_id = self.database.next_id(Tag) self.forum_tag = Tag.create( tag_id, notebook_id= None, # this tag is not in the namespace of a single notebook user_id=self.anonymous.object_id, name=u"forum", description=u"a discussion forum") self.database.save(self.forum_tag) self.general_thread = Notebook.create( self.database.next_id(Notebook), u"Welcome to the general forum!") self.database.save(self.general_thread) self.database.execute( self.anonymous.sql_save_notebook( self.general_thread.object_id, read_write=Notebook.READ_WRITE_FOR_OWN_NOTES), ) self.database.execute( self.anonymous.sql_save_notebook_tag(self.general_thread.object_id, self.forum_tag.object_id, value=u"general"), ) self.support_thread = Notebook.create( self.database.next_id(Notebook), u"Welcome to the support forum!") self.database.save(self.support_thread) self.database.execute( self.anonymous.sql_save_notebook( self.support_thread.object_id, read_write=Notebook.READ_WRITE_FOR_OWN_NOTES), ) self.database.execute( self.anonymous.sql_save_notebook_tag(self.support_thread.object_id, self.forum_tag.object_id, value=u"support"), ) self.blog_thread = Notebook.create(self.database.next_id(Notebook), u"Welcome to the blog!") self.database.save(self.blog_thread) self.database.execute( self.anonymous.sql_save_notebook( self.blog_thread.object_id, read_write=Notebook.READ_WRITE_FOR_OWN_NOTES), ) self.database.execute( self.blog_user.sql_save_notebook(self.blog_thread.object_id, read_write=Notebook.READ_WRITE), ) self.database.execute( self.anonymous.sql_save_notebook_tag(self.blog_thread.object_id, self.forum_tag.object_id, value=u"forum"), )