Skip to content

Instantly share code, notes, and snippets.

@db0company
Created April 13, 2018 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save db0company/ca57643cf3451c989cb0958c927d6d35 to your computer and use it in GitHub Desktop.
Save db0company/ca57643cf3451c989cb0958c927d6d35 to your computer and use it in GitHub Desktop.
# Original calendar thingy
@property
def availability_calendar(self):
calendar = [['', 'M', 'T', 'W', 'T', 'F', 'S', 'S']] + [
[self._I_TO_AVAILABILITY[i] if j == 0 else 'no' for j in range(8)]
for i in range(len(self.AVAILABILITY_CHOICES))
]
for a, v in self.availability.items():
if v:
for i in range(5):
calendar[self._AVAILABILITY_TO_I[a]][i + 1] = 'yes'
for a, v in self.weekend_availability.items():
if v:
for i in range(5, 7):
calendar[self._AVAILABILITY_TO_I[a]][i + 1] = 'yes'
return calendar
# With timezones
def _strtime_to_int(self, time):
time = time.replace('am', '')
if time == 'midnight':
time = 0
elif time == 'noon':
time = 12
elif 'pm' in time:
time = int(time.replace('pm', '')) + 12
else:
time = int(time)
return time
def _int_to_strtime(self, integer):
if integer == 0:
return 'midnight'
elif integer == 12:
return 'noon'
if integer > 12:
return u'{} pm'.format(integer - 12)
return u'{} am'.format(integer)
def _hour_timezone_convert(self, hour, other_timezone):
today = datetime.datetime.today()
past_monday = today - datetime.timedelta(days=today.weekday())
new_date = past_monday.replace(hour=hour, tzinfo=pytz.timezone(self.timezone))
new_date_with_other_timezone = new_date.astimezone(pytz.timezone(other_timezone))
return new_date_with_other_timezone.hour
def _availability_time_to_timezone(self, time, other_timezone):
new_time = []
for time in time.split('-'):
integer_time = self._strtime_to_int(time)
integer_other_timezone = self._hour_timezone_convert(integer_time, other_timezone)
strtime_other_timezone = self._int_to_strtime(integer_other_timezone)
new_time.append(strtime_other_timezone)
return ' - '.join(new_time)
def availability_calendar_timezone(self, other_timezone=None):
weekdays = ['M', 'T', 'W', 'T', 'F', 'S', 'S']
if self.timezone and other_timezone:
padding = 2
weekdays = [self.timezone, other_timezone] + weekdays
def to_default_value(i, j):
if j == 0:
return self._I_TO_T_AVAILABILITY[i]
elif j == 1:
return self._availability_time_to_timezone(self._I_TO_AVAILABILITY[i], other_timezone)
return 'no'
else:
padding = 1
weekdays = [''] + weekdays
def to_default_value(i, j):
if j == 0:
return self._I_TO_AVAILABILITY[i]
return 'no'
calendar = [weekdays] + [
[to_default_value(i, j) for j in range(7 + padding)]
for i in range(len(self.AVAILABILITY_CHOICES))
]
for a, v in self.availability.items():
if v:
for i in range(5):
calendar[self._AVAILABILITY_TO_I[a]][i + padding] = 'yes'
for a, v in self.weekend_availability.items():
if v:
for i in range(5, 7):
calendar[self._AVAILABILITY_TO_I[a]][i + padding] = 'yes'
return calendar
@property
def availability_calendar(self):
return self.availability_calendar_timezone()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment