Beispiel #1
0
def changed_items(for_date, batch=None):
    """Returns a list of objects that are new or changed and not pushed"""
    from nudge.client import send_command
    types = []
    for type_key in settings.NUDGE_SELECTIVE:
        app, model = type_key.lower().split('.')
        try:
            types.append(ContentType.objects.get_by_natural_key(app, model))
        except ContentType.DoesNotExist:
            raise ValueError(
                'Model listed in NUDGE_SELECTIVE does not exist: %s.%s' %
                  (app, model))

    eligible_versions = Version.objects.all().filter(
        revision__date_created__gte=for_date,
        content_type__in=types
      ).order_by('-revision__date_created')
    
    pot_batch_items = [PotentialBatchItem(version, batch=batch)
                        for version in eligible_versions]

    seen_pbis = []
    keys = [pbi.key() for pbi in pot_batch_items]
    response = send_command('check-versions/', {
        'keys': json.dumps(keys)}
      ).read()
    try:
        remote_versions = json.loads(response)
    except ValueError, e:
        raise CommandException(
          'Error decoding \'check-versions\' response: %s' % e, e)
Beispiel #2
0
def changed_items(for_date, batch=None):
    """Returns a list of objects that are new or changed and not pushed"""
    from nudge.client import send_command
    types = []
    for type_key in settings.NUDGE_SELECTIVE:
        app, model = type_key.lower().split('.')
        try:
            types.append(ContentType.objects.get_by_natural_key(app, model))
        except ContentType.DoesNotExist:
            raise ValueError(
                'Model listed in NUDGE_SELECTIVE does not exist: %s.%s' %
                  (app, model))

    eligible_versions = Version.objects.all().filter(
        revision__date_created__gte=for_date,
        content_type__in=types
      ).order_by('-revision__date_created')
    
    pot_batch_items = [PotentialBatchItem(version, batch=batch)
                        for version in eligible_versions]

    seen_pbis = []
    keys = [pbi.key() for pbi in pot_batch_items]
    response = send_command('check-versions/', {
        'keys': json.dumps(keys)}
      ).read()
    try:
        remote_versions = json.loads(response)
    except ValueError, e:
        raise CommandException(
          'Error decoding \'check-versions\' response: %s' % e, e)
Beispiel #3
0
def push_test_batch():
    """
    pushes empty batch to server to test settings and returns True on success
    """
    try:
        key = settings.NUDGE_KEY.decode('hex')
        response = send_command('batch/', serialize_batch(key, Batch()))
        return False if response.getcode() != 200 else True
    except:
        return False
Beispiel #4
0
def push_one(batch_push_item):
    key = settings.NUDGE_KEY.decode('hex')
    if batch_push_item.last_tried and batch_push_item.success:
        return 200
    batch_push_item.last_tried = datetime.datetime.now()
    try:
        response = send_command(
          'batch/', serialize_objects(key, [batch_push_item]))
        if response.getcode() == 200:
            batch_push_item.success=True
    except CommandException, e:
        response = e.orig_exception
Beispiel #5
0
def changed_items(for_date, batch=None):
    """Returns a list of objects that are new or changed and not pushed"""
    from nudge.client import send_command
    types = []
    for type_key in settings.NUDGE_SELECTIVE:
        app, model = type_key.split('.')
        types.append(ContentType.objects.get_by_natural_key(app, model))

    eligible_versions = (Version.objects.all()
                         .filter(revision__date_created__gte=for_date)
                         .filter(content_type__in=types)
                         .order_by('-revision__date_created'))

    pot_batch_items = [PotentialBatchItem(version, batch=batch) for version in
                       eligible_versions]

    seen_pbis = []
    keys = [pbi.key() for pbi in pot_batch_items]
    remote_versions_str = send_command('check-versions/',
                                       {'keys': json.dumps(keys)}).read()
    remote_versions = json.loads(remote_versions_str)

    def seen(key):
        if key not in seen_pbis:
            seen_pbis.append(key)
            return True
        else:
            return False

    pot_batch_items = filter(seen, pot_batch_items)
    screened_pbis = []
    for pbi in pot_batch_items:
        remote_details = remote_versions[pbi.key()]
        if remote_details:
            version_pk, version_type, timestamp = remote_details
            if not(version_pk == pbi.version.pk):
                pbi.remote_timestamp = timestamp
                pbi.remote_change_type = VERSION_TYPE_LOOKUP[version_type]
                screened_pbis.append(pbi)
        else:
            screened_pbis.append(pbi)

    return sorted(screened_pbis, key=lambda pbi: pbi.content_type)