Ejemplo n.º 1
0
    def _validate(self, spot, data):
        fields = {}
        missing_fields = []
        seen_fields = {}
        for section in space_definitions():
            for field in section['fields'] if 'fields' in section else []:
                for value in field['value'] if isinstance(
                        field['value'], list) else [field['value']]:
                    key = value['key']
                    val = data[key] if key in data else None
                    orig_val = self._spot_value(key, spot)
                    if key in data and val != orig_val:
                        fields[key] = val
                        # SPOT-1277
                        if val == None or val == "":
                            orig_val = None

                    if 'required' in field and bool(val) == False and bool(
                            orig_val) == False:
                        s = section.get('section')
                        f = field.get('name')
                        if s + f not in seen_fields:
                            missing_fields.append({'section': s, 'field': f})
                            seen_fields[s + f] = True

        if 'available_hours' in data:
            valid_hours = True
            available = {}
            for d in data['available_hours']:
                hours = [0 for i in range(0, 2400)]
                for h in data['available_hours'][d]:
                    if len(h) == 2:
                        opn = int("".join(h[0].split(":")))
                        cls = int("".join(h[1].split(":")))
                        if opn < 0 or opn > 2359 or opn % 100 >= 60 or cls < 0 or cls > 2359 or cls % 100 >= 60 or opn >= cls:
                            valid_hours = False
                        else:
                            for i in range(opn, cls):
                                if i % 100 < 60:
                                    hours[i] = 1
                    else:
                        valid_hours = False

                available[d] = []
                opn = 0
                state = 0
                for i in range(0, 2360):
                    if i % 100 < 60:
                        if state != hours[i]:
                            state = hours[i]
                            if hours[i]:
                                opn = i
                            else:
                                available[d].append(self._hours_range([opn,
                                                                       i]))

            if valid_hours:
                fields['available_hours'] = available

        return fields, missing_fields
Ejemplo n.º 2
0
    def _validate(self, spot, data):
        fields = {}
        missing_fields = []
        seen_fields = {}
        for section in space_definitions():
            for field in section['fields'] if 'fields' in section else []:
                for value in field['value'] if isinstance(field['value'], list) else [field['value']]:
                    key = value['key']
                    val = data[key] if key in data else None
                    orig_val = self._spot_value(key, spot)
                    if key in data and val != orig_val:
                        fields[key] = val
                        # SPOT-1277
                        if val == None or val == "":
                            orig_val = None

                    if 'required' in field and bool(val) == False and bool(orig_val) == False:
                        s = section.get('section')
                        f = field.get('name')
                        if s + f not in seen_fields:
                            missing_fields.append({ 'section': s, 'field': f })
                            seen_fields[s + f] = True

        if 'available_hours' in data:
            valid_hours = True
            available = {}
            for d in data['available_hours']:
                hours = [0 for i in range(0,2400)]
                for h in data['available_hours'][d]:
                    if len(h) == 2:
                        opn = int("".join(h[0].split(":")))
                        cls = int("".join(h[1].split(":")))
                        if opn < 0 or opn > 2359 or opn % 100 >= 60 or cls < 0 or cls > 2359 or cls % 100 >= 60 or opn >= cls:
                            valid_hours = False
                        else:
                            for i in range(opn, cls):
                                if i % 100 < 60:
                                    hours[i] = 1
                    else:
                        valid_hours = False

                available[d] = []
                opn = 0
                state = 0
                for i in range(0, 2360):
                    if i % 100 < 60:
                        if state != hours[i]:
                            state = hours[i]
                            if hours[i]:
                                opn = i
                            else:
                                available[d].append(self._hours_range([opn, i]))

            if valid_hours:
                fields['available_hours'] = available

        return fields, missing_fields
Ejemplo n.º 3
0
    def space_rep(self, space, spot, schema):
        json_rep = space.json_data_structure()

        # overlay a spot copy with modifications
        if space.pending and len(space.pending) > 0:
            spot = copy.deepcopy(spot)
            self.apply_pending(spot, space)

        json_rep['is_published'] = (space.spot_id is not None)
        json_rep['is_modified'] = (space.spot_id is not None
                                   and (space.pending and len(space.pending) != 0
                                   or len(SpaceImage.objects.filter(space=space.id))
                                   or len(SpotImageLink.objects.filter(space=space.id,
                                                                       is_deleted__isnull=False))))
        json_rep['is_pending_publication'] = json_rep['is_pending_publication']
        json_rep['name'] = spot.get('name', '')
        json_rep['type'] = spot.get('type', '')
        json_rep['manager'] = spot.get('manager', space.manager)
        json_rep['modified_by'] = space.modified_by
        json_rep['last_modified'] = spot.get('last_modified', space.modified_date)
        json_rep['group'] = spot.get('location').get('building_name')
        json_rep['sections'] = []

        if settings.SS_SPACE_DESCRIPTION:
            json_rep['description'] = self.get_value(settings.SS_SPACE_DESCRIPTION, spot, schema)

        for secdef in space_definitions():
            section = {
                'section': secdef['section']
            }

            if secdef['section'] == 'hours_access':
                hours = spot['available_hours']
                section['available_hours'] = []
                # present all 7 days so translation and order happen here
                for d in ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']:
                    hrs = {
                        'day': d
                    }

                    if d in hours:
                        hrs['hours'] = hours[d]
        
                    section['available_hours'].append(hrs)
            elif secdef['section'] == 'images':
                section['images'] = [];
                images = []

                if space.spot_id:
                    n = 0;
                    for img in spot['images']:
                        n += 1
                        try:
                            link = SpotImageLink.objects.get(space=space.id,
                                                            spot_id=space.spot_id,
                                                            image_id=img.get('id'))
                            if link.is_deleted:
                                continue

                        except SpotImageLink.DoesNotExist:
                            link = SpotImageLink(space=space,
                                                 spot_id=space.spot_id,
                                                 image_id=img.get('id'),
                                                 display_index=n)
                            link.save()

                        images.append({
                                'order': link.display_index,
                                'description': img.get('description'),
                                'url': "{0}api/v1/space/{1}/image/-{2}".format(settings.APP_URL_ROOT,
                                                                               space.id, link.id)})

                for i in SpaceImage.objects.filter(space=space):
                    images.append({
                            'order': i.display_index,
                            'description': i.description,
                            'url': "{0}api/v1/space/{1}/image/{2}".format(settings.APP_URL_ROOT, space.id, i.id)})

                for i in sorted(images, key=lambda k: k['order']):
                    section['images'].append({
                            'description': i.get('description'),
                            'url': i.get('url')})

            if 'fields' in secdef:
                section['fields'] = []
        
                for f in secdef['fields']:
                    field = {
                        'name': f.get('name', '')
                    }

                    if 'required' in f:
                        field['required'] = f['required']
        
                    if 'help' in f:
                        field['help'] = f['help']

                    if 'value' in f:
                        if isinstance(f['value'], dict):
                            value = copy.deepcopy(f['value'])
                            if value.get('key') in json_rep:
                                value['value'] = json_rep[value.get('key')]
                            else:
                                value['value'] = self.get_value(value['key'], spot, schema)
                        else:
                            vals = []
                            for v in f['value']:
                                vc = copy.deepcopy(v)
                                vc['value'] = self.get_value(v['key'], spot, schema)
                                vals.append(vc)
        
                            value = vals
        
                        if value:
                            field['value'] = value
        
                    section['fields'].append(field)
        
            json_rep['sections'].append(section)

        return json_rep
Ejemplo n.º 4
0
    def space_rep(self, space, spot, schema):
        json_rep = space.json_data_structure()

        # overlay a spot copy with modifications
        if space.pending and len(space.pending) > 0:
            spot = copy.deepcopy(spot)
            self.apply_pending(spot, space)

        json_rep['is_published'] = (space.spot_id is not None)
        json_rep['is_modified'] = (
            space.spot_id is not None
            and (space.pending and len(space.pending) != 0
                 or len(SpaceImage.objects.filter(space=space.id)) or len(
                     SpotImageLink.objects.filter(space=space.id,
                                                  is_deleted__isnull=False))))
        json_rep['is_pending_publication'] = json_rep['is_pending_publication']
        json_rep['name'] = spot.get('name', '')
        json_rep['type'] = spot.get('type', '')
        json_rep['manager'] = spot.get('manager', space.manager)
        json_rep['modified_by'] = space.modified_by
        json_rep['last_modified'] = spot.get('last_modified',
                                             space.modified_date)
        json_rep['group'] = spot.get('location').get('building_name')
        json_rep['sections'] = []

        if settings.SS_SPACE_DESCRIPTION:
            json_rep['description'] = self.get_value(
                settings.SS_SPACE_DESCRIPTION, spot, schema)

        for secdef in space_definitions():
            section = {'section': secdef['section']}

            if secdef['section'] == 'hours_access':
                hours = spot['available_hours']
                section['available_hours'] = []
                # present all 7 days so translation and order happen here
                for d in [
                        'monday', 'tuesday', 'wednesday', 'thursday', 'friday',
                        'saturday', 'sunday'
                ]:
                    hrs = {'day': d}

                    if d in hours:
                        hrs['hours'] = hours[d]

                    section['available_hours'].append(hrs)
            elif secdef['section'] == 'images':
                section['images'] = []
                images = []

                if space.spot_id:
                    n = 0
                    for img in spot['images']:
                        n += 1
                        try:
                            link = SpotImageLink.objects.get(
                                space=space.id,
                                spot_id=space.spot_id,
                                image_id=img.get('id'))
                            if link.is_deleted:
                                continue

                        except SpotImageLink.DoesNotExist:
                            link = SpotImageLink(space=space,
                                                 spot_id=space.spot_id,
                                                 image_id=img.get('id'),
                                                 display_index=n)
                            link.save()

                        images.append({
                            'order':
                            link.display_index,
                            'description':
                            img.get('description'),
                            'url':
                            "{0}api/v1/space/{1}/image/-{2}".format(
                                settings.APP_URL_ROOT, space.id, link.id)
                        })

                for i in SpaceImage.objects.filter(space=space):
                    images.append({
                        'order':
                        i.display_index,
                        'description':
                        i.description,
                        'url':
                        "{0}api/v1/space/{1}/image/{2}".format(
                            settings.APP_URL_ROOT, space.id, i.id)
                    })

                for i in sorted(images, key=lambda k: k['order']):
                    section['images'].append({
                        'description':
                        i.get('description'),
                        'url':
                        i.get('url')
                    })

            if 'fields' in secdef:
                section['fields'] = []

                for f in secdef['fields']:
                    field = {'name': f.get('name', '')}

                    if 'required' in f:
                        field['required'] = f['required']

                    if 'help' in f:
                        field['help'] = f['help']

                    if 'value' in f:
                        if isinstance(f['value'], dict):
                            value = copy.deepcopy(f['value'])
                            if value.get('key') in json_rep:
                                value['value'] = json_rep[value.get('key')]
                            else:
                                value['value'] = self.get_value(
                                    value['key'], spot, schema)
                        else:
                            vals = []
                            for v in f['value']:
                                vc = copy.deepcopy(v)
                                vc['value'] = self.get_value(
                                    v['key'], spot, schema)
                                vals.append(vc)

                            value = vals

                        if value:
                            field['value'] = value

                    section['fields'].append(field)

            json_rep['sections'].append(section)

        return json_rep