Пример #1
0
    def load_patterns_legacy(self):
        ret = []
        
        for pattern in TextPattern.objects.all().order_by('order'):
            ret.append({
                'id': dputils.get_short_uid(pattern.created),
                'key': pattern.key,
                'title': pattern.title,
                'pattern': pattern.pattern,
                'updated': pattern.modified,
            })

        helpers = [
            ('', ur''),
            ('number', ur'\b(duabus|duae|duas|due|duo|duorum|duos|dve|dimid|dimidi%|dimd%|centum|mill%|decem|nonaginta|nouem|octo|octoginta|quadringentis|quattuor|quatuor|quindecim|quinquaginta|quinque|sex|tres|tribus|uiginti|unam|uno|unius|unum|unus|[iuxlcmdMD]+(( | et )[iuxlcmdMD]+)?)\b'),
            ('measurement', ur'<number> <PATTERN>( et dimid%| et <number> <PATTERN>| <number> minus| <number> <PATTERN> minus)*'),
            ('person', ur'\w\w%'),
            ('name', ur'\w+(( et)? [A-Z]\w*)*'),
            ('money', ur'\b(solidos|libras|obolum|obolus|numm%|denar%)\b'),
            ('title', ur'\b(abbas|comes|capellanus|episcopus|frater|mater|presbiter|regina|rex|tagn%|taigni|tainn%|tangi|tangn%|tani|tanni%|tanorum|tanus|tegn%|teign%|teinorum|tenus|thesaurarius|uicecomes|uxor)\b'),
            ('hide', ur'\b(hid%|uirg%|urig%|fer.i%|agr%|car%c%)\b'),
            ('peasant', ur'\b(uillan%|bordar%|cott?ar%|costcet%|seru%)\b'),
            ('livestock', ur'\b(porc%|oues%|capra%|animal%|ronc%|runc%|uacas)\b'),
        ]
        for key, pattern in helpers:
            ret.append({
                'id': dputils.get_short_uid(),
                'key': 'helper_%s' % key,
                'title': key.title(),
                'pattern': pattern,
                'updated': dputils.now(),
            })
        
        return ret
Пример #2
0
    def load_patterns_legacy(self):
        ret = []

        for pattern in TextPattern.objects.all().order_by('order'):
            ret.append({
                'id': dputils.get_short_uid(pattern.created),
                'key': pattern.key,
                'title': pattern.title,
                'pattern': pattern.pattern,
                'updated': pattern.modified,
            })

        helpers = [
            ('', ur''),
            ('number',
             ur'\b(duabus|duae|duas|due|duo|duorum|duos|dve|dimid|dimidi%|dimd%|centum|mill%|decem|nonaginta|nouem|octo|octoginta|quadringentis|quattuor|quatuor|quindecim|quinquaginta|quinque|sex|tres|tribus|uiginti|unam|uno|unius|unum|unus|[iuxlcmdMD]+(( | et )[iuxlcmdMD]+)?)\b'
             ),
            ('measurement',
             ur'<number> <PATTERN>( et dimid%| et <number> <PATTERN>| <number> minus| <number> <PATTERN> minus)*'
             ),
            ('person', ur'\w\w%'),
            ('name', ur'\w+(( et)? [A-Z]\w*)*'),
            ('money', ur'\b(solidos|libras|obolum|obolus|numm%|denar%)\b'),
            ('title',
             ur'\b(abbas|comes|capellanus|episcopus|frater|mater|presbiter|regina|rex|tagn%|taigni|tainn%|tangi|tangn%|tani|tanni%|tanorum|tanus|tegn%|teign%|teinorum|tenus|thesaurarius|uicecomes|uxor)\b'
             ),
            ('hide', ur'\b(hid%|uirg%|urig%|fer.i%|agr%|car%c%)\b'),
            ('peasant', ur'\b(uillan%|bordar%|cott?ar%|costcet%|seru%)\b'),
            ('livestock',
             ur'\b(porc%|oues%|capra%|animal%|ronc%|runc%|uacas)\b'),
        ]
        for key, pattern in helpers:
            ret.append({
                'id': dputils.get_short_uid(),
                'key': 'helper_%s' % key,
                'title': key.title(),
                'pattern': pattern,
                'updated': dputils.now(),
            })

        return ret
Пример #3
0
    def process_request_api(self, request, root, path=''):
        t0 = datetime.now()

        self.options = request.GET.copy()

        # what to return?
        toreturn = dputils.get_request_var(request, 'ret', root).split(',')
        self.toreturn = toreturn

        # make sure this is called AFTER self.options is set
        patterns = self.get_patterns()

        params = path.strip('/').split('/')

        data = None
        if request.body:
            data = dputils.json_loads(request.body)

        request_pattern = None
        request_patterni = None
        if root == 'patterns':
            if len(params) == 1:
                patternid = params[0]
                for i in range(0, len(patterns)):
                    if patterns[i]['id'] == patternid:
                        request_patterni = i
                        request_pattern = patterns[i]
                        break

        # Manages modifications
        modified = False

        if request.method == 'DELETE':
            if request_pattern:
                del patterns[request_patterni]
                modified = True

        if request.method == 'PUT':
            if request_pattern:
                if data['updated'] < request_pattern['updated']:
                    self.add_message(
                        'Change cancelled (this pattern was modified in the meantime)',
                        'error')
                else:
                    data['updated'] = dputils.now()
                    reorder = (patterns[i]['key'] != data['key'])
                    patterns[i] = data
                    self.auto_correct_pattern(patterns[i])
                    if reorder:
                        self.auto_correct_pattern_orders_and_numbers()
                    modified = True

        if self.move_pattern(request, root, data):
            modified = True

        # add new pattern if missing and client asked for it
        # TODO: not restful to change data on GET!
        # turn this into separate POST request.
        if 1:
            title_new = 'New Pattern'
            if not patterns or patterns[-1]['title'] != title_new:
                print 'ADD new pattern'
                patterns.append({
                    'id': dputils.get_short_uid(),
                    'key': slugify(unicode(title_new)),
                    'title': title_new,
                    'updated': dputils.now(),
                    'pattern': '',
                })
                modified = True

        if root == 'segunits':
            if request.method == 'POST':
                if data:
                    self.options.update(data)
                self.get_segunits()

        if modified:
            self.validate_patterns()
            self.save_patterns()

        pattern_errors = sum(
            [1 for pattern in self.patterns if 'error' in pattern])
        if pattern_errors:
            self.add_message('%s invalid patterns' % pattern_errors, 'warn')

        ret = {}

        if 'patterns' in toreturn:
            ret['patterns'] = self.get_patterns()
        if 'segunits' in toreturn:
            ret['segunits'] = self.get_json_from_segunits(toreturn)
        if 'variants' in toreturn:
            ret['variants'] = self.variants
        if 'stats' in toreturn:
            self.stats['duration_response'] = (datetime.now() -
                                               t0).total_seconds()
            ret['stats'] = self.stats
        ret['messages'] = self.messages
        ret['format'] = self.options.get('format', 'json')

        if ret['format'] == 'csv':
            ret['csv'] = self.get_csv_from_segunits()

        return ret
Пример #4
0
    def process_request_api(self, request, root, path=''):
        t0 = datetime.now()
        
        self.options = request.GET.copy()
        
        # what to return?
        self.toreturn = toreturn = request.REQUEST.get('ret', root).split(',')

        # make sure this is called AFTER self.options is set 
        patterns = self.get_patterns()

        params = path.strip('/').split('/')
        
        data = None
        if request.body:
            data = dputils.json_loads(request.body)

        request_pattern = None
        request_patterni = None
        if root == 'patterns':
            if len(params) == 1:
                patternid = params[0]
                for i in range(0, len(patterns)):
                    if patterns[i]['id'] == patternid:
                        request_patterni = i
                        request_pattern = patterns[i]
                        break

        # Manages modifications 
        modified = False

        if request.method == 'DELETE':
            if request_pattern:
                del patterns[request_patterni]
                modified = True

        if request.method == 'PUT':
            if request_pattern:
                if data['updated'] < request_pattern['updated']:
                    self.add_message('Change cancelled (this pattern was modified in the meantime)', 'error')
                else: 
                    data['updated'] = dputils.now()
                    reorder = (patterns[i]['key'] != data['key'])
                    patterns[i] = data
                    self.auto_correct_pattern(patterns[i])
                    if reorder: self.auto_correct_pattern_orders_and_numbers()
                    modified = True
                    
        if self.move_pattern(request, root, data): modified = True
        
        # add new pattern if missing and client asked for it
        # TODO: not restful to change data on GET!
        # turn this into separate POST request.
        if 1:
            title_new = 'New Pattern'
            if not patterns or patterns[-1]['title'] != title_new:
                print 'ADD new pattern'
                patterns.append({
                    'id': dputils.get_short_uid(),
                    'key': slugify(unicode(title_new)),
                    'title': title_new,
                    'updated': dputils.now(),
                    'pattern': '',
                });
                modified = True
                
        if root == 'segunits':
            if request.method == 'POST':
                if data:
                    self.options.update(data)
                self.get_segunits()
        
        if modified:
            self.validate_patterns()
            self.save_patterns()
            
        pattern_errors = sum([1 for pattern in self.patterns if 'error' in pattern])
        if pattern_errors:
            self.add_message('%s invalid patterns' % pattern_errors, 'warn')

        ret = {}
        
        if 'patterns' in toreturn:
            ret['patterns'] = self.get_patterns()
        if 'segunits' in toreturn:
            ret['segunits'] = self.get_json_from_segunits(toreturn)
        if 'variants' in toreturn:
            ret['variants'] = self.variants
        if 'stats' in toreturn:
            self.stats['duration_response'] = (datetime.now() - t0).total_seconds()
            ret['stats'] = self.stats
        ret['messages'] = self.messages
        ret['format'] = self.options.get('format', 'json')
        
        if ret['format'] == 'csv':
            ret['csv'] = self.get_csv_from_segunits()

        return ret