def test_create_no_prior(self): test_listing = copy.deepcopy(TEST_LISTING) test_db_adapter = test_util.TestDBAdapter() listing_collection = test_util.TestCollection() listing_collection.find_result = test_listing test_db_adapter.collection = listing_collection self.mox.StubOutWithMock(tiny_classified, 'get_db_adapter') tiny_classified.get_db_adapter().AndReturn(test_db_adapter) self.mox.StubOutWithMock(services.listing_service, 'update') services.listing_service.update(test_listing) self.mox.ReplayAll() response = self.app.post( '/author/content/' + TEST_EMAIL + '/contact', data=TEST_FORM ) self.assertEqual(200, response.status_code) self.assertTrue(test_listing['contact_infos']) self.assertEqual(1, len(test_listing['contact_infos'])) self.assertEqual(1, test_listing['contact_id_next']) data_response = json.loads(response.data) self.assertTrue(test_util.check_dict( TEST_CONTACT, data_response ))
def delete_by_slug(qualified_slug): """Delete a listing by one of its fully qualified slugs. @param qualified_slug: A fully qualified slug of the listing to delete. @type qualified_slug: str @raise ValueError: If the given slug is not fully qualified. """ ensure_qualified_slug(qualified_slug) tiny_classified.get_db_adapter().delete_listing_by_slug(qualified_slug)
def delete(user): """Delete / remove a user. @param user: The user or user email to delete @type user: dict or str """ if isinstance(user, basestring): email = user else: email = user['email'] tiny_classified.get_db_adapter().delete_user(email)
def create(user): """Create a new user. @param user: The user to create. @type user: dict @raises: ValueError if the given user describes a email address that already exists. """ if read(user['email']): raise ValueError('A user with that email address already exists.') tiny_classified.get_db_adapter().upsert_user(user)
def update(listing): """Update the listing corresponding to a qualified listing slug. @param listing: A listing that already has been saved to the database @type listing: dict @raise ValueError: If the given listing has not already been saved to the database """ if not listing.get('_id', None): raise ValueError('Listing not yet saved to database') sanitize_tags(listing) calculate_slugs(listing) tiny_classified.get_db_adapter().upsert_listing(listing)
def create(listing): """Create a new listing and persist it to the database. @param listing: The new listing to save @type listing: dict @raise ValueError: If a listing with the same name as the new listing already exists. """ if tiny_classified.get_db_adapter().get_listing_by_name(listing['name']): raise ValueError( 'Listing with name %s already exists.' % listing['name'] ) sanitize_tags(listing) calculate_slugs(listing) tiny_classified.get_db_adapter().upsert_listing(listing)
def index_tags(): """List all unique listings tags. @return: listing tags as a list of tag dicts @rtype: iterable over dicts """ listings = tiny_classified.get_db_adapter().get_listings_collection() return listings.distinct('tags')
def update(original_email, user): """Updates / modifies a user by their previous email address. @param original_email: The previous / current email of the user to update. @type original_email: str @param user: The updated user @type user: dict @raises: ValueError if the given email address does not correspond to a user """ original_user = read(original_email) if not original_user: raise ValueError('A user with that email address does not exist.') user['_id'] = original_user['_id'] tiny_classified.get_db_adapter().upsert_user(user)
def read_by_email(email): """Get the listing corresponding to a listing author email address @param email: A user / author email address or None if not found @type email: str or None """ db_adapter = tiny_classified.get_db_adapter() listing_collection = db_adapter.get_listings_collection() return listing_collection.find_one({'author_email': email})
def read_by_slug(qualified_slug): """Get the listing corresponding to a qualified listing slug. @param qualified_slug: A qualified listing slug corresponding to a listing @type qualified_slug: str @raise ValueError: If the given slug is not fully qualified. """ ensure_qualified_slug(qualified_slug) return tiny_classified.get_db_adapter().get_listing_by_slug(qualified_slug)
def read(email): """Get the user that corresponds to the given email address. @param email: The email of the user to find. @type email: str @return: The user or None @rtype: dict or None """ return tiny_classified.get_db_adapter().get_user_by_email(email)
def test_read_by_email_not_found(self): test_collection = controllers.test_util.TestCollection() test_collection.find_result = None test_db_adapter = controllers.test_util.TestDBAdapter() test_db_adapter.collection = test_collection self.mox.StubOutWithMock(tiny_classified, 'get_db_adapter') tiny_classified.get_db_adapter().AndReturn(test_db_adapter) self.mox.ReplayAll() result = listing_service.read_by_email(TEST_EMAIL) self.assertEqual(None, result) expected_find_hash = {'author_email': TEST_EMAIL} self.assertTrue(controllers.test_util.check_dict( expected_find_hash, test_collection.find_hash ))
def create_default_listing_for_user(email): """Create a listing for the user corresponding to the given email. Create a listing for the user corresponding to the given email, initializes the created listing with the user's email address, and saves the new listing to the database. @param email: An email address corresponding to a user @type email: str @return: The new listing @rtype: dict """ listing = { 'author_email': email, 'name': 'Listing for user %s' % email, 'slugs': [], 'about': 'About section', 'tags': [] } tiny_classified.get_db_adapter().upsert_listing(listing) return listing
def delete(listing): """Delete a listing from the database. Deletes a listing from the database if that listing has been saved to the database. @param listing: The listing to delete. @type listing: dict """ listing_id = listing.get('_id', None) if listing_id: collection = tiny_classified.get_db_adapter().get_listings_collection() collection.remove(listing_id)
def list_by_slug(slug): """Determines if a specific listing should be returned or a listing index. If this slug refers to a specific listing, returns that single listing. Otherwise, returns a list of listings that have this slug as their prefix or, in other words, the listings under the category / sub-category that this slug refers to. @param slug: The slug to look up. @type slug: str @return: The individual listing or the list of listings at this slug. Will be a list if this refers to a cateogry / sub-category. Will be a list of one element if this slug refers to a single listing. @rtype: list """ return tiny_classified.get_db_adapter().list_listings_by_slug(slug)