Changeset 7e45324 for asadb/groups


Ignore:
Timestamp:
Sep 9, 2012, 6:43:39 AM (13 years ago)
Author:
Alex Dehnert <adehnert@…>
Branches:
master, space-access, stable, stage
Children:
c632b1c
Parents:
04bdacd
git-author:
Alex Dehnert <adehnert@…> (09/09/12 05:08:28)
git-committer:
Alex Dehnert <adehnert@…> (09/09/12 06:43:39)
Message:

Refactoring of GroupConstitution?.update()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • asadb/groups/models.py

    r9ce8bc1 r7e45324  
    134134    failure_reason = models.CharField(max_length=100, blank=True, default="")
    135135
     136    def record_failure(self, msg):
     137        now = datetime.datetime.now()
     138        self.failure_date = now
     139        self.status_msg = msg
     140        self.failure_reason = self.status_msg
     141        self.save()
     142
     143    def record_success(self, msg, updated):
     144        now = datetime.datetime.now()
     145        if updated:
     146            self.last_update = now
     147        self.status_msg = msg
     148        self.last_download = now
     149        self.failure_date = None
     150        self.failure_reason = ""
     151        self.save()
     152
    136153    def update(self, ):
    137154        url = self.source_url
     
    141158            url_opener = urllib.FancyURLopener()
    142159            now = datetime.datetime.now()
     160
     161            # Fetch the file
    143162            try:
    144163                tmp_path, headers = url_opener.retrieve(url)
    145164            except IOError:
    146                 self.failure_date = now
    147                 self.status_msg = "retrieval failed"
    148                 self.failure_reason = self.status_msg
    149                 self.save()
     165                self.record_failure("retrieval failed (IOError)")
    150166                success = False
    151167                return (success, self.status_msg, old_success, )
     168
     169            # At this point, failures are our fault's, not the group's.
     170            # We can let any errors bubble all the way up, rather than
     171            # trying to catch and neatly record them
     172            success = True
     173
     174            # Find a destination, and how to put it there
     175            save_filename = self.compute_filename(tmp_path, headers, )
     176            dest_path = self.path_from_filename(self.dest_file)
    152177            if tmp_path == url:
    153178                mover = shutil.copyfile
    154179            else:
    155180                mover = shutil.move
    156             save_filename = self.compute_filename(tmp_path, headers, )
    157             dest_path = self.path_from_filename(self.dest_file)
     181
     182            # Process the update
    158183            if save_filename != self.dest_file:
    159184                if self.dest_file: os.remove(dest_path)
    160185                mover(tmp_path, self.path_from_filename(save_filename))
    161186                self.dest_file = save_filename
    162                 self.last_update = now
    163                 self.status_msg = "new path"
     187                self.record_success("new path", updated=True)
    164188            else:
    165189                if filecmp.cmp(tmp_path, dest_path, shallow=False, ):
    166                     self.status_msg = "no change"
     190                    self.record_success("no change", updated=False)
    167191                else:
    168192                    # changed
    169193                    mover(tmp_path, dest_path)
    170                     self.last_update = now
    171                     self.status_msg = "updated in place"
    172             self.last_download = now
    173             self.failure_date = None
    174             self.failure_reason = ""
    175             self.save()
    176             success = True
     194                    self.record_success("updated in place", updated=True)
     195
    177196        else:
     197            self.record_failure("no url")
    178198            success = False
    179             self.status_msg = "no url"
    180             self.failure_reason = self.status_msg
    181             self.save()
     199
    182200        return (success, self.status_msg, old_success, )
    183201
Note: See TracChangeset for help on using the changeset viewer.