def v2_linked_accounts(self): linked_facebook = getattr(self, 'linked_facebook', None) has_linked_gplus = hasattr(self, 'linked_gplus') _linked_accounts = { 'facebook': linked_facebook is not None, 'gplus': has_linked_gplus, } if linked_facebook: _linked_accounts['facebook_details'] = { 'facebook_id': linked_facebook.facebook_id, 'access_token': linked_facebook.access_token, 'expires_at': date_unix(linked_facebook.expires_at), 'scopes': linked_facebook.scopes } return _linked_accounts
def _subscribe_users_to_mailing_list(users=None, user_ids=None, raise_errors=True): if users is None: users = User.objects.filter(id__in=user_ids) request_body = [] for user in users: ap = user.ap fields = { 'email': user.email, 'first_name': user.first_name, 'last_name': user.last_name, 'is_active': 'true' if user.is_active else 'false', 'is_activated': 'true' if user.is_activated else 'false', 'type': user.v3_type_name, 'username': user.username, 'date_joined': date_unix(user.date_joined), 'last_login': date_unix(user.last_login) if user.last_login else 0, 'country': ap.country, 'image': ap.image, 'platform': " ".join( map(lambda c: str(c.replace('shoutit-', '')), user.api_client_names)), 'gender': getattr(ap, 'gender', ''), } request_body.append(fields) try: response = sg_api.client.contactdb.recipients.post( request_body=request_body) response_data = json.loads(response.response_body) # Check added if response_data['new_count'] > 0: debug_logger.debug("Added %d user(s) to SendGrid Contacts DB" % response_data['new_count']) # Check updated if response_data['updated_count'] > 0: debug_logger.debug("Updated %d user(s) on SendGrid Contacts DB" % response_data['updated_count']) # Update added / updated users added_emails = map(lambda pr: base64.b64decode(pr), response_data['persisted_recipients']) User.objects.filter(email__in=added_emails).update( on_mailing_list=True) # Errors if response_data['error_count'] > 0: debug_logger.warning( "Error adding/updating %d user(s) to SendGrid contacts db" % response_data['error_count']) debug_logger.warning(response_data['errors']) raise ValueError(response_data['errors']) except Exception as e: debug_logger.warning("Error adding/updating %d users to SendGrid: %s" % (len(users), str(e))) if raise_errors: raise
def modified_at_unix(self): return date_unix(self.modified_at)
def created_at_unix(self): return date_unix(self.created_at)
def published_at_unix(self): return date_unix(self.published_at)
def expires_at_unix(self): return date_unix(self.expires_at)
def expires_at_unix(self): return date_unix(self.expires_at) if self.days is not None else None
def to_representation(self, instance): return date_unix(instance)
def test_datetime_created_ascending_after(self): after = date_unix(self.dt_before(days=2, hours=1)) q_get = '?after={}'.format(after) resp = self.client.get(reverse(self.dt_created_asc_url) + q_get) self.assert200(resp) self.assert_ids_equal(self.get_results(resp), self.mms[2:], True)
def test_datetime_created_descending_before(self): before = date_unix(self.dt_before(days=1, hours=1)) q_get = '?before={}'.format(before) resp = self.client.get(reverse(self.dt_created_desc_url) + q_get) self.assert200(resp) self.assert_ids_equal(self.get_results(resp), self.mms[:3][::-1], True)