def _import_copies(self, impression, parser, code): # adds the copies to the impression copies = parser.get_copies() # Special case some that are known not to work. if not copies: if code == u'32–1a-Sm': copies = {u'F-Pn': u'Ac.p. 2682 \u2013 333 x 251 mm (v). TP: annotation \u2018D\xe9pos\xe9 \xe0 la Direction\uf0bdN 1837 - No 197\u2019.'} elif code == u'74–1-H': copies = {u'F-Pn': u'4 O. 408 \uf02d 277 x 195 mm. TP: publisher\u2019s oval stamp. Wrapper (fawn): p. [1] \u2018F. CHOPIN.\uf0bdM\xe9lodies Polonaises\uf0bd - in\xe9dites en France -\uf0bdOp. 74.\uf0bdPo\xe9sies\n Fran\xe7aises\uf0bdde VICTOR WILDER.\uf0bdPrix net. 5 Fr.\uf0bdParis, Maison J. MAHO, Editeur\uf0bdJ. Hamelle, Successeur\uf0bd25 faudourg [sic] Saint-Honor\xe9 25.\u2019, p. [2]\n blank, pp. [3, 4] blank.', u'US-Cu': u'M1619.S46 \uf02d 270 x 190 mm. TP: publisher\u2019s oval stamp, stamp \u2018SCHOTT FR\xc8RES\uf0bdBRUXELLES\u2019.'} keys = copies.keys() keys.sort() for key in keys: slug = slugify(key) library = Library.objects.filter(slug=slug).first() if not library: library = Library(title=key) library.slug = slug library_index_page = Page.objects.filter( slug='iii').first() library_index_page.add_child(instance=library) copy = Copy() copy.library = library copy.description = copies[key] copy.save() impression.copies.add( ImpressionCopy(impression=impression, copy=copy))
def run(app=app_): """ Updates the table with OpenURL servers in the application context """ with app.app_context(): updates = [] with open(app.config['INSTITUTE_OPENURL_DATA']) as fh: for line in fh: try: name, server, icon = line.strip().split('\t') except: # If it's just a number, it's that stupid first line # otherwise report the problem if not line.strip().isdigit(): sys.stderr.write('Found line with wrong number of tabs: %s\n'%line.strip()) continue # We have valid data # Let's see if we already know about this entry try: l = Library.query.filter(Library.libname == name).one() # It's there, but check if the rest is the same if l.iconurl != icon or l.libserver != server: l.iconurl = icon l.libserver = server sys.stderr.write('Updating OpenURL entry: %s\n'%l) db.session.commit() except NoResultFound: # We have a new record l = Library(libname = name, iconurl = icon, libserver = server, institute = 0) sys.stderr.write('Adding OpenURL entry: %s\n'%l) db.session.add(l) db.session.commit()
def test_user_can_get_documents_from_library(self): """ Test that can retrieve all the bibcodes from a library :return: no return """ # Ensure a user exists user = User(absolute_uid=self.stub_uid) db.session.add(user) db.session.commit() # Ensure a library exists library = Library(name='MyLibrary', description='My library', public=True, bibcode=[self.stub_document['bibcode']]) # Give the user and library permissions permission = Permissions(read=True, write=True) # Commit the stub data user.permissions.append(permission) library.permissions.append(permission) db.session.add_all([library, permission, user]) db.session.commit() # Retrieve the bibcodes using the web services bibcodes = self.library_view.get_documents_from_library( library_id=library.id )
def test_user_can_add_to_library(self): """ Tests that adding a bibcode to a library works correctly :return: """ # Ensure a user exists user = User(absolute_uid=self.stub_uid) db.session.add(user) db.session.commit() # Ensure a library exists library = Library(name='MyLibrary', description='My library', public=True) # Give the user and library permissions permission = Permissions(read=True, write=True) # Commit the stub data user.permissions.append(permission) library.permissions.append(permission) db.session.add_all([library, permission, user]) db.session.commit() library_id = library.id user_id = user.id # Get stub data for the document # Add a document to the library self.library_view.add_document_to_library( library_id=library_id, document_data=self.stub_document ) # Check that the document is in the library library = Library.query.filter(Library.id == library_id).all() for _lib in library: self.assertIn(self.stub_document['bibcode'], _lib.bibcode) self.stub_document['bibcode'] = self.stub_document['bibcode'] + 'NEW' # Add a different document to the library self.library_view.add_document_to_library( library_id=library_id, document_data=self.stub_document ) # Check that the document is in the library library = Library.query.filter(Library.id == library_id).all() for _lib in library: self.assertIn(self.stub_document['bibcode'], _lib.bibcode)
def run(app=app_): """ Updates the table with OpenURL servers in the application context """ with app.app_context(): updates = [] names = [] with open(app.config['INSTITUTE_OPENURL_DATA']) as fh: for line in fh: try: name, server, icon = line.strip().split('\t') # We keep a list of names for later, to check for deletions names.append(name) except: # If it's just a number, it's that stupid first line # otherwise report the problem if not line.strip().isdigit(): sys.stderr.write( 'Found line with wrong number of tabs: %s\n' % line.strip()) continue # We have valid data # First do updates # Let's see if we already know about this entry try: l = Library.query.filter(Library.libname == name).one() # It's there, but check if the rest is the same if l.iconurl != icon or l.libserver != server: l.iconurl = icon l.libserver = server sys.stderr.write('Updating OpenURL entry: %s\n' % l) db.session.commit() except NoResultFound: # We have a new record l = Library(libname=name, iconurl=icon, libserver=server, institute=0) sys.stderr.write('Adding OpenURL entry: %s\n' % l) db.session.add(l) db.session.commit() # Now check if we need to do deletions # Get database records with a name not in the master list records = db.session.query(Library).filter( ~Library.libname.in_(names)).all() for record in records: sys.stderr.write('Deleting stale OpenURL entry: %s\n' % record) Library.query.filter(Library.id == record.id).delete() db.session.commit()
class TestJsonSerialize(TestCase): @generate([[ Library(Book("book1"), Author("author1")), Library(Book("book1"), Author("author2")) ], "tests/lib"]) def test_save(self, data): JsonSerialize.save(data[1], data[0]) stream, ls = jsonSerializeToStream(data[0]) self.assertListEqual(json.load(stream), ls) @generate([[ Library(Book("book1"), Author("author1")), Library(Book("book1"), Author("author2")) ], "tests/lib"], [[], "unreal-path/unreal-file"]) def test__getFromFile(self, data): lst = [] for i in JsonSerialize.load(data[1]): objDict = JsonSerialize._prepareSave(i.__dict__) lst.append([objDict, i.__class__.__name__]) if not os.path.isfile("tables/json/" + data[1] + ".json"): return [] with open("tables/json/" + data[1] + ".json") as f: self.assertListEqual(json.load(f), lst)
def setUp(self): # set up the test DB self.db = tested_db self.db.drop_all() # there seems to be a problem with tearDown method. # It does not clear everything out everytime we test, so there's always an error with UNIQUE constraints. # Had to manually delete everything with my delete method with Postman and rerun testLibrary. self.db.create_all() self.db.session.add( Library(id=1, name="Bibliotheque Nationale", address="123 Berri", telephone="514-123-1234", email="*****@*****.**")) self.db.session.add( Library(id=2, name="Bibliotheque St-Michel", address="1234 St-Michel", telephone="514-111-4321", email="*****@*****.**")) self.db.session.commit() self.app = tested_app.test_client()
def _import_copies(self, impression, parser, code): # adds the copies to the impression copies = parser.get_copies() # Special case some that are known not to work. if not copies: if code == u'32–1a-Sm': copies = { u'F-Pn': u'Ac.p. 2682 \u2013 333 x 251 mm (v). TP: annotation \u2018D\xe9pos\xe9 \xe0 la Direction\uf0bdN 1837 - No 197\u2019.' } elif code == u'74–1-H': copies = { u'F-Pn': u'4 O. 408 \uf02d 277 x 195 mm. TP: publisher\u2019s oval stamp. Wrapper (fawn): p. [1] \u2018F. CHOPIN.\uf0bdM\xe9lodies Polonaises\uf0bd - in\xe9dites en France -\uf0bdOp. 74.\uf0bdPo\xe9sies\n Fran\xe7aises\uf0bdde VICTOR WILDER.\uf0bdPrix net. 5 Fr.\uf0bdParis, Maison J. MAHO, Editeur\uf0bdJ. Hamelle, Successeur\uf0bd25 faudourg [sic] Saint-Honor\xe9 25.\u2019, p. [2]\n blank, pp. [3, 4] blank.', u'US-Cu': u'M1619.S46 \uf02d 270 x 190 mm. TP: publisher\u2019s oval stamp, stamp \u2018SCHOTT FR\xc8RES\uf0bdBRUXELLES\u2019.' } keys = copies.keys() keys.sort() for key in keys: slug = slugify(key) library = Library.objects.filter(slug=slug).first() if not library: library = Library(title=key) library.slug = slug library_index_page = Page.objects.filter(slug='iii').first() library_index_page.add_child(instance=library) copy = Copy() copy.library = library copy.description = copies[key] copy.save() impression.copies.add( ImpressionCopy(impression=impression, copy=copy))
def create_library(self, service_uid, library_data): _name = library_data['name'] _description = library_data['description'] _read = library_data['read'] _write = library_data['write'] _public = library_data['public'] current_app.logger.info( 'Creating library for user_service: {0:d}'.format(service_uid)) try: # Make the library in the library table library = Library(name=_name, description=_description, public=_public) user = User.query.filter(User.id == service_uid).one() # Make the permissions permission = Permissions(read=_read, write=_write) # Use the ORM to link the permissions to the library and user, # so that no commit is required until the complete action is # finished. This means any rollback will not leave a single # library without permissions library.permissions.append(permission) user.permissions.append(permission) db.session.add_all([library, permission, user]) db.session.commit() current_app.logger.info( 'Library: "{0}" made, user_service: {1:d}'.format( library.name, user.id)) return library except IntegrityError as error: # Roll back the changes db.session.rollback() current_app.logger.error('IntegitryError, database has been rolled' 'back. Caused by user_service: {0:d}.' 'Full error: {1}'.format(user.id, error)) # Log here raise except Exception: db.session.rollback() raise
def read_input(lines: [str]): idx = 0 libraries = list() while idx < len(lines): if len(lines[idx]) > 0: if idx == 0: elements = lines[idx].split() n_books = elements[0] n_libraries = elements[1] days = int(elements[2]) idx += 1 elif idx == 1: books = build_books(lines[idx].split()) idx += 1 elif idx % 2 == 0: first_line_elements = lines[idx].split() second_line_elements = lines[idx + 1].split() library_n_books = first_line_elements[0] library_signup = first_line_elements[1] library_books_day = first_line_elements[2] library_books = search_books_by_id(books, second_line_elements) library = Library(id=idx // 2 - 1, signup_time=library_signup, books_day=library_books_day, books=library_books) libraries.append(library) if int(library_n_books) != len(library_books): raise Exception('Input data is not consistent') idx += 2 if int(n_books) != len(books) or int(n_libraries) != len(libraries): raise Exception('Input data is not consistent') # sort books by book.score descendant books.sort(key=lambda book: book.score, reverse=True) # sort libraries by library.signup_time ascendant libraries.sort(key=lambda library: library.signup_time, reverse=False) return books, libraries, days
def fileToObject(filename): with open(filename, 'r') as fin: lines = fin.read().splitlines() numOfBooks, numOfLibs, deadline = map(int, lines[0].split(' ')) bookScores = list(map(int, lines[1].split(' '))) libs = [] for i in range(2, len(lines), 2): if (lines[i] == ''): continue _, signUp, booksOutput = map(int, lines[i].split(' ')) books = [] for idx in map(int, lines[i+1].split(' ')): books.append(Book(idx, bookScores[idx])) libs.append(Library(len(libs), signUp, booksOutput, books, [])) return Input(numOfBooks, deadline, libs)
def test_user_can_remove_document_from_library(self): """ Test that can remove a document from the library :return: no return """ # Ensure a user exists user = User(absolute_uid=self.stub_uid) db.session.add(user) db.session.commit() # Ensure a library exists library = Library(name='MyLibrary', description='My library', public=True, bibcode=[self.stub_document['bibcode']]) # Give the user and library permissions permission = Permissions(read=True, write=True) # Commit the stub data user.permissions.append(permission) library.permissions.append(permission) db.session.add_all([library, permission, user]) db.session.commit() # Remove the bibcode from the library self.library_view.remove_documents_from_library( library_id=library.id, document_data=self.stub_document ) # Check it worked library = Library.query.filter(Library.id == library.id).one() self.assertTrue( len(library.bibcode) == 0, 'There should be no bibcodes: {0}'.format(library.bibcode) )
def test_user_can_delete_a_library(self): """ Tests that the user can correctly remove a library from its account :return: no return """ # Step 1. Make the user, library, and permissions # Ensure a user exists user = User(absolute_uid=self.stub_uid) db.session.add(user) db.session.commit() # Ensure a library exists library = Library(name='MyLibrary', description='My library', public=True, bibcode=[self.stub_document['bibcode']]) # Give the user and library permissions permission = Permissions(read=True, write=True) # Commit the stub data user.permissions.append(permission) library.permissions.append(permission) db.session.add_all([library, permission, user]) db.session.commit() library = Library.query.filter(Library.id == library.id).one() self.assertIsInstance(library, Library) self.library_view.delete_library(library_id=library.id) with self.assertRaises(NoResultFound): library = Library.query.filter(Library.id == library.id).one()
def upload_library(name, description, file): gs = geographic_servers.get_instance().get_default_server() library = Library(name=name, description=description) library.save() library_path = utils.check_library_path(library) file_path = utils.__get_uncompressed_file_upload_path(file) utils.copyrecursively(file_path + "/resources/", library_path) file_list = os.listdir(file_path) for file in file_list: if not os.path.isdir(file_path + "/" + file): f = open(file_path + "/" + file, 'r') sld = sld_reader.parse(f) style_name = remove_accents(sld.NamedLayer[0].Name) sld.NamedLayer[0].Name = style_name style_title = sld.NamedLayer[0].UserStyle[0].FeatureTypeStyle[ 0].Rule[0].Title r = sld.NamedLayer[0].UserStyle[0].FeatureTypeStyle[0].Rule[0] style = Style(name=style_name, title=style_title, is_default=True, type="US") style.save() rule = Rule(style=style, name=style_name, title=style_title, abstract='', filter=str(""), minscale=-1 if r.MinScaleDenominator is None else r.MinScaleDenominator, maxscale=-1 if r.MaxScaleDenominator is None else r.MaxScaleDenominator, order=0) rule.save() library_rule = LibraryRule(library=library, rule=rule) library_rule.save() scount = r.Symbolizer.__len__() - 1 for s in r.Symbolizer: if s.original_tagname_ == 'PointSymbolizer': opacity = s.Graphic.Opacity.valueOf_ rotation = s.Graphic.Rotation.valueOf_ size = s.Graphic.Size.valueOf_ if len(s.Graphic.Mark) >= 1: mark = s.Graphic.Mark[0] stroke = '#000000' if mark.Stroke.CssParameter.__len__() > 0: stroke = mark.Stroke.CssParameter[0].valueOf_ stroke_width = 1 if mark.Stroke.CssParameter.__len__() > 1: stroke_width = mark.Stroke.CssParameter[1].valueOf_ stroke_opacity = 1 if mark.Stroke.CssParameter.__len__() > 2: stroke_opacity = mark.Stroke.CssParameter[ 2].valueOf_ stroke_dash_array = 'none' if mark.Stroke.CssParameter.__len__() > 3: stroke_dash_array = mark.Stroke.CssParameter[ 3].valueOf_ symbolizer = MarkSymbolizer( rule=rule, order=scount, opacity=opacity, size=size, rotation=rotation, well_known_name=mark.WellKnownName, fill=mark.Fill.CssParameter[0].valueOf_, fill_opacity=mark.Fill.CssParameter[1].valueOf_, stroke=stroke, stroke_width=stroke_width, stroke_opacity=stroke_opacity, stroke_dash_array=stroke_dash_array) symbolizer.save() if len(s.Graphic.ExternalGraphic) >= 1: external_graphic = s.Graphic.ExternalGraphic[0] online_resource = external_graphic.OnlineResource.href.split( '/') online_resource[-2] = library.name new_online_resource = settings.MEDIA_URL + online_resource[ -3] + '/' + library.name + '/' + remove_accents( online_resource[-1]) symbolizer = ExternalGraphicSymbolizer( rule=rule, order=scount, opacity=opacity, size=size, rotation=rotation, online_resource=new_online_resource, format=external_graphic.Format) symbolizer.save() elif s.original_tagname_ == 'LineSymbolizer': stroke = '#000000' if s.Stroke: if s.Stroke.CssParameter.__len__() > 0: stroke = s.Stroke.CssParameter[0].valueOf_ stroke_width = 1 if s.Stroke.CssParameter.__len__() > 1: stroke_width = s.Stroke.CssParameter[1].valueOf_ stroke_opacity = 1 if s.Stroke.CssParameter.__len__() > 2: stroke_opacity = s.Stroke.CssParameter[2].valueOf_ stroke_dash_array = 'none' if s.Stroke.CssParameter.__len__() > 3: stroke_dash_array = s.Stroke.CssParameter[ 3].valueOf_ symbolizer = LineSymbolizer( rule=rule, order=scount, stroke=stroke, stroke_width=stroke_width, stroke_opacity=stroke_opacity, stroke_dash_array=stroke_dash_array) symbolizer.save() elif s.original_tagname_ == 'PolygonSymbolizer': stroke = '#000000' if s.Stroke.CssParameter.__len__() > 0: stroke = s.Stroke.CssParameter[0].valueOf_ stroke_width = 1 if s.Stroke.CssParameter.__len__() > 1: stroke_width = s.Stroke.CssParameter[1].valueOf_ stroke_opacity = 1 if s.Stroke.CssParameter.__len__() > 2: stroke_opacity = s.Stroke.CssParameter[2].valueOf_ stroke_dash_array = 'none' if s.Stroke.CssParameter.__len__() > 3: stroke_dash_array = s.Stroke.CssParameter[3].valueOf_ symbolizer = PolygonSymbolizer( rule=rule, order=scount, fill=s.Fill.CssParameter[0].valueOf_, fill_opacity=s.Fill.CssParameter[1].valueOf_, stroke=stroke, stroke_width=stroke_width, stroke_opacity=stroke_opacity, stroke_dash_array=stroke_dash_array) symbolizer.save() scount -= 1 output = StringIO.StringIO() sld.export(output, 0) sld_body = output.getvalue() output.close() gs.createStyle(style.name, sld_body) gs.reload_nodes() utils.__delete_temporaries(file_path)
def save(request): library_form = LibraryForm(request.POST or None) if library_form.is_valid(): form_clean = library_form.cleaned_data pk = form_clean.get('id', None) if not pk: library_model = Library(**form_clean) library_model.save() else: library_model = get_object_or_404(Library, pk=pk) library_model.title = form_clean.get('title', '') library_model.description = form_clean.get('description', '') library_model.status = form_clean.get('status', '') library_model.friend_name = form_clean.get('friend_name', '') library_model.friend_email = form_clean.get('friend_email', '') library_model.save() return HttpResponseRedirect(reverse('index')) book_list = Library.objects.all() context = { 'book_list': book_list, 'library_form': library_form, } return render(request, 'library/index.html', context)