Ejemplo n.º 1
0
def __DELETE(req, data_path):

    # config object
    config = {
        'data_path': data_path,
    }

    # define tasks (order is important)
    tasks = (
        __translate_path,
        __load_model,
        __pick_record,
        __check_ownership,
    )

    # run tasks
    config = __execute_taks(req, config, tasks)
    if isinstance(config, HttpResponse):
        return config

    if hasattr(config['model'],
               'immediate_delete') and config['model'].immediate_delete:
        config['rec'].delete()
    else:
        # mark the record as deleted
        config['rec'].deleted = True
        config['rec'].save(force_update=True)

    comet.Notifier(req, config['rec'], 'delete')
Ejemplo n.º 2
0
 def post_save(self):
     if not self.approved or not self.new_approval:
         return
     # notification data
     notif_data = {}
     if self.join_type == 'R':
         notif_data['owner'] = self.person.owner
         notif_data['type'] = 'join_accept'
         notif_data['text'] = self.location.name
         notif_data['picture'] = self.location.picture
     elif self.join_type == 'I':
         loc_p = models.get_model(settings.DATA_APP,
                                  'Person').objects.getOwn(
                                      self.location.owner)
         notif_data['owner'] = self.location.owner
         notif_data['type'] = 'invite_accept'
         notif_data['text'] = misc.formatFullname(self.person,
                                                  loc_p.name_order, True)
         notif_data['picture'] = self.person.picture
     if not notif_data['picture']:
         notif_data['picture'] = '/static/images/globe.png'
     # create notification
     notif = models.get_model(settings.DATA_APP,
                              'Notification').objects.create(**notif_data)
     # comet notify
     message = {}
     message['model'] = 'Notification'
     message['operation'] = 'create'
     message['uuid'] = notif.uuid
     message['user'] = notif_data['owner'].username
     message['session'] = None
     comet.Notifier().postMessage(message)
Ejemplo n.º 3
0
def __POST(req, data_path):

    # config object
    config = {
        'data_path': data_path,
        'rec_data': {},
        'm2m_data': {},
    }

    # define tasks (order is important)
    tasks = (
        __translate_path,
        __load_model,
        __check_create_permission,
        __decode_body,
        __prepare_record_data,
    )

    # run tasks
    config = __execute_taks(req, config, tasks)
    if isinstance(config, HttpResponse):
        return config

    # save data into database
    try:
        # create record
        config['rec'] = config['model'](**config['rec_data'])
        # validate
        try:
            config['rec'].full_clean()
        except ValidationError, e:
            if settings.DEBUG:
                msg = 'VALIDATION ERROR'
                msg += ': %s' % e.message_dict
                logging.debug(msg)
            return HttpResponseBadRequest("Bad Request")
        # save
        config['rec'].save(force_insert=True)

        # add this record to a a related record
        if config.has_key('related_field'):
            config['related_field'].add(config['rec'])
        # add m2m fields
        __handle_m2m_fields(config)
        comet.Notifier(req, config['rec'], 'create')
        # post save handler
        if hasattr(config['rec'], 'post_save'):
            getattr(config['rec'], 'post_save')()
Ejemplo n.º 4
0
def __PUT(req, data_path):

    # config object
    config = {
        'data_path': data_path,
        'rec_data': {},
        'm2m_data': {},
    }

    # define tasks (order is important)
    tasks = (
        __translate_path,
        __load_model,
        __pick_record,
        __check_ownership,
        __decode_body,
        __prepare_record_data,
    )

    # run tasks
    config = __execute_taks(req, config, tasks)
    if isinstance(config, HttpResponse):
        return config

    # send data to database
    try:
        # rec_data can be empty if case only m2m fields are updated
        if config['rec_data']:
            for k, v in config['rec_data'].iteritems():
                setattr(config['rec'], k, v)
            # validate record
            try:
                config['rec'].full_clean()
            except ValidationError, e:
                if settings.DEBUG:
                    msg = 'VALIDATION ERROR'
                    msg += ': %s' % e.message_dict
                    logging.debug(msg)
                return HttpResponseBadRequest("Bad Request")
            # save
            config['rec'].save(force_update=True)
        # add m2m fields
        __handle_m2m_fields(config)
        comet.Notifier(req, config['rec'], 'update')
        # post save handler
        if hasattr(config['rec'], 'post_save'):
            getattr(config['rec'], 'post_save')()
Ejemplo n.º 5
0
 def save(self, *args, **kwargs):
     notify_location = False
     # receive join request
     if self.owner != self.location.owner:
         self.owner = self.location.owner
         self.join_type = 'R'
         if self.location.member_auto_accept:
             self.approved = True
             notify_location = True
         try:
             self.profile = self.location.membershipprofile_set.get(
                 default=True)
         except:
             pass
     super(LocationMembership, self).save(*args, **kwargs)
     # auto join notification
     if notify_location:
         loc_p = models.get_model(settings.DATA_APP,
                                  'Person').objects.getOwn(
                                      self.location.owner)
         notif_data = {}
         notif_data['owner'] = self.location.owner
         notif_data['type'] = 'auto_join'
         notif_data['text'] = misc.formatFullname(self.person,
                                                  loc_p.name_order, True)
         notif_data['text2'] = self.location.name
         if self.person.picture:
             notif_data['picture'] = self.person.picture
         else:
             notif_data['picture'] = '/static/images/globe.png'
         notif = models.get_model(
             settings.DATA_APP, 'Notification').objects.create(**notif_data)
         message = {}
         message['model'] = 'Notification'
         message['operation'] = 'create'
         message['uuid'] = notif.uuid
         message['user'] = notif_data['owner'].username
         message['session'] = None
         comet.Notifier().postMessage(message)
     # delete
     # mark person deleted if person is not self created
     if self.deleted and not self.person.self_created and self.person.owner == self.location.owner:
         self.person.deleted = True
         self.person.save(force_update=True)