Changeset 471 in main


Ignore:
Timestamp:
11/14/11 10:31:03 (8 years ago)
Author:
pcosquer
Message:

csv: work on #46
this commit enables mails sending/index updating after the database commit
a new task (update_indexes) is available to update several indexes at once

Location:
trunk/openPLM
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/openPLM/plmapp/controllers/plmobject.py

    r402 r471  
    6060     
    6161    @classmethod 
    62     def create(cls, reference, type, revision, user, data={}, block_mails=False): 
     62    def create(cls, reference, type, revision, user, data={}, block_mails=False, 
     63            no_index=False): 
    6364        u""" 
    6465        This method builds a new :class:`.PLMObject` of 
     
    9192        obj = class_(reference=reference, type=type, revision=revision, 
    9293                     owner=user, creator=user) 
     94        if no_index: 
     95            obj.no_index = True 
    9396        if data: 
    9497            for key, value in data.iteritems(): 
     
    121124         
    122125    @classmethod 
    123     def create_from_form(cls, form, user, block_mails=False): 
     126    def create_from_form(cls, form, user, block_mails=False, no_index=False): 
    124127        u""" 
    125128        Creates a :class:`PLMObjectController` from *form* and associates *user* 
     
    136139            type = form.Meta.model.__name__ 
    137140            rev = form.cleaned_data["revision"] 
    138             obj = cls.create(ref, type, rev, user, form.cleaned_data, block_mails) 
     141            obj = cls.create(ref, type, rev, user, form.cleaned_data, 
     142                    block_mails, no_index) 
    139143            return obj 
    140144        else: 
  • trunk/openPLM/plmapp/csvimport.py

    r406 r471  
    1616from openPLM.plmapp.unicodecsv import UnicodeReader 
    1717from openPLM.plmapp.controllers.plmobject import PLMObjectController 
     18from openPLM.plmapp.tasks import update_indexes 
    1819 
    1920 
     
    141142 
    142143    @transaction.commit_on_success 
    143     def import_csv(self, headers): 
    144         """ 
    145         Imports the csv file. *headers* is the list of headers as given by the 
    146         user. Columns whose header is `None` are ignored. 
    147         *headers* must contains all values of :attr:`REQUIRED_HEADERS`. 
    148  
    149         If one or several errors occur (missing headers, row which can not be 
    150         parsed), a :exc:`CSVImportError` is raised with all detected errors. 
    151  
    152         :return: A list of :class:`.PLMObjectController` of all created objects. 
    153         """ 
     144    def __do_import_csv(self, headers): 
    154145        self.csv_file.seek(0) 
    155146        reader = UnicodeReader(self.csv_file, encoding=self.encoding) 
     
    171162        if self._errors: 
    172163            raise CSVImportError(self._errors) 
     164 
     165    def import_csv(self, headers): 
     166        """ 
     167        Imports the csv file. *headers* is the list of headers as given by the 
     168        user. Columns whose header is `None` are ignored. 
     169        *headers* must contains all values of :attr:`REQUIRED_HEADERS`. 
     170 
     171        If one or several errors occur (missing headers, row which can not be 
     172        parsed), a :exc:`CSVImportError` is raised with all detected errors. 
     173 
     174        :return: A list of :class:`.PLMObjectController` of all created objects. 
     175        """ 
     176        # puts all stuff in a private method so we call tear_down only after 
     177        # after a database commit  
     178        self.__do_import_csv(headers) 
     179        self.tear_down() 
     180        return self.objects 
     181 
     182    def tear_down(self): 
     183        """ 
     184        Method called once *all* rows have been successfully parsed. 
     185 
     186        By default, this method sends all blocked mails. 
     187        """ 
    173188        for obj in self.objects: 
    174189            obj.unblock_mails() 
    175         return self.objects 
    176190 
    177191    def store_errors(self, line, *errors): 
     
    252266            for cls in models.get_all_plmobjects().itervalues())) 
    253267 
     268    def tear_down(self): 
     269        super(PLMObjectsImporter, self).tear_down() 
     270        instances = [] 
     271        for obj in self.objects: 
     272            instance = obj.object 
     273            instances.append((instance._meta.app_label, 
     274                    instance._meta.module_name, instance._get_pk_val())) 
     275        update_indexes.delay(instances)  
    254276 
    255277    def parse_row(self, line, row): 
     
    279301            self.store_errors(line, *items) 
    280302        else: 
    281             obj = PLMObjectController.create_from_form(form, self.user, True) 
     303            obj = PLMObjectController.create_from_form(form, self.user, True, True) 
    282304            self.objects.append(obj) 
    283305 
  • trunk/openPLM/plmapp/search_indexes.py

    r410 r471  
    4343 
    4444    def enqueue_save(self, instance, **kwargs): 
    45         update_index.delay(instance._meta.app_label, instance._meta.module_name, instance._get_pk_val()) 
     45        if not getattr(instance, "no_index", False): 
     46            update_index.delay(instance._meta.app_label, 
     47                    instance._meta.module_name, instance._get_pk_val()) 
    4648 
    4749    def enqueue_delete(self, instance, **kwargs): 
  • trunk/openPLM/plmapp/tasks.py

    r418 r471  
    4949@task(default_retry_delay = 5 * 60, max_retries = 1) 
    5050def update_index(app_name, model_name, pk, **kwargs): 
    51     logger = update_index.get_logger(**kwargs) 
    5251    model_class = get_model(app_name, model_name) 
    5352    instance = model_class.objects.get(pk=pk) 
    5453    search_index = site.get_index(model_class) 
    5554    search_index.update_object(instance) 
     55 
     56@synchronized 
     57@task(default_retry_delay = 5 * 60, max_retries = 1) 
     58def update_indexes(instances): 
     59    for app_name, model_name, pk in instances: 
     60        model_class = get_model(app_name, model_name) 
     61        instance = model_class.objects.get(pk=pk) 
     62        search_index = site.get_index(model_class) 
     63        search_index.update_object(instance) 
    5664 
    5765@task 
  • trunk/openPLM/settings.py

    r459 r471  
    109109CELERY_ROUTES = { 
    110110    "openPLM.plmapp.tasks.update_index": {"queue": "index"}, 
     111    "openPLM.plmapp.tasks.update_indexes": {"queue": "index"}, 
    111112    "openPLM.plmapp.mail.do_send_histories_mail" : {"queue" : "mails"}, 
    112113    "openPLM.plmapp.mail.do_send_mail" : {"queue" : "mails"}, 
Note: See TracChangeset for help on using the changeset viewer.