Пример #1
0
 def save_model(self, request, obj, form, change):
     utz = request.user.get_profile().timezone
     if obj.start:
         obj.start = adjust_datetime_to_timezone(obj.start, utz, TZ)
     if obj.end:
         obj.end = adjust_datetime_to_timezone(obj.end, utz, TZ)
     obj.save()
Пример #2
0
 def created_local(self):
     if hasattr(self.service.user, 'get_profile'):
         return adjust_datetime_to_timezone(
             self.created,
             'UTC',
             unicode(self.service.user.get_profile().timezone)
         )
     else:
         return adjust_datetime_to_timezone(self.created, 'UTC', TIME_ZONE)
Пример #3
0
def get_ievent(request, d, event_id):
    from django.conf import settings
    from timezones.utils import adjust_datetime_to_timezone
    from tendenci.addons.events.models import Event

    site_url = get_setting('site', 'global', 'siteurl')

    event = Event.objects.get(id=event_id)
    e_str = "BEGIN:VEVENT\n"

    # organizer
    organizers = event.organizer_set.all()
    if organizers:
        organizer_name_list = [organizer.name for organizer in organizers]
        e_str += "ORGANIZER:%s\n" % (', '.join(organizer_name_list))

    # date time
    if event.start_dt:
        start_dt = adjust_datetime_to_timezone(event.start_dt,
                                               settings.TIME_ZONE, 'GMT')
        start_dt = start_dt.strftime('%Y%m%dT%H%M%SZ')
        e_str += "DTSTART:%s\n" % (start_dt)
    if event.end_dt:
        end_dt = adjust_datetime_to_timezone(event.end_dt, settings.TIME_ZONE,
                                             'GMT')
        end_dt = end_dt.strftime('%Y%m%dT%H%M%SZ')
        e_str += "DTEND:%s\n" % (end_dt)

    # location
    if event.place:
        e_str += "LOCATION:%s\n" % (event.place.name)

    e_str += "TRANSP:OPAQUE\n"
    e_str += "SEQUENCE:0\n"

    # uid
    e_str += "UID:uid%d@%s\n" % (event.pk, d['domain_name'])

    event_url = "%s%s" % (site_url, reverse('event', args=[event.pk]))
    d['event_url'] = event_url

    # text description
    e_str += "DESCRIPTION:%s\n" % (build_ical_text(event, d))
    #  html description
    e_str += "X-ALT-DESC;FMTTYPE=text/html:%s\n" % (build_ical_html(event, d))

    e_str += "SUMMARY:%s\n" % strip_tags(event.title)
    e_str += "PRIORITY:5\n"
    e_str += "CLASS:PUBLIC\n"
    e_str += "BEGIN:VALARM\n"
    e_str += "TRIGGER:-PT30M\n"
    e_str += "ACTION:DISPLAY\n"
    e_str += "DESCRIPTION:Reminder\n"
    e_str += "END:VALARM\n"
    e_str += "END:VEVENT\n"

    return e_str
Пример #4
0
 def created_local(self):
     """Calculate the created time using timexone to give a datetime
     in the users time zone."""
     if hasattr(self, 'created'):
         if hasattr(self.user, 'get_profile'):
             return adjust_datetime_to_timezone(self.created, 'UTC', unicode(self.user.get_profile().timezone))
         else:
             return adjust_datetime_to_timezone(self.created, 'UTC', TIME_ZONE)
     else:
         return None
Пример #5
0
def get_ievent(request, d, event_id):
    from django.conf import settings
    from timezones.utils import adjust_datetime_to_timezone
    from events.models import Event
    
    site_url = get_setting('site', 'global', 'siteurl')
    
    event = Event.objects.get(id=event_id)
    e_str = "BEGIN:VEVENT\n"
    
    # organizer
    organizers = event.organizer_set.all()
    if organizers:
        organizer_name_list = [organizer.name for organizer in organizers]
        e_str += "ORGANIZER:%s\n" % (', '.join(organizer_name_list))
    
    # date time 
    if event.start_dt:
        start_dt = adjust_datetime_to_timezone(event.start_dt, settings.TIME_ZONE, 'GMT')
        start_dt = start_dt.strftime('%Y%m%dT%H%M%SZ')
        e_str += "DTSTART:%s\n" % (start_dt)
    if event.end_dt:
        end_dt = adjust_datetime_to_timezone(event.end_dt, settings.TIME_ZONE, 'GMT')
        end_dt = end_dt.strftime('%Y%m%dT%H%M%SZ')
        e_str += "DTEND:%s\n" % (end_dt)
    
    # location
    if event.place:
        e_str += "LOCATION:%s\n" % (event.place.name)
        
    e_str += "TRANSP:OPAQUE\n"
    e_str += "SEQUENCE:0\n"
    
    # uid
    e_str += "UID:uid%d@%s\n" % (event.pk, d['domain_name'])
    
    event_url = "%s%s" % (site_url, reverse('event', args=[event.pk]))
    d['event_url'] = event_url
    
    # text description
    e_str += "DESCRIPTION:%s\n" % (build_ical_text(event,d))
    #  html description
    e_str += "X-ALT-DESC;FMTTYPE=text/html:%s\n" % (build_ical_html(event,d))
    
    e_str += "SUMMARY:%s\n" % strip_tags(event.title)
    e_str += "PRIORITY:5\n"
    e_str += "CLASS:PUBLIC\n"
    e_str += "BEGIN:VALARM\n"
    e_str += "TRIGGER:-PT30M\n"
    e_str += "ACTION:DISPLAY\n"
    e_str += "DESCRIPTION:Reminder\n"
    e_str += "END:VALARM\n"
    e_str += "END:VEVENT\n"
        
    return e_str
 def forwards(self, orm):
     articles = orm['articles.Article'].objects.all()
     now = datetime.datetime.now()
     now_with_tz = adjust_datetime_to_timezone(now, settings.TIME_ZONE) 
     for article in articles:
         if article.timezone and article.release_dt and article.timezone.zone != settings.TIME_ZONE:
             time_diff = adjust_datetime_to_timezone(now, article.timezone) - now_with_tz
             article.release_dt_local = article.release_dt + time_diff
         else:
             article.release_dt_local = article.release_dt
         # only update the release_dt_local field
         orm['articles.Article'].objects.filter(id=article.id).update(release_dt_local=article.release_dt_local)
 def forwards(self, orm):
     all_news = orm['news.News'].objects.all()
     now = datetime.datetime.now()
     now_with_tz = adjust_datetime_to_timezone(now, settings.TIME_ZONE) 
     for news in all_news:
         if news.timezone and news.release_dt and news.timezone.zone != settings.TIME_ZONE:
             time_diff = adjust_datetime_to_timezone(now, news.timezone) - now_with_tz
             news.release_dt_local = news.release_dt + time_diff
         else:
             news.release_dt_local = news.release_dt
         # only update the release_dt_local field
         orm['news.News'].objects.filter(id=news.id).update(release_dt_local=news.release_dt_local)
 def forwards(self, orm):
     articles = orm['articles.Article'].objects.all()
     now = datetime.datetime.now()
     now_with_tz = adjust_datetime_to_timezone(now, settings.TIME_ZONE)
     for article in articles:
         if article.timezone and article.release_dt and article.timezone.zone != settings.TIME_ZONE:
             time_diff = adjust_datetime_to_timezone(
                 now, article.timezone) - now_with_tz
             article.release_dt_local = article.release_dt + time_diff
         else:
             article.release_dt_local = article.release_dt
         # only update the release_dt_local field
         orm['articles.Article'].objects.filter(id=article.id).update(
             release_dt_local=article.release_dt_local)
Пример #9
0
 def forwards(self, orm):
     all_news = orm['news.News'].objects.all()
     now = datetime.datetime.now()
     now_with_tz = adjust_datetime_to_timezone(now, settings.TIME_ZONE)
     for news in all_news:
         if news.timezone and news.release_dt and news.timezone.zone != settings.TIME_ZONE:
             time_diff = adjust_datetime_to_timezone(
                 now, news.timezone) - now_with_tz
             news.release_dt_local = news.release_dt + time_diff
         else:
             news.release_dt_local = news.release_dt
         # only update the release_dt_local field
         orm['news.News'].objects.filter(id=news.id).update(
             release_dt_local=news.release_dt_local)
Пример #10
0
 def test_adjust_datetime_to_timezone(self):
     self.assertEqual(
         adjust_datetime_to_timezone(
             datetime(2008, 6, 25, 18, 0, 0), "UTC"
         ).strftime("%m/%d/%Y %H:%M:%S"),
         "06/25/2008 18:00:00"
     )
Пример #11
0
 def coerce_datetime_tz(self, datetime):
     server_tz = settings.TIME_ZONE
     if self.instance is None or self.instance.creator is None:
         user_tz = self.creator.get_profile().timezone
     else:
         user_tz = self.instance.creator.get_profile().timezone
     return tz_utils.adjust_datetime_to_timezone(datetime, user_tz, server_tz)
Пример #12
0
 def test_adjust_datetime_to_timezone(self):
     self.assertEqual(
         adjust_datetime_to_timezone(
             datetime(2008, 6, 25, 18, 0, 0), "UTC"
         ).strftime("%m/%d/%Y %H:%M:%S"),
         "06/25/2008 18:00:00"
     )
Пример #13
0
 def coerce_datetime_tz(self, datetime):
     server_tz = settings.TIME_ZONE
     if self.instance is None or self.instance.creator is None:
         user_tz = self.creator.get_profile().timezone
     else:
         user_tz = self.instance.creator.get_profile().timezone
     return tz_utils.adjust_datetime_to_timezone(datetime, user_tz,
                                                 server_tz)
Пример #14
0
 def local_send_time(self):
     if self.billee.timezone:
         return adjust_datetime_to_timezone(
             self.send_date,
             settings.TIME_ZONE,
             self.billee.timezone
         )
     return self.send_date
Пример #15
0
 def local_send_time(self):
     if self.billee.timezone:
         return adjust_datetime_to_timezone(
             self.send_date,
             settings.TIME_ZONE,
             self.billee.timezone
         )
     return self.send_date
Пример #16
0
    def clean_start (self):
        date = self.cleaned_data["start"]
        if self.timezone:
            print "IN CLEAN START EDIT"
            print date
            date = adjust_datetime_to_timezone (date, self.timezone)
            print date

        return date
Пример #17
0
 def stop_date_localized(self):
     """
     return localized stop date
     """
     from timezones.utils import adjust_datetime_to_timezone
     return adjust_datetime_to_timezone(
         self.stop_date,
         settings.TIME_ZONE, 
         self.user.get_profile().timezone)
Пример #18
0
    def clean_end(self):
        date = self.cleaned_data["end"]
        if self.timezone:
            print "IN CLEAN END EDIT"
            print date
            date = adjust_datetime_to_timezone(date, self.timezone)
            print date

        return date
Пример #19
0
    def clean_start(self):
        date = self.cleaned_data["start"]
        if self.timezone:
            print "IN CLEAN START EDIT"
            print date
            date = adjust_datetime_to_timezone(date, self.timezone)
            print date

        return date
Пример #20
0
 def local_send_time(self):
     # TODO: Get this from UserProfile?
     if getattr(self.billee, 'timezone', None):
         return adjust_datetime_to_timezone(
             self.send_date,
             settings.TIME_ZONE,
             self.billee.timezone
         )
     return self.send_date
Пример #21
0
    def clean_end (self):
        date = self.cleaned_data["end"]
        if self.timezone:
            print "IN CLEAN END EDIT"
            print date
            date = adjust_datetime_to_timezone (date, self.timezone)
            print date

        return date
Пример #22
0
    def clean_completed_at(self):
        value = self.cleaned_data['completed_at']
        if value is None:
            return value

        time_zone = self.task.user.get_profile().time_zone
        localtime = value.replace(tzinfo=pytz.timezone(time_zone))
        utctime = adjust_datetime_to_timezone(localtime, time_zone, 'UTC')
        utctime_raw = utctime.replace(tzinfo=None)
        return utctime_raw
Пример #23
0
    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
Пример #24
0
def localize_date(value, to_tz=None):
    from timezones.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 ''
Пример #25
0
 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
Пример #26
0
def localize_date(value, to_tz=None):
    from timezones.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 ''
Пример #27
0
    def clean_start (self):
        date = self.cleaned_data["start"]
        if date < datetime.datetime.now ():
            raise forms.ValidationError ("Starting event date can not be in the past")

        print "IN CLEAN START"

        if self.timezone:
            print date
            date = adjust_datetime_to_timezone (date, self.timezone)
            print date
        return date
Пример #28
0
    def clean_end (self):
        date = self.cleaned_data["end"]
        if date < datetime.datetime.now ():
            raise forms.ValidationError ("Ending event date can not be in the past")

        print "IN CLEAN END"

        if self.timezone:
            print date
            date = adjust_datetime_to_timezone (date, self.timezone)
            print date
        return date
Пример #29
0
def now_localized():
    from timezones.utils import adjust_datetime_to_timezone
    from time import strftime, gmtime

    os_timezone = strftime("%Z", gmtime())
    if os_timezone == "CST":
        os_timezone = "US/Central"
    django_timezone = settings.TIME_ZONE

    now = adjust_datetime_to_timezone(datetime.now(), from_tz=os_timezone, to_tz=django_timezone)
    now = now.replace(tzinfo=None)
    return now
Пример #30
0
    def get_dated_queryset(self, order='asc', **lookup):
        d = self.date
        date_field = self.get_date_field()
        qs = self.get_queryset().filter(**lookup)

        period = self.period(d)
        date_range = (period.start, period.finish)
        date_range = [adjust_datetime_to_timezone(
                     i, settings.TIME_ZONE, self.timezone) for i in date_range]
        date_range = [i.replace(tzinfo=None) for i in date_range]
        filter_kwargs = {'%s__range' % date_field: date_range}
        order = '' if order == 'asc' else '-'
        return qs.filter(**filter_kwargs).order_by("%s%s" % (order, date_field))
Пример #31
0
def now_localized():
    from timezones.utils import adjust_datetime_to_timezone
    from time import strftime, gmtime

    os_timezone = strftime('%Z', gmtime())
    if os_timezone == 'CST': os_timezone = 'US/Central'
    django_timezone = settings.TIME_ZONE

    now = adjust_datetime_to_timezone(datetime.now(),
                                      from_tz=os_timezone,
                                      to_tz=django_timezone)
    now = now.replace(tzinfo=None)
    return now
Пример #32
0
    def clean_start(self):
        date = self.cleaned_data["start"]
        if date < datetime.datetime.now():
            raise forms.ValidationError(
                "Starting event date can not be in the past")

        print "IN CLEAN START"

        if self.timezone:
            print date
            date = adjust_datetime_to_timezone(date, self.timezone)
            print date
        return date
Пример #33
0
    def clean_end(self):
        date = self.cleaned_data["end"]
        if date < datetime.datetime.now():
            raise forms.ValidationError(
                "Ending event date can not be in the past")

        print "IN CLEAN END"

        if self.timezone:
            print date
            date = adjust_datetime_to_timezone(date, self.timezone)
            print date
        return date
Пример #34
0
def localize_date(date, from_tz=None, to_tz=None):
    """
    Convert from one timezone to another
    """
    # set the defaults
    if from_tz is None:
        from_tz = settings.TIME_ZONE

    if to_tz is None:
        to_tz = "US/Central"

    date = adjust_datetime_to_timezone(date, from_tz=from_tz, to_tz=to_tz)
    date = date.replace(tzinfo=None)
    return date
Пример #35
0
def localize_date(date, from_tz=None, to_tz=None):
    """
    Convert from one timezone to another
    """
    # set the defaults
    if from_tz is None:
        from_tz = settings.TIME_ZONE

    if to_tz is None:
        to_tz = "US/Central"

    date = adjust_datetime_to_timezone(date, from_tz=from_tz, to_tz=to_tz)
    date = date.replace(tzinfo=None)
    return date
 def test_localized_occurrence_proxy_wrapping_proxy_works_properly(self):
     timezone = 'Antarctica/McMurdo'
     localized = LocalizedOccurrenceProxy(
         self.localized, timezone=timezone
     )
     assert_equal(localized.real_start, self.occurrence.start)
     assert_equal(localized.real_finish, self.occurrence.finish)
     for attr in ('start', 'finish'):
         expected = adjust_datetime_to_timezone(
             getattr(self.occurrence, attr),
             settings.TIME_ZONE,
             timezone
         )
         assert_equal(getattr(localized, attr), expected)
Пример #37
0
def download_files_from_s3(prefix='',
                           to_dir='',
                           update_only=False,
                           dry_run=False):
    if not prefix:
        print('No prefix, exiting..')
        return
    if not os.path.isdir(to_dir):
        print('Destination directory does not exist.')
        return
    name = '%s/%s' % (settings.PROJECT_NAME, prefix)
    q = Auth(settings.QINIU_ACCESS_KEY, settings.QINIU_SECRET_KEY)
    bucket = BucketManager(q)
    ret, eof, info = bucket.list(settings.QINIU_BUCKET_NAME, prefix)
    for item in ret.get('items'):
        qiniu_file_relative_path = item.name.replace(name, '').lstrip('/')
        copy_to_fullpath = os.path.join(to_dir, qiniu_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' % qiniu_file_relative_path)
                continue

            elif dst_modified_dt > src_modified_dt:
                print("Not updated. %s is current." % qiniu_file_relative_path)
                continue

        if dry_run:
            print('Pretended to download %s' % qiniu_file_relative_path)
        else:
            item.get_contents_to_filename(copy_to_fullpath)
            print('Downloaded %s' % qiniu_file_relative_path)
Пример #38
0
def localize_date(date, from_tz=None, to_tz=None):
    """
        Takes the given date/timezone
        and converts it to the sites date/timezone

        localize_date(date, from_tz, to_tz=None)
    """
    from timezones.utils import adjust_datetime_to_timezone

    # set the defaults
    if from_tz is None:
        from_tz = settings.TIME_ZONE

    if to_tz is None:
        to_tz = settings.TIME_ZONE

    return adjust_datetime_to_timezone(date, from_tz=from_tz, to_tz=to_tz)
Пример #39
0
def localize_date(date, from_tz=None, to_tz=None):
    """
        Takes the given date/timezone
        and converts it to the sites date/timezone

        localize_date(date, from_tz, to_tz=None)
    """
    from timezones.utils import adjust_datetime_to_timezone

    # set the defaults
    if from_tz is None:
        from_tz=settings.TIME_ZONE

    if to_tz is None:
        to_tz=settings.TIME_ZONE

    return adjust_datetime_to_timezone(date,from_tz=from_tz,to_tz=to_tz)
Пример #40
0
def index(request):
    time_zone = request.user.get_profile().time_zone
    localtime = localtime_for_timezone(datetime.datetime.now(),time_zone)
    start_of_today = localtime.replace(hour=0,minute=0,second=0,microsecond=0)
    start_of_today_utc = adjust_datetime_to_timezone(start_of_today, time_zone, 'UTC')
    start_of_today_utc_raw = start_of_today_utc.replace(tzinfo=None)

    pending_tasks = Task.active.filter(
        user = request.user,
        completed_at__isnull = True,
        backlogged = False
    ).order_by('-created_at')

    completed_today = Task.active.filter(
        user = request.user,
        completed_at__gte = start_of_today_utc_raw
    ).order_by('-created_at')

    if request.REQUEST.has_key('days_back') and request.REQUEST['days_back'].isdigit():
        days_back = int(request.REQUEST['days_back']) + 1
    else:
        days_back = DEFAULT_DAYS_BACK

    completed_lists = [
        Task.active.filter(
            user = request.user,
            completed_at__gte = start_of_today_utc_raw - datetime.timedelta(days=delta),
            completed_at__lt = start_of_today_utc_raw - datetime.timedelta(days=delta) + datetime.timedelta(days=1)
        ).order_by('-created_at')
        for delta in range(1, days_back)
    ]

    return render_to_response('tasks/index.html',
        {
            'time_zone': time_zone,
            'new_task_form' : CreateTaskForm(request.user),
            'pending_tasks' : pending_tasks,
            'completed_today' : completed_today,
            'completed_lists': completed_lists
        },
        context_instance=RequestContext(request)
    )
    def test_occurrences_localized_correctly(self):
        timezone = 'Antarctica/McMurdo'
        occurrences = Occurrence.objects.all()
        localized_year = Year(
            self.start, occurrences=occurrences, timezone=timezone
        )
        assert_equal(localized_year.timezone, pytz.timezone(timezone))
        assert self.year.timezone is None

        for o in localized_year.occurrences:
            assert isinstance(o, LocalizedOccurrenceProxy)
        for localized_month, month in zip(localized_year, self.year):
            for localized_o, o in zip(localized_month.occurrences, occurrences):
                assert_equal(localized_o.real_start, o.start.replace(tzinfo=None))
                assert_equal(localized_o.real_finish, o.finish.replace(tzinfo=None))
                for attr in ('start', 'finish'):
                    expected = adjust_datetime_to_timezone(
                        getattr(o, attr), settings.TIME_ZONE, timezone
                    )
                    assert_equal(getattr(localized_o, attr), expected)
Пример #42
0
 def clean(self, value):
     value = super(LocalizedDateTimeField, self).clean(value)
     return adjust_datetime_to_timezone(value, from_tz=self.timezone)
Пример #43
0
 def clean(self, value):
     value = super(LocalizedDateTimeField, self).clean(value)
     return adjust_datetime_to_timezone(value, from_tz=self.timezone)
Пример #44
0
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)
Пример #45
0
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
Пример #46
0
 def clean(self, value):
     value = super(LocalizedDateTimeField, self).clean(value)
     if value is None: # field was likely not required
         return None
     return adjust_datetime_to_timezone(value, from_tz=self.timezone)
Пример #47
0
def usertime_to_servertime(value):
    return adjust_datetime_to_timezone(value, settings.USER_TIME_ZONE,
                                       settings.TIME_ZONE)
Пример #48
0
def from_localtime(value, timezone):
    return adjust_datetime_to_timezone(value, timezone)
 def clean(self, value):
     value = super(LocalizedDateTimeField, self).clean(value)
     if value is None:  # field was likely not required
         return None
     return adjust_datetime_to_timezone(value, from_tz=self.timezone)
Пример #50
0
def localtime_for_timezone(value, timezone):
    """
    Given a ``datetime.datetime`` object in UTC and a timezone represented as
    a string, return the localized time for the timezone.
    """
    return adjust_datetime_to_timezone(value, settings.TIME_ZONE, timezone)
Пример #51
0
 def adjust_tz(self, dt):
     dt = adjust_datetime_to_timezone(dt, **self.tz_params)
     dt = dt.replace(tzinfo=None)
     return dt