def get_ordered_masters(self): from tools import unifylist # we should unify masters list in case if some masters absence # and raise error if unavailable _masters = unifylist(self.masters.split(',')) # masters are passed here as ordered array of masters ids as they # placed on editor page instances = Master.all().filter(Master.id.in_(self.masters.split(','))) masters = [] for p in _masters: for m in instances: if m.id == int(p): masters.append(m) break return masters
def POST(self, project_id): project = models.Project.get(id=project_id) if not project: raise web.notfound() # masters = models.Master.filter(project_id=project.id, editable=True) # masters = masters.order_by(models.Master.editor_ordering.asc()) if not project.masters: masters = models.Master.filter(project_id=project.id) project.masters = ','.join(map(lambda x: str(x.id), masters[:2])) web.ctx.orm.commit() masters = [] for p in project.masters.split(','): for m in models.Master.filter(project_id=project.id): if m.id == int(p): masters.append(m) break masters = map(lambda x: int(x.id), unifylist(masters)) zipped = zip(masters[::2], masters[1::2]) return simplejson.dumps({'axes': map(list, zipped), 'project_id': project_id})
def POST(self): postdata = web.input(masters='', project_id=0, glyphname='') project = models.Project.get(id=postdata.project_id) if not project: raise web.notfound() # we should unify masters list in case if some masters absence # and raise error if unavailable _masters = unifylist(postdata.masters.split(',')) # masters are passed here as ordered array of masters ids as they # placed on editor page instances = models.Master.all().filter( models.Master.id.in_(postdata.masters.split(','))) masters = [] for p in _masters: for m in instances: if m.id == int(p): masters.append(m) break self.set_masters(masters) version = models.Master.max(models.Master.version, project_id=project.id) master = models.Master.create(project_id=project.id, version=(version + 1)) prepare_master_environment(master) self.initialize(project.projectname, masters[0].version, masters[1].version) self.call_metapost_all_glyphs(self.get_lft_master()) logpath = project.get_instancelog(version=self.get_lft_master().version) for glyph in self.get_lft_master().get_glyphs(): json = get_edges_json(logpath, glyph.name) if not json['edges']: raise web.badrequest(simplejson.dumps({'error': 'could not find any contours for instance in %s' % logpath})) zpoints = glyph.get_zpoints() points = [] for i, contourpoints in enumerate(json['edges'][0]['contours']): if not contourpoints: raise web.badrequest(simplejson.dumps({'error': 'could not find any points in contour for instance in %s' % logpath})) metapost_points = [] for point in contourpoints: if session.get('mfparser', '') == 'controlpoints': metapost_points.append({'x': self.round(point['controls'][0]['x']), 'y': self.round(point['controls'][0]['y'])}) metapost_points.append({'x': self.round(point['x']), 'y': self.round(point['y'])}) if session.get('mfparser', '') == 'controlpoints': metapost_points.append({'x': self.round(point['controls'][1]['x']), 'y': self.round(point['controls'][1]['y'])}) if session.get('mfparser', '') == 'controlpoints' and metapost_points: if i != 0: points_ = metapost_points[1:] + metapost_points[:1] points += points_ else: points_ = metapost_points[2:] + metapost_points[:2] points += points_[::-1] else: points += metapost_points if len(zpoints) != len(points): raise web.badrequest(simplejson.dumps({'error': '%s zp != mp %s' % (len(zpoints), len(points))})) newglypha = models.Glyph.create(master_id=master.id, name=glyph.name, width=glyph.width, project_id=glyph.project_id) i = 0 for point in points: self.create_glyphpoint(newglypha, (i + 1), zpoints[i], point) i += 1 project.masters = ','.join([str(master.id)] * len(project.masters.split(','))) glyph = models.Glyph.get(name=postdata.glyphname, master_id=master.id) result = self.get_glyphs_jsondata(glyph.name, master) return simplejson.dumps({'version': '{0:03d}'.format(master.version), 'master_id': master.id, 'glyphdata': result, 'versions': get_versions(master.project_id)})