Exemplo n.º 1
0
 def save(self, old_record, list_record, detail_record):
     attributes = list_record.pop('attributes', {})
     list_record.setdefault('schema', self.schema.id)
     if not old_record:
         list_record.setdefault('item_date', today())
         list_record.setdefault('pub_date', now())
     from ebpub.db.forms import NewsItemForm
     form = NewsItemForm(list_record, instance=old_record)
     if form.is_valid():
         return self.create_or_update(old_record, attributes,
                                      **form.cleaned_data)
     else:
         raise SkipRecord(form.errors)
Exemplo n.º 2
0
 def save(self, old_record, list_record, detail_record):
     attributes = list_record.pop('attributes', {})
     list_record.setdefault('schema', self.schema.id)
     if not old_record:
         list_record.setdefault('item_date', today())
         list_record.setdefault('pub_date', now())
     from ebpub.db.forms import NewsItemForm
     form = NewsItemForm(list_record, instance=old_record)
     if form.is_valid():
         return self.create_or_update(old_record, attributes,
                                      **form.cleaned_data)
     else:
         self.logger.info("Skipping due to validation failures:")
         for key, val in form.errors.items():
             self.logger.info("%s: %s" % (key, val.as_text()))
         raise SkipRecord(form.errors)
Exemplo n.º 3
0
 def save(self, old_record, list_record, detail_record):
     attributes = list_record.pop('attributes', {})
     list_record.setdefault('schema', self.schema.id)
     if not old_record:
         list_record.setdefault('item_date', today())
         list_record.setdefault('pub_date', now())
     from ebpub.db.forms import NewsItemForm
     form = NewsItemForm(list_record, instance=old_record)
     if form.is_valid():
         return self.create_or_update(old_record, attributes,
                                      **form.cleaned_data)
     else:
         self.logger.info("Skipping due to validation failures:")
         for key, val in form.errors.items():
             self.logger.info("%s: %s" % (key, val.as_text()))
         raise SkipRecord(form.errors)
Exemplo n.º 4
0
def _item_create(info):
    info = copy.deepcopy(info)
    try:
        assert info.pop('type') == 'Feature'
        props = info['properties']
    except (KeyError, AssertionError):
        raise InvalidNewsItem({'type': 'not a valid GeoJSON Feature'})
    try:
        slug = props.pop('type', None)
        schema = models.Schema.objects.get(slug=slug)
    except (models.Schema.DoesNotExist):
        raise InvalidNewsItem({'type': 'schema %r does not exist' % slug})

    data = {'schema': schema.id}
    for key in ('title', 'description', 'url'):
        data[key] = props.pop(key, '')

    # If there are errors parsing the dates, keep the raw data and let
    # the ModelForm sort it out.
    pub_date = props.pop('pub_date', None)
    if pub_date:
        try:
            data['pub_date'] = normalize_datetime(pyrfc3339.parse(pub_date))
        except Exception:
            data['pub_date'] = pub_date
    else:
        data['pub_date'] = normalize_datetime(datetime.datetime.utcnow())
    item_date = props.pop('item_date', None)
    if item_date:
        try:
            data['item_date'] = parse_date(item_date, '%Y-%m-%d', False)
        except Exception:
            data['item_date'] = item_date
    else:
        try:
            data['item_date'] = data['pub_date'].date()
        except Exception:
            data['item_date'] = None

    data['location'], data['location_name'] = _get_location_info(
        info.get('geometry'), props.pop('location_name', None))
    if not data['location']:
        logger.warn("Saving NewsItem %s with no geometry" % data['title'])
    if not data['location_name']:
        logger.warn("Saving NewsItem %s with no location_name" % data['title'])


    from ebpub.db.forms import NewsItemForm
    form = NewsItemForm(data)
    if form.is_valid():
        item = form.save()
    else:
        raise InvalidNewsItem(form.errors)

    # Everything else goes in .attributes.
    attributes = {}
    for key, val in props.items():
        sf = models.SchemaField.objects.get(schema=schema, name=key)
        if sf.is_many_to_many_lookup():
            lookups = []
            for lookup_name in val:
                lookups.append(
                    models.Lookup.objects.get_or_create_lookup(sf, lookup_name))
            val = ','.join((str(lookup.id) for lookup in lookups))
        elif sf.is_lookup:
            val = models.Lookup.objects.get_or_create_lookup(sf, val)
        elif sf.is_type('date'):
            val = normalize_datetime(parse_date(val, '%Y-%m-%d'))
        elif sf.is_type('time'):
            val = normalize_datetime(parse_time(val, '%H:%M'))
        elif sf.is_type('datetime'):
            val = normalize_datetime(pyrfc3339.parse(datetime))
        attributes[key] = val
    item.attributes = attributes
    return item
Exemplo n.º 5
0
def _item_create(info):
    info = copy.deepcopy(info)
    try:
        assert info.pop('type') == 'Feature'
        props = info['properties']
    except (KeyError, AssertionError):
        raise InvalidNewsItem({'type': 'not a valid GeoJSON Feature'})
    try:
        slug = props.pop('type', None)
        schema = models.Schema.objects.get(slug=slug)
    except (models.Schema.DoesNotExist):
        raise InvalidNewsItem({'type': 'schema %r does not exist' % slug})

    data = {'schema': schema.id}
    for key in ('title', 'description', 'url'):
        data[key] = props.pop(key, '')

    # If there are errors parsing the dates, keep the raw data and let
    # the ModelForm sort it out.
    pub_date = props.pop('pub_date', None)
    if pub_date:
        try:
            data['pub_date'] = normalize_datetime(pyrfc3339.parse(pub_date))
        except Exception:
            data['pub_date'] = pub_date
    else:
        data['pub_date'] = normalize_datetime(datetime.datetime.utcnow())
    item_date = props.pop('item_date', None)
    if item_date:
        try:
            data['item_date'] = parse_date(item_date, '%Y-%m-%d', False)
        except Exception:
            data['item_date'] = item_date
    else:
        try:
            data['item_date'] = data['pub_date'].date()
        except Exception:
            data['item_date'] = None

    data['location'], data['location_name'] = _get_location_info(
        info.get('geometry'), props.pop('location_name', None))
    if not data['location']:
        logger.warn("Saving NewsItem %s with no geometry" % data['title'])
    if not data['location_name']:
        logger.warn("Saving NewsItem %s with no location_name" % data['title'])

    from ebpub.db.forms import NewsItemForm
    form = NewsItemForm(data)
    if form.is_valid():
        item = form.save()
    else:
        raise InvalidNewsItem(form.errors)

    # Everything else goes in .attributes.
    attributes = {}
    for key, val in props.items():
        sf = models.SchemaField.objects.get(schema=schema, name=key)
        if sf.is_many_to_many_lookup():
            lookups = []
            for lookup_name in val:
                lookups.append(
                    models.Lookup.objects.get_or_create_lookup(
                        sf, lookup_name))
            val = ','.join((str(lookup.id) for lookup in lookups))
        elif sf.is_lookup:
            val = models.Lookup.objects.get_or_create_lookup(sf, val)
        elif sf.is_type('date'):
            val = normalize_datetime(parse_date(val, '%Y-%m-%d'))
        elif sf.is_type('time'):
            val = normalize_datetime(parse_time(val, '%H:%M'))
        elif sf.is_type('datetime'):
            val = normalize_datetime(pyrfc3339.parse(datetime))
        attributes[key] = val
    item.attributes = attributes
    return item