<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django.core.management.base import BaseCommand, CommandError
from django.core.mail import send_mass_mail
from django.contrib.auth.models import User
from app.models import Alert
from datetime import datetime, date, timedelta
import requests
import json

def create_email(username, to_email):
	subject = u'LinsAlert - оповещение о смене линз.'
	msg = u'Здравствуйте, ' + username + u'.\n\n Сервис LinsAlert напоминает вам о необходимости смены линз.'
	from_email = u'<EMAIL>'

	return (subject, msg, from_email, [to_email])

def create_sms(to_phone):
	return {
		'to': to_phone,
		'from': u'LinsAlert',
		'text': u'Сервис LinsAlert напоминает вам о необходимости смены линз.'
	}

class Command(BaseCommand):
	args = ''
	help = 'Send alerts for users'

	def handle(self, *args, **options):
		cur_time = datetime.now().strftime('%H:%M')
		cur_date = date.today()
		pre_alerts = Alert.objects.all()#filter(alert_server_time=cur_time, start__lte=cur_date, finish__gte=cur_date)
		alerts = []

		for alert in pre_alerts:
			real_period = {'1': 1, '2': 7, '3': 14, '4': 28}
			t_date = alert.start
			
			while t_date <= cur_date:
				if t_date == cur_date:
					alerts.append(alert)
					break
				else:
					t_date += timedelta(days=real_period[alert.period])
		
		if len(alerts) != 0:
			email_msg = []
			sms_msg = []
			
			for alert in alerts:
				if alert.alert_email:				
					email_msg.append(create_email(alert.user.first_name, alert.email))
				if alert.alert_phone:
					sms_msg.append(create_sms(alert.phone))

			try:
				send_mass_mail(email_msg, fail_silently=False)
			except Exception as error:
				print 'Email alert error: %s' % CommandError(error)
			try:
				sms_id = '7920'
				sms_key = '<KEY>'
				headers = {'content-type': 'application/json'}<commit_msg>Fix server send alerts script<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django.core.management.base import BaseCommand, CommandError
from django.core.mail import send_mass_mail
from django.contrib.auth.models import User
from app.models import Alert
from datetime import datetime, date, timedelta
import requests
import json

def create_email(username, to_email):
	subject = u'LinsAlert - оповещение о смене линз.'
	msg = u'Здравствуйте, ' + username + u'.\n\nСервис LinsAlert напоминает вам о необходимости смены линз.'
	from_email = u'<EMAIL>'

	return (subject, msg, from_email, [to_email])

def create_sms(to_phone):
	return {
		'to': to_phone,
		'from': u'LinsAlert',
		'text': u'Сервис LinsAlert напоминает вам о необходимости смены линз.'
	}

class Command(BaseCommand):
	args = ''
	help = 'Send alerts for users'

	def handle(self, *args, **options):
		cur_time = datetime.now().strftime('%H:%M')
		cur_date = date.today()
		pre_alerts = Alert.objects.filter(alert_server_time=cur_time, start__lte=cur_date, finish__gte=cur_date)
		alerts = []

		for alert in pre_alerts:
			real_period = {'1': 1, '2': 7, '3': 14, '4': 28}
			t_date = alert.start
			
			while t_date <= cur_date:
				if t_date == cur_date:
					alerts.append(alert)
					break
				else:
					t_date += timedelta(days=real_period[alert.period])
		
		if len(alerts) != 0:
			email_msg = []
			sms_msg = []
			
			for alert in alerts:
				if alert.alert_email:				
					email_msg.append(create_email(alert.user.first_name, alert.email))
				if alert.alert_sms:
					sms_msg.append(create_sms(alert.phone))

			try:
				send_mass_mail(email_msg, fail_silently=False)
			except Exception as error:
				print 'Email alert error: %s' % CommandError(error)
			try:
				sms_id = '7920'
				sms_key = '<KEY>'
				headers = {'content-type': 'application/json'}
{'fraction_non_alphanumeric': 0.12605042016806722, 'fraction_numerical': 0.00867443751694226, 'mean_word_length': 3.9331550802139037, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5379238', 'n_tokens_mistral': 1559, 'n_tokens_neox': 1352, 'n_words': 320}