def register_history_bets(s3_bucket_name, filename, user_id, tz_string, commit): """ Notice that it can register total bets both for open events and for events with results. In case of events with results, you can have existing closed bet events or new bet events will be created """ # s3_bucket_name = 'zakanda-static-01' # filename = 'past_bets.xlsx' # user_id = 29 # tz_string = 'Europe/Athens' user = get_user(user_id=user_id ) # 10:c, 26:p, 23:t2, 27:p1, 25:bob, 28:liono, 29:yahdim try: tz = pytz.timezone(tz_string) except Exception as e: logger.error(e) tz = None if not s3_bucket_name or not filename or not user or not tz: logger.error('arguments error!') return s3_obj = utils.get_s3_obj(bucket=s3_bucket_name, filename=filename) # s3_obj = join(dirname(dirname(abspath(__file__))), 'fixtures', filename) # read file from local disk work_book, data = utils.read_data(s3_obj) utils.remove_empty_rows(data) # datemode: Which date system was in force when this file was last saved.<br /> # 0 => 1900 system (the Excel for Windows default).<br /> # 1 => 1904 system (the Excel for Macintosh default).<br /> datemode = work_book.datemode read_tbs = create_read_objects(user, data, datemode, tz=tz) if not commit: utils.check_validity(read_tbs) else: trees_created = utils.create_zakanda_bet_trees(read_tbs) if trees_created: user.profile.settle_total_bets(call_api=True)
def user_from_id(user_id): try: obj = get_user(int(user_id)) except Exception as e: logger.warning('%s', e) obj = None return obj
def email_user(target_user_id, html_template, subject, extra_context=None): """ :param target_user_id: the user to send the email to :param html_template: it must be relative to this file location ('emails/template.html') :param subject: :param extra_context: dictionary :return: """ user = get_user(target_user_id) if not user: return domain = Site.objects.get_current().domain context = { 'domain': domain, 'site': Site.objects.get_current(), 'user': user } if isinstance(extra_context, dict): context.update(extra_context) html_content = render_to_string(html_template, context) text_content = strip_tags( html_content ) # this strips the html, so people will have the text as well. msg = EmailMultiAlternatives(subject, text_content, to=[user.email]) msg.attach_alternative(html_content, "text/html") msg.send()
def new_followers(target_user_id, hours): """ gets the followers of the given user that have been created within the last given hours """ user = get_user(target_user_id) if not user: return end_date = timezone.now() start_date = end_date - timezone.timedelta(hours=hours) qs = Follow.objects.followers_qs(user).filter(started__gt=start_date, started__lt=end_date) filtered_followers = [follow.user for follow in qs] return filtered_followers
def update_stored_stats(user_id): """ it updates the basic stats stored in db with the result of the stats calculation which is cached so the cached stats must be updated first """ if not user_id: return user = get_user(user_id) if not user: return try: user.basic_stats.update() except Exception as e: # if basic stats doesn't exist for this user basic_stats, created = BasicStats.objects.get_or_create(user=user) basic_stats.update() return
def email_user_for_new_followers(target_user_id, filtered_followers): user = get_user(target_user_id) if not user: return domain = Site.objects.get_current().domain context = { 'user': user, 'filtered_followers': filtered_followers, 'domain': domain, 'site': Site.objects.get_current() } subject = '{} you have new zakanda followers!'.format(user.username) html_content = render_to_string('emails/new_followers.html', context) text_content = strip_tags( html_content ) # this strips the html, so people will have the text as well. msg = EmailMultiAlternatives(subject, text_content, to=[user.email]) msg.attach_alternative(html_content, "text/html") msg.send()
def user_signed_up_(request, email_address, **kwargs): logger.debug("sending welcome email to {}...".format(email_address)) try: # email_address is an object user_id = email_address.user_id except Exception as e: user_id = None user = get_user(user_id) if not user: return subject = 'Welcome to zakanda {}'.format(user.username) context = { 'user': user, 'request': request, 'site': Site.objects.get_current() } html_content = render_to_string('founder_welcome.html', context) text_content = strip_tags( html_content ) # this strips the html, so people will have the text as well. msg = EmailMultiAlternatives(subject, text_content, to=[user.email]) msg.attach_alternative(html_content, "text/html") msg.send()