Skip to content

Commit da8b11f

Browse files
committed
users fix
1 parent 871c2a1 commit da8b11f

6 files changed

Lines changed: 27 additions & 12 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 4.2.3 on 2023-10-01 14:48
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("mailing", "0004_alter_mailingschema_schema"),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name="mailingschema",
15+
name="name",
16+
field=models.CharField(max_length=100, unique=True),
17+
),
18+
]

mailing/utils.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
from django.template.loader import render_to_string
88

99
from users.models import CustomUser
10-
from .models import MailingSchema
1110

1211

1312
class MailSender:
1413
@staticmethod
1514
def send(
1615
users: django.db.models.QuerySet | List[CustomUser],
1716
subject: str,
18-
mailing_schema_id: id,
17+
template_path: str,
1918
template_context: Union[
2019
Dict,
2120
List,
@@ -27,16 +26,15 @@ def send(
2726
Throws an error if template render is unsuccessful.
2827
Args:
2928
users: - The list of users who should receive the email.
30-
mailing_schema_id: PK of MailingSchema model.
29+
template_path: str of template_path
3130
subject: Subject of mail.
3231
template_context: Context for template render.
3332
connection: Connection to mail backend
3433
"""
3534
if template_context is None:
3635
template_context = {}
3736

38-
schema = MailingSchema.objects.get(pk=mailing_schema_id)
39-
template_path = pathlib.Path(schema.template.path).absolute()
37+
template_path = pathlib.Path(template_path).absolute()
4038
html_msg = render_to_string(template_path, template_context)
4139
plain_msg = render_to_string(template_path, template_context)
4240
emails = [user.email for user in users]

mailing/views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class SendMailView(APIView):
1212
def post(self, request):
13-
users = request.POST["users"]
13+
users = request.POST["users[]"]
1414
schema_id = request.POST["schemas"]
1515
subject = request.POST["subject"]
1616
mail_schema = MailingSchema.objects.get(pk=schema_id)
@@ -20,7 +20,7 @@ def post(self, request):
2020
if key_in_post in request.POST:
2121
context[key] = request.POST[key_in_post]
2222
users_to_send = CustomUser.objects.filter(pk__in=users)
23-
MailSender.send(users_to_send, subject, schema_id)
23+
MailSender.send(users_to_send, subject, mail_schema.template.path)
2424
return JsonResponse({"detail": "ok"})
2525

2626

partner_programs/admin.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,10 @@ def get_urls(self):
5252
return custom_urls + default_urls
5353

5454
def mailing(self, request, partner_program):
55-
print("kekekekekek")
5655
profiles = PartnerProgramUserProfile.objects.filter(
5756
partner_program=partner_program
5857
)
59-
users = [profile.user for profile in profiles]
58+
users = [profile.user for profile in profiles if profile.user is not None]
6059
return MailingTemplateRender().render_template(request, None, users, None)
6160

6261
def changeform_view(self, request, object_id=None, form_url="", extra_context=None):
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<div>Kek</div>
1+
Some text

templates/mailing/mail_schema.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
Выберите пользователей для отправки:
1919
<div class="checkselect">
2020
{% for user in picked_users %}
21-
<label><input type="checkbox" name="users" value="{{ user.id }}" checked>{{ user.title }}</label>
21+
<label><input type="checkbox" name="users[]" value="{{ user.id }}" checked>{{ user.title }}</label>
2222
{% endfor %}
2323
{% for user in unpicked_users %}
24-
<label><input type="checkbox" name="users" value="{{ user.id }}">{{ user.title }}</label>
24+
<label><input type="checkbox" name="users[]" value="{{ user.id }}">{{ user.title }}</label>
2525
{% endfor %}
2626
</div>
2727
<br>

0 commit comments

Comments
 (0)