Ejemplo n.º 1
0
def pushNotification(user,
                     notification_type,
                     values,
                     url_values=None,
                     image=None,
                     force=False):
    """
    If a similar notification has been pushed less than 2 hours ago, it will not push a notification.
    Can be forced with force=True.
    """
    data = {
        'i_message': models.Notification.get_i('message', notification_type),
        'owner_id': user.id,
        'c_message_data': join_data(*values),
        'c_url_data': (join_data(*url_values) if url_values else None),
        'image': image,
    }

    if not force:
        now = timezone.now()
        two_hours_ago = now - datetime.timedelta(hours=2)
        previous_similar_notifications = models.Notification.objects.filter(
            creation__gt=two_hours_ago,
            seen=False,
        ).filter(**data)
        if previous_similar_notifications.count():
            return None

    notification = models.Notification.objects.create(**data)
    models.UserPreferences.objects.filter(pk=user.preferences.pk).update(
        unread_notifications=F('unread_notifications') + 1, )
    return notification
def pushNotification(user, notification_type, values, url_values=None, image=None):
    """
    If preferences is not specified, it will use the preferences in user
    """
    notification = models.Notification.objects.create(
        owner=user,
        i_message=models.Notification.get_i('message', notification_type),
        c_message_data=join_data(*values),
        c_url_data=(join_data(*url_values) if url_values else None),
        image=image)
    models.UserPreferences.objects.filter(pk=user.preferences.pk).update(unread_notifications=F('unread_notifications') + 1)
    return notification
Ejemplo n.º 3
0
 def remove_c(self, field_name, to_remove):
     """
     Remove strings to a CSV formatted c_something
     """
     current_c = getattr(self, field_name)
     setattr(self, 'c_{name}'.format(name=field_name),
             join_data(*[c for c in current_c if c not in to_remove]))
Ejemplo n.º 4
0
 def add_c(self, field_name, to_add):
     """
     Add strings from a CSV formatted c_something
     """
     current_c = getattr(self, field_name)
     setattr(self, 'c_{name}'.format(name=field_name),
             join_data(*(current_c + [c for c in to_add if c not in current_c])))
Ejemplo n.º 5
0
 def remove_c(self, field_name, to_remove):
     """
     Remove strings to a CSV formatted c_something
     """
     current_c = getattr(self, field_name)
     setattr(self, 'c_{name}'.format(name=field_name),
             join_data(*[c for c in current_c if c not in to_remove]))
Ejemplo n.º 6
0
 def add_c(self, field_name, to_add):
     """
     Add strings from a CSV formatted c_something
     """
     current_c = getattr(self, field_name)
     setattr(self, 'c_{name}'.format(name=field_name),
             join_data(*(current_c + [c for c in to_add if c not in current_c])))
Ejemplo n.º 7
0
def prepare_data(data, model, unique, download_images):
    manytomany = {}
    dictionaries = {}
    images = {}
    for k, v in data.items():
        if modelHasField(model, k) and isinstance(model._meta.get_field(k),
                                                  ImageField):
            if download_images:
                images[k] = v
        elif k.startswith('d_') and isinstance(v, dict):
            if unique:
                data[k] = json.dumps(v)
            else:
                dictionaries[k] = v
        elif (k.startswith('i_') and not getattr(
                model, u'{}_WITHOUT_I_CHOICES'.format(k[2:].upper()), False)
              and not isinstance(v, int)):
            data[k] = model.get_i(k[2:], v)
        elif (k.startswith('c_') and isinstance(v, list)):
            data[k] = join_data(*v)
        elif (k.startswith('j_')):
            data[k] = json.dumps(v)
        elif not unique and isinstance(v, list):
            manytomany[k] = v
    for k in manytomany.keys():
        del (data[k])
    for k in dictionaries.keys():
        del (data[k])
    for k in images.keys():
        del (data[k])
    if unique:
        return data
    return data, manytomany, dictionaries, images
Ejemplo n.º 8
0
 def update_cache(self, field_name):
     to_cache_method = getattr(self, u'to_cache_{}'.format(field_name), None)
     if not to_cache_method:
         raise NotImplementedError(u'to_cache_{f} method is required for {f} cache'.format(f=field_name))
     setattr(self, u'_cache_{}_last_update'.format(field_name), timezone.now())
     value = to_cache_method()
     if hasattr(self, '_cache_j_{}'.format(field_name)):
         value = json.dumps(value) if value else None
         setattr(self, u'_cache_j_{}'.format(field_name), value)
     elif hasattr(self, '_cache_c_{}'.format(field_name)):
         value = join_data(*value) if value else None
         setattr(self, u'_cache_c_{}'.format(field_name), value)
     setattr(self, u'_cache_{}'.format(field_name), value)
Ejemplo n.º 9
0
 def update_cache(self, field_name):
     to_cache_method = getattr(self, u'to_cache_{}'.format(field_name), None)
     if not to_cache_method:
         raise NotImplementedError(u'to_cache_{f} method is required for {f} cache'.format(f=field_name))
     setattr(self, u'_cache_{}_last_update'.format(field_name), timezone.now())
     value = to_cache_method()
     if hasattr(self, '_cache_j_{}'.format(field_name)):
         value = json.dumps(value) if value else None
         setattr(self, u'_cache_j_{}'.format(field_name), value)
     elif hasattr(self, '_cache_c_{}'.format(field_name)):
         value = join_data(*value) if value else None
         setattr(self, u'_cache_c_{}'.format(field_name), value)
     setattr(self, u'_cache_{}'.format(field_name), value)
Ejemplo n.º 10
0
 def save_c(self, field_name, c):
     """
     Completely replace any existing CSV formatted list into c_something
     """
     setattr(self, 'c_{name}'.format(name=field_name), join_data(*c) if c is not None else None)
Ejemplo n.º 11
0
 def to_internal_value(self, data):
     return join_data(*data)
Ejemplo n.º 12
0
 def save_c(self, field_name, c):
     """
     Completely replace any existing CSV formatted list into c_something
     """
     setattr(self, 'c_{name}'.format(name=field_name), join_data(*c) if c is not None else None)