def home(request): users_count = User.objects.filter(is_superuser=False).count() male_units = ((u'пользователь', u'пользователя', u'пользователей'), 'm') users_count = num2text(users_count, male_units) money_count = 0 tarif_count = 0 for us in User.objects.filter(is_superuser=False): money_count = money_count + Post.objects.filter(user=us).last().money tarif = Tarif.objects.filter(user=us).last() if tarif != None: tarif_count = tarif_count + Tarif.objects.filter(user=us).last().money_for_mons/30 else: tarif_count = tarif_count + 0 female_units = ((u'гривна', u'гривен', u'гривен'), 'f') money_count = num2text(money_count, female_units) tarif_count = num2text(tarif_count, female_units) data = { 'users_count': users_count, 'money_count': money_count, 'tarif_count': tarif_count, 'commits':News.objects.filter(news_type=2).order_by('-created_date')[:3], 'news_wed':News.objects.filter(news_type=1).order_by('-created_date')[:3], } # e_send_on_50(request) return render(request, 'home.html', data,)
def get_contract_sum_string(self): rub = ((u'рубль', u'рубля', u'рублей'), 'm') kop = ((u'копейка', u'копейки', u'копеек'), 'f') #sum_str = num2text(self.contract_sum, rub) #sum_str = sum_str + ' ' + num2text((self.contract_sum - int(self.contract_sum))*100, kop) #sum_str = num2text((self.contract_sum - int(self.contract_sum))*100, female_units) #sum_str=self.contract_sum sum_arr = modf(self.contract_sum) sum_str = num2text(sum_arr[1], rub) + ' ' + num2text( sum_arr[0] * 100, kop) return sum_str
def user_detail(request, pk): m_user = get_object_or_404(User, username=pk) pr_user = UserProfile.objects.get(user_id=m_user.id) money_last = Post.objects.filter(user=m_user).order_by('created_date').last() tarif_last = Tarif.objects.filter(user=m_user).last() if tarif_last: from number_to_text import num2text male_units = ((u'день', u'дня', u'дней'), 'm') days_left = num2text(round(money_last.money/(tarif_last.money_for_mons/30)), male_units) else: days_left = None log_list = Logs.objects.filter(log_user=m_user).order_by('-log_date')[:10] for l in log_list: l.log_type = TYPE_CHOICES[int(l.log_type)-1][1] state= None return render(request, 'userprofile/user_profile.html', { 'm_user': m_user, 'pr_user': pr_user, 'money_last':money_last, 'tarif_last':tarif_last, 'days_left':days_left, 'log_list': log_list, 'state':state, 'LOGSTATUSYES':LOGSTATUSYES, 'LOGSTATUSNO':LOGSTATUSNO, })
def user_check_money(request, pk): m_user = get_object_or_404(User, username=pk) pr_user = UserProfile.objects.get(user_id=m_user.id) money_last = Post.objects.filter(user=m_user).order_by('created_date').last() tarif_last = Tarif.objects.filter(user=m_user).last() if timezone.localtime(timezone.now()) - money_last.created_date > timedelta(minutes=5): money_check(m_user) money_last = Post.objects.filter(user=m_user).order_by('created_date').last() tarif_last = Tarif.objects.filter(user=m_user).last() cheker = True else: state_type = 'warning' state_message = '<strong>Внимание!</strong> Принудительно проверять деньги чаще чем раз в пять минут нельзя!' cheker = False if tarif_last: male_units = ((u'день', u'дня', u'дней'), 'm') days_left = num2text(round(money_last.money/(tarif_last.money_for_mons/30)), male_units) else: days_left = None log_list = Logs.objects.filter(log_user=m_user).order_by('-log_date')[:10] for l in log_list: l.log_type = TYPE_CHOICES[int(l.log_type)-1][1] # print(log_list.first().log_status) if cheker != False: if log_list.first().log_status == LOGSTATUSNO: state_type = 'danger' state_message = '<strong>Ошибка!</strong> Что-то пошло не так. Попробуйте повторить немножко позже.' if log_list.first().log_status == LOGSTATUSYES: state_type = 'success' state_message = '<strong>Отлично!</strong> Все прошло как надо! Данные обновлены!' state = { 'state_type':state_type, 'state_message':state_message, } return render(request, 'userprofile/user_profile.html', { 'm_user': m_user, 'pr_user': pr_user, 'money_last':money_last, 'tarif_last':tarif_last, 'days_left':days_left, 'log_list': log_list, 'state':state, 'LOGSTATUSYES':LOGSTATUSYES, 'LOGSTATUSNO':LOGSTATUSNO, })
def user_preferences(request, pk): m_user = get_object_or_404(User, username=pk) pr_user = UserProfile.objects.get(user_id=m_user.id) money_last = Post.objects.filter(user=m_user).order_by('created_date').last() tarif_last = Tarif.objects.filter(user=m_user).last() if tarif_last: from number_to_text import num2text male_units = ((u'день', u'дня', u'дней'), 'm') days_left = num2text(round(money_last.money/(tarif_last.money_for_mons/30)), male_units) user_tarif = tarif_last else: days_left = None user_tarif = '' data = { 'user_tarif': user_tarif, 'user_email': m_user.email, 'user_vk': pr_user.vk_link, 'user_ok': pr_user.ok_link, } if request.method == 'POST': form = PreferencesUserForm(request.POST) if form.is_valid(): try: obj = Tarif.objects.get(user=m_user) obj.money_for_mons = form.cleaned_data['user_tarif'] obj.save() except Tarif.DoesNotExist: obj = Tarif(user = m_user, money_for_mons = form.cleaned_data['user_tarif']) obj.save() m_user.email = form.cleaned_data['user_email'] m_user.save() pr_user.vk_link = form.cleaned_data['user_vk'] pr_user.ok_link = form.cleaned_data['user_ok'] pr_user.save() return HttpResponseRedirect('/user/'+request.user.username) else: form = PreferencesUserForm(data, initial=data) return render(request, 'userprofile/preferences_user.html', { 'm_user': m_user, 'pr_user': pr_user, 'money_last':money_last, 'tarif_last':tarif_last, 'days_left':days_left, 'form': form, })
def e_send_on_email(request): subject = 'Wimagic-alert теперь знает Вашу почту!' money_last = Post.objects.filter(user=request.user).order_by('created_date').last() tarif_last = Tarif.objects.filter(user=request.user).last() male_units = ((u'день', u'дня', u'дней'), 'm') context = { 'title': subject, 'pr_user': UserProfile.objects.get(user_id=request.user.id), 'money_last': money_last, 'days_left': num2text(round(money_last.money/(tarif_last.money_for_mons/30)), male_units), } txt = render_to_string('email/on_email.txt', context) html = render_to_string('email/on_email.html', context) send_mail(subject, txt, EMAIL_HOST_USER, ['*****@*****.**'], fail_silently=False, html_message=html)
def e_send_on_50(request): subject = 'У Вас уже меньше 50грн. на балансе.' money_last = Post.objects.filter(user=request.user).order_by('created_date').last() tarif_last = Tarif.objects.filter(user=request.user).last() male_units = ((u'день', u'дня', u'дней'), 'm') # (tarif_last.money_for_mons/30) context = { 'title': subject, 'pr_user': UserProfile.objects.get(user_id=request.user.id), 'money_last': money_last, 'tarif_last':tarif_last.money_for_mons/30, 'days_left': num2text(round(money_last.money/(tarif_last.money_for_mons/30)), male_units), 'request':request, } txt = render_to_string('email/on_50.txt', context) html = render_to_string('email/on_50.html', context) send_mail(subject, txt, EMAIL_HOST_USER, ['*****@*****.**'], fail_silently=False, html_message=html)
def sender_email(): """ Выбрал пользователей, добавляю каждому пользователю метку о состоянии счета. """ tdelta = timedelta(seconds=50) male_units = ((u'день', u'дня', u'дней'), 'm') users = User.objects.filter(is_superuser=False) for user in users: money_last = Post.objects.filter(user=user).order_by('created_date').last() money_status = numbers_to_template_name(money_last.money) if money_status and true_log(user, tdelta): tarif = Tarif.objects.filter(user=user).last() print(tarif) if tarif: tarif_last = tarif.money_for_mons/30 # print(tarif_last) days_left = num2text(round(money_last.money/(tarif.money_for_mons/30)), male_units) # print(days_left) # else: # tarif_last = '11' # print(tarif_last) # days_left = '11' # print(days_left) user.money_status = money_status user.title = money_status.get('subject') user.pr_user = UserProfile.objects.get(user_id=user.id) user.money_last = money_last.money user.tarif_last = tarif_last # print(user.tarif_last) user.days_left = days_left fifty_users = [] zero_users = [] twenty_users = [] for user in users: if user.money_status.get('template_name') == 'fifty': context = Context({ 'title':user.title, 'pr_user': user.pr_user, 'money_last': user.money_last, 'tarif_last': None, 'days_left':'',# user.days_left, }) # txt = render_to_string('email/on_50.txt', user.context) html = render_to_string('email/on_50.html', context) # send_mail(subject, txt, EMAIL_HOST_USER, ['*****@*****.**'], # fail_silently=False, html_message=html) fifty_users.append(user) elif user.money_status.get('template_name') == 'twenty': twenty_users.append(user) elif user.money_status.get('template_name') == 'zero': zero_users.append(user) sorted_users = dict(fifty=fifty_users, zero=zero_users, twenty=twenty_users) # print(sorted_users) # for user in fifty_users: # print(user.money_status.get('template_name')) return 'yo'