def save_features(self): try: map_id = self.request.matchdict.get("map_id") map = DBSession.query(Map).get(map_id) if map is None: return HTTPNotFound() if not self.has_permission(self.request.user, map): return HTTPUnauthorized() if 'features' not in self.request.params: return HTTPBadRequest() features = self.request.params.get('features').\ replace(u'\ufffd', '?') feature_collection = geojson.\ loads(features, object_hook=geojson.GeoJSON.to_instance) for feature in feature_collection['features']: feature_id = feature.properties.get('fid') if feature_id: cur_feature = DBSession.query(Feature).get(feature_id) DBSession.delete(cur_feature) obj = Feature(feature) map.features.append(obj) DBSession.commit() return {'success': True} except: DBSession.rollback() traceback.print_exc(file=sys.stdout) return {'success': False}
def upload_symbol(self): user = self.request.user if user is None: return HTTPUnauthorized() username = user.username file = self.request.POST['file'] # type checking if file != '' and not imghdr.what(file.file): return {'success': True, 'msg': 'Bad image'} if file is None: return json_dumps({'ok': 'false', 'description': 'No file to save'}) filename = file.filename input_file = file.file if not os.path.exists("/tmp/"+username): os.makedirs("/tmp/"+username) file_path = os.path.join("/tmp/"+username, filename) temp_file_path = file_path + '~' output_file = open(temp_file_path, 'wb') input_file.seek(0) while True: data = input_file.read(2 << 16) if not data: break output_file.write(data) output_file.close() im1 = Image.open(temp_file_path) scaled_width = 40 scaled_height = 40 cur_file = Symbols() width, height = im1.size if width > scaled_width: ratio = width/scaled_width scaled_height = height / ratio im2 = im1.resize((scaled_width, scaled_height), Image.NEAREST) im2.save(file_path) f = open(file_path) else: f = open(temp_file_path) cur_file.symbol_name = file.filename cur_file.symbol = f.read() cur_file.login_owner = username cur_file.is_public = False DBSession.add(cur_file) DBSession.commit() if os.path.exists(temp_file_path): os.remove(temp_file_path) if os.path.exists(file_path): os.remove(file_path) try: self.generate_symbol_file() except: DBSession.rollback() traceback.print_exc(file=sys.stdout) return {'success': 'true', 'description': 'file added', 'result': { 'url': "/symbol/" + str(cur_file.id), 'id': cur_file.id, 'symboltype': 'us', 'name': cur_file.symbol_name}}