def __init__(self): # create an author self.GOLDMAN = "Emma Goldman" self.goldman = Author(name=self.GOLDMAN) self.goldman.save() # create a Card self.fixture_isbn = "987" self.fixture_title = "living my life" self.autobio = Card(title=self.fixture_title, isbn=self.fixture_isbn) self.autobio.save() self.autobio.authors.add(self.goldman) # mandatory: unknown card type self.typ = CardType(name="unknown") self.typ.save() # create other card types self.type_book = "book" self.typ = CardType(name=self.type_book) self.typ.save() # add a publisher self.pub = Publisher(name="agone") self.pub.save() self.autobio.publishers.add(self.pub) # a needed place: self.place = Place(name="test place", is_stand=False, can_sell=True) self.place.save() # Preferences: (default place) self.preferences = Preferences(default_place=self.place).save() # create a Basket self.basket = Basket(name="basket_test") self.basket.save() # a Distributor and a Deposit with no cards self.distributor_name = "distributor test" self.distributor = Distributor(name=self.distributor_name).save() self.deposit = Deposit(name="deposit test").save()
class TestViews(TestCase): def setUp(self): self.c = Client() self.GOLDMAN = "Emma Goldman" self.goldman = Author(name=self.GOLDMAN) self.goldman.save() self.autobio = Card(title="Living my Life", isbn="123") self.autobio.save() self.autobio.authors.add(self.goldman) # see also test fixtures https://docs.djangoproject.com/en/1.7/topics/testing/tools/#django.test.TransactionTestCase.fixtures self.user = auth.models.User.objects.create_user(username="******", password="******") self.c.login(username="******", password="******") def post_to_view(self, isbn=None): post_params = {} if isbn: post_params["isbn"] = isbn return self.c.post(reverse("card_sell"), post_params) def test_sell_isbn_doesnt_exist(self): resp = self.post_to_view(isbn="9876") self.assertTrue(resp) pass # test views with RequestFactory def test_sell_isbn(self): resp = self.post_to_view(isbn="123") self.assertTrue(resp) def test_sell_no_isbn_in_post(self): resp = self.post_to_view() self.assertTrue(resp)
def setUp(self): # create an author self.GOLDMAN = "Emma Goldman" self.goldman = Author(name=self.GOLDMAN) self.goldman.save() # create a Card self.fixture_isbn = "987" self.fixture_title = "living my life" self.autobio = Card(title=self.fixture_title, isbn=self.fixture_isbn) self.autobio.save() self.autobio.authors.add(self.goldman) # Create a distributor self.dist = Distributor(name="dist test") self.dist.save() # mandatory: unknown card type typ = CardType(name="unknown") typ.save() # create other card types self.type_book = "book" typ = CardType(name=self.type_book) typ.save() # create places and add copies (to Move) self.place = PlaceFactory.create() self.place2 = PlaceFactory.create() self.place.add_copy(self.autobio) self.c = Client() self.user = auth.models.User.objects.create_user(username="******", password="******") self.c.login(username="******", password="******")
def setUp(self): self.c = Client() self.GOLDMAN = "Emma Goldman" self.goldman = Author(name=self.GOLDMAN) self.goldman.save() self.autobio = Card(title="Living my Life", isbn="123") self.autobio.save() self.autobio.authors.add(self.goldman) # see also test fixtures https://docs.djangoproject.com/en/1.7/topics/testing/tools/#django.test.TransactionTestCase.fixtures self.user = auth.models.User.objects.create_user(username="******", password="******") self.c.login(username="******", password="******")
def handle(self, *args, **options): """ Add a card with a quantity. !!! the script can add some cards and exit. It is not indempotent ! """ WARN_MSG = "***** This script is not indempotent. If you run it twice it will add cards twice. Contact us if you are in such a situation. *****" print(WARN_MSG) csvfile = options.get('input') if not csvfile.endswith('csv'): self.stdout.write("Please give a .csv file as argument.") exit(1) with open(csvfile, "r") as f: lines = f.readlines() lines = [it.strip() for it in lines] source = 'librairiedeparis' print("INFO: the default bibliographic source is FR. See the (unimplemented) -l option.") if options.get('lang'): self.stdout.write("Unimplemented. Would you buy me a beer ?") exit(1) default_place = Preferences.prefs().default_place if not default_place: print("We couldn't find a default place. Please check and try again.") exit(1) separator = find_separator(lines[0], default=";") for i, line in enumerate(lines): isbn, quantity = line.split(separator) if not is_isbn(isbn): self.stdout.write("It seems that {} is not a valid isbn or one that we know around here. Please check and try again.".format(isbn)) exit(1) print("[{}] - searching for {}...".format(i + 1, isbn), end="") res, traces = search_on_data_source(source, isbn) if not res: print(" no result :( Exiting.") exit(1) print(" ok ({} results)".format(len(res))) if len(res) > 1: print("INFO: got more than 1 result for {}, we pick the first one.". format(isbn)) res = res[0] print("\t adding to {}...".format(default_place), end="") try: card, msgs = Card.from_dict(res) # XXX quite long except Exception as e: print() print("Error with res {}: {}.".format(res, e)) print("Exiting.") exit(1) default_place.add_copy(card, to_int(quantity)) print(" done quantity x{}.".format(quantity))
class TestAddView(TestCase): def setUp(self): # create an author self.GOLDMAN = "Emma Goldman" self.goldman = Author(name=self.GOLDMAN) self.goldman.save() # create a Card self.fixture_isbn = "987" self.fixture_title = "living my life" self.autobio = Card(title=self.fixture_title, isbn=self.fixture_isbn) self.autobio.save() self.autobio.authors.add(self.goldman) # Create a distributor self.dist = Distributor(name="dist test") self.dist.save() # mandatory: unknown card type typ = CardType(name="unknown") typ.save() # create other card types self.type_book = "book" typ = CardType(name=self.type_book) typ.save() # create places and add copies (to Move) self.place = PlaceFactory.create() self.place2 = PlaceFactory.create() self.place.add_copy(self.autobio) self.c = Client() self.user = auth.models.User.objects.create_user(username="******", password="******") self.c.login(username="******", password="******") def test_move(self, mock_data_source): resp = self.c.get(reverse("card_move", args=(1, ))) self.assertEqual(resp.status_code, 200) resp = self.c.post(reverse("card_move", args=(1, )), data={ "origin": 1, "destination": 2, "nb": 2, })
class TestAddView(TestCase): def setUp(self): # create an author self.GOLDMAN = "Emma Goldman" self.goldman = Author(name=self.GOLDMAN) self.goldman.save() # create a Card self.fixture_isbn = "987" self.fixture_title = "living my life" self.autobio = Card(title=self.fixture_title, isbn=self.fixture_isbn) self.autobio.save() self.autobio.authors.add(self.goldman) # Create a distributor self.dist = Distributor(name="dist test") self.dist.save() # mandatory: unknown card type typ = CardType(name="unknown") typ.save() # create other card types self.type_book = "book" typ = CardType(name=self.type_book) typ.save() # create places and add copies (to Move) self.place = PlaceFactory.create() self.place2 = PlaceFactory.create() self.place.add_copy(self.autobio) self.c = Client() self.user = auth.models.User.objects.create_user(username="******", password="******") self.c.login(username="******", password="******") def test_move(self, mock_data_source): resp = self.c.get(reverse("card_move", args=(1,))) self.assertEqual(resp.status_code, 200) resp = self.c.post(reverse("card_move", args=(1,)), data={ "origin": 1, "destination": 2, "nb": 2, })
def search_on_data_source(data_source, search_terms, PAGE=1): """search with the appropriate scraper. data_source is the name of an imported module. search_terms: list of strings. return: a couple (search results, stacktraces). Search result is a list of dicts. """ # get the imported module by name. # They all must have a class Scraper. scraper = getattr(globals()[data_source], "Scraper") # res, traces = scraper(*search_terms).search() res, traces = scraper(search_terms, PAGE=PAGE).search() # Check if we already have these cards in stock (with isbn). res = Card.is_in_stock(res) return res, traces
def collection_export(request, **kwargs): """Export a search of our stock or all of it. - format: text, csv. - select: all, selection. """ if request.method == 'GET': formatt = request.GET.get('format') select = request.GET.get('select') query = request.GET.get('query') distributor = request.GET.get("distributor") distributor_id = request.GET.get("distributor_id") card_type_id = request.GET.get("card_type_id") publisher_id = request.GET.get("publisher_id") place_id = request.GET.get("place_id") shelf_id = request.GET.get("shelf_id") order_by = request.GET.get("order_by") # bought = request.GET.get("in_stock") # Export all the stock or a custom search ? # would rather validate request.GET and ** # or call api's cards search and get the json. if select == "selection": query_list = query.split(" ") res, meta = Card.search(query_list, to_list=True, distributor=distributor, distributor_id=distributor_id, publisher_id=publisher_id, card_type_id=card_type_id, place_id=place_id, shelf_id=shelf_id, order_by=order_by, in_deposits=True) elif select == "all": # res = Card.objects.filter(in_stock=True).all() res = Card.cards_in_stock() # Which format ? if formatt == 'txt': content_type = "text/raw" content = ppcard(res) elif formatt == "csv": content_type = "text/csv" cards = [it.to_list() for it in res] content = cards2csv(cards) else: content = "This format isn't supported." # Build the response. response = HttpResponse(content, content_type=content_type) filename = u"Abelujo stock search" if query: filename += " - {}".format(query) response[ 'Content-Disposition'] = u'attachment; filename="{}.{}"'.format( filename, formatt) return response else: content = "no search query" return HttpResponse(content, content_type=content_type)