def assign_release_dt_local(self): """ convert release_dt to the corresponding local time example: if release_dt: 2014-05-09 03:30:00 timezone: US/Pacific settings.TIME_ZONE: US/Central then the corresponding release_dt_local will be: 2014-05-09 05:30:00 """ now = datetime.now() now_with_tz = adjust_datetime_to_timezone(now, settings.TIME_ZONE) if self.timezone and self.release_dt and self.timezone.zone != settings.TIME_ZONE: time_diff = adjust_datetime_to_timezone(now, self.timezone) - now_with_tz self.release_dt_local = self.release_dt + time_diff else: self.release_dt_local = self.release_dt
def localize_date(value, to_tz=None): from tendenci.apps.base.utils import adjust_datetime_to_timezone try: if to_tz is None: to_tz = settings.UI_TIME_ZONE from_tz = settings.TIME_ZONE return adjust_datetime_to_timezone(value, from_tz=from_tz, to_tz=to_tz) except AttributeError: return ''
def download_files_from_s3(prefix='', to_dir='', update_only=False, dry_run=False): """ Retrieve the files inside the prefix (ex: themes) from S3, and store them to the directory to_dir. Example use: To download all files in the themes folder, call: download_files_from_s3(prefix='themes', to_dir='/path/to/site/themes') :type prefix: string :param prefix: The prefix of where to retrieve the files from s3 :type to_dir: string :param to_dir: The directory of where to download the files :type update_only: bool :param update_only: If True, only update the modified files, otherwise, overwrite the existing files. :type dry_run: bool :param dry_run: If True, do everything except saving the files. """ if not prefix: print('No prefix, exiting..') return if not os.path.isdir(to_dir): print('Destination directory does not exist.') return if all([settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY, settings.AWS_STORAGE_BUCKET_NAME, settings.AWS_LOCATION]): name = '%s/%s' % (settings.AWS_LOCATION, prefix) conn = boto.connect_s3(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY) bucket = conn.get_bucket(settings.AWS_STORAGE_BUCKET_NAME) for item in bucket.list(prefix=name): s3_file_relative_path = item.name.replace(name, '').lstrip('/') copy_to_fullpath = os.path.join(to_dir, s3_file_relative_path) copy_to_dir = os.path.dirname(copy_to_fullpath) if not os.path.isdir(copy_to_dir): # directory not exists, create it os.makedirs(copy_to_dir) if update_only and os.path.isfile(copy_to_fullpath): # check if this file from s3 has been modified. # if not modified, no need to update. src_modified_dt = dparser.parse(item.last_modified) dst_modified_dt = datetime.fromtimestamp(os.path.getmtime(copy_to_fullpath)) # adjust the timezone for dst_modified_dt # to compare the modified date time in the same time zone dst_modified_dt = adjust_datetime_to_timezone( dst_modified_dt, from_tz=settings.TIME_ZONE, to_tz=src_modified_dt.tzname()) if dst_modified_dt == src_modified_dt: # source is current, no need to update print('Not modified %s' % s3_file_relative_path) continue elif dst_modified_dt > src_modified_dt: print("Not updated. %s is current." % s3_file_relative_path) continue if dry_run: print('Pretended to download %s' % s3_file_relative_path) else: item.get_contents_to_filename(copy_to_fullpath) print('Downloaded %s' % s3_file_relative_path)