def auth_with_gplus(gplus_user, credentials, initial_user=None, is_test=False): email = gplus_user.get('emails')[0].get('value').lower() name = gplus_user.get('name', {}) first_name = name.get('givenName') last_name = name.get('familyName') username = initial_user.get('username') gplus_id = gplus_user.get('id') gender = gplus_user.get('gender') profile_fields = {} location = {} if initial_user: if initial_user.get('location'): location = initial_user.get('location') location_controller.add_predefined_city(location) elif initial_user.get('ip'): location = location_controller.from_ip(initial_user.get('ip')) profile_fields.update(location) profile_fields.update({'gender': gender}) try: # Todo: Check! # in rare cases when clients send multiple requests and while the first one is still in progress # (post save isn't yet completed i.e. no profile yet) the second one passes this check and breaks # in the coming lines of updating profile location as there is no profile. The result of such call can be even # worse as two users will be created user = User.objects.get(email=email) debug_logger.debug( 'Found user: {} with same email of gplus_user: {}'.format( user, gplus_id)) if location: location_controller.update_profile_location(user.profile, location, add_pc=False) except User.DoesNotExist: user = create_user(email=email, first_name=first_name, last_name=last_name, username=username, is_activated=True, profile_fields=profile_fields, is_test=is_test) if not user.is_activated: user.activate() if not user.profile.gender and gender: user.profile.update(gender=gender) credentials_json = credentials.to_json() try: LinkedGoogleAccount.objects.create(user=user, credentials_json=credentials_json, gplus_id=gplus_id) except IntegrityError as e: raise ShoutitBadRequest( message=_("Could not access your Google account, try again later"), developer_message=str(e)) image_url = gplus_user['image']['url'].split('?')[0] media_controller.set_profile_media(user.profile, 'image', image_url) return user
def edit_shout_v2(shout, shout_type=None, title=None, text=None, price=None, currency=None, category=None, images=None, videos=None, location=None, page_admin_user=None): item_controller.edit_item(shout.item, name=title, description=text, price=price, currency=currency, images=images, videos=videos) if shout_type: shout.type = shout_type if text: shout.text = text if category: shout.category = category if location: location_controller.update_object_location(shout, location, save=False) location_controller.add_predefined_city(location) if page_admin_user: shout.page_admin_user = page_admin_user shout.save() return shout
def auth_with_facebook(fb_user, initial_user=None, is_test=False): email = fb_user.get('email').lower() first_name = fb_user.get('first_name') last_name = fb_user.get('last_name') facebook_id = fb_user.get('id') gender = fb_user.get('gender') birthday = fb_user.get('birthday') username = initial_user and initial_user.get('username') location = initial_user and initial_user.get('location') profile_fields = {} update_fields = [] if location: location_controller.add_predefined_city( location) # Todo (mo): are we still using this? profile_fields.update(location) update_fields += location.keys() if gender: profile_fields.update({'gender': gender}) update_fields.append('gender') if birthday: fmt = "%m/%d/%Y" if birthday.count( '/') == 2 else '%d/%m' if birthday.count('/') == 1 else '%Y' birthday = datetime.strptime(birthday, fmt).date() profile_fields.update({'birthday': birthday}) update_fields.append('birthday') try: user = User.objects.filter( email=email).select_related('profile').first() if not user: raise User.DoesNotExist() profile = user.profile debug_logger.debug('Found user: %s with same email of fb_user: %s' % (user, facebook_id)) if location: location_controller.update_object_location(profile, location, save=False) if gender: profile.gender = gender profile.save(update_fields=update_fields) except User.DoesNotExist: user = create_user(email=email, first_name=first_name, last_name=last_name, username=username, is_activated=True, profile_fields=profile_fields, is_test=is_test) if not user.is_activated: user.activate() return user
def user_from_guest_data(initial_gust_user, is_test=False): profile_fields = {} location = {} if initial_gust_user.get('location'): location = initial_gust_user.get('location') location_controller.add_predefined_city(location) elif initial_gust_user.get('ip'): location = location_controller.from_ip(initial_gust_user.get('ip')) profile_fields.update(location) return create_user(is_test=bool(is_test), is_guest=True, profile_fields=profile_fields)
def edit_shout(shout, title=None, text=None, price=None, currency=None, category=None, filters=None, images=None, videos=None, location=None, expires_at=None, page_admin_user=None, available_count=None, is_sold=None, mobile=None, publish_to_facebook=None): item_controller.edit_item(shout.item, name=title, description=text, price=price, currency=currency, images=images, videos=videos, available_count=available_count, is_sold=is_sold) # Can be unset shout.text = text shout.mobile = mobile shout.expires_at = expires_at if filters is not None: tag_ids = map(lambda f: f['value']['id'], filters) tags = Tag.objects.filter(id__in=tag_ids) shout.tags.clear() shout.tags.add(*tags) # Can't be unset if category is not None: shout.category = category if location is not None: location_controller.update_object_location(shout, location, save=False) location_controller.add_predefined_city(location) if page_admin_user is not None: shout.page_admin_user = page_admin_user shout.publish_to_facebook = publish_to_facebook shout.save() return shout
def create_shout_v2(user, shout_type, title, text, price, currency, category, location, images=None, videos=None, published_at=None, exp_days=None, priority=0, page_admin_user=None, publish_to_facebook=None, api_client=None, api_version=None): # item item = item_controller.create_item(name=title, description=text, price=price, currency=currency, images=images, videos=videos) shout = Shout(user=user, type=shout_type, text=text, category=category, item=item, priority=priority, page_admin_user=page_admin_user) location_controller.update_object_location(shout, location, save=False) # Published and Expires if published_at: shout.published_at = published_at if exp_days: shout.expires_at = published_at + timedelta(days=exp_days) shout.api_client, shout.api_version = api_client, api_version shout.publish_to_facebook = publish_to_facebook shout.save() location_controller.add_predefined_city(location) return shout
def user_from_shoutit_signup_data(signup_data, initial_user=None, is_test=False): email = signup_data.get('email') password = signup_data.get('password') first_name = signup_data.get('first_name') last_name = signup_data.get('last_name') username = initial_user.get('username') profile_fields = {} location = {} if initial_user: if initial_user.get('location'): location = initial_user.get('location') location_controller.add_predefined_city(location) elif initial_user.get('ip'): location = location_controller.from_ip(initial_user.get('ip')) profile_fields.update(location) return create_user(email=email, password=password, first_name=first_name, last_name=last_name, username=username, is_test=bool(is_test), profile_fields=profile_fields)
def create_shout(user, shout_type, title, text, price, currency, category, location, filters=None, images=None, videos=None, published_at=None, is_sss=False, exp_days=None, expires_at=None, priority=0, page_admin_user=None, publish_to_facebook=None, available_count=None, is_sold=None, mobile=None, api_client=None, api_version=None): # Create the Item item = item_controller.create_item(name=title, description=text, price=price, currency=currency, images=images, videos=videos, available_count=available_count, is_sold=is_sold) # Prepare the Shout shout = Shout(user=user, type=shout_type, text=text, category=category, item=item, filters=filters, mobile=mobile, is_sss=is_sss, priority=priority, page_admin_user=page_admin_user) location_controller.update_object_location(shout, location, save=False) # Published and Expires if published_at: shout.published_at = published_at if exp_days is not None or expires_at is not None: shout.expires_at = now_plus_delta( days=exp_days) if exp_days is not None else expires_at # Set attributes for post saving and tracking shout.api_client, shout.api_version = api_client, api_version shout.publish_to_facebook = publish_to_facebook # Save try: # Don't save the index, wait for the tags shout.save_shout_index = False shout.save() except (ValidationError, IntegrityError): item.delete() raise else: # Tags if filters: tag_ids = map(lambda f: f['value']['id'], filters) tags = Tag.objects.filter(id__in=tag_ids) shout.tags.add(*tags) # Now save the index save_shout_index(shout=shout, created=True) location_controller.add_predefined_city(location) return shout