Send filtered data using django via email

i want filter the student data by the skills in the vacancy and then send it to the company who posted the requirements in vacancy via email.does anyone have idea about this.

** The Model**

class Student(models.Model):

    name = models.CharField(max_length=50)
    skills = models.ManyToManyField(Skill)
    passout = models.ManyToManyField(Passout)
    email = models.EmailField(max_length=254 ,null = True)
    resume = models.ImageField(upload_to='media/resume', height_field=None, width_field=None, max_length=None , null=True)
    create_date = models.DateTimeField('created date',auto_now_add=True)

    def __str__(self):
        return self.name
    
class Company(models.Model):

    comp_name = models.CharField(max_length=50)
    loc = models.CharField(max_length=50)
    hr_name = models.CharField(max_length=50)
    hr_email = models.EmailField(max_length=254)
    
    def __str__(self):
        return self.comp_name

class Vacanccy(models.Model):

    com_name = models.ForeignKey(Company, on_delete=models.CASCADE)
    skillreq = models.ManyToManyField(Skill)
    yearofpass = models.ManyToManyField(Passout)
    title = models.CharField(max_length=255)
    expiry_date = models.CharField(max_length=50)

    def __str__(self):
        return str(self.com_name) 

enter image description here

the above image 1st tabel is student , 2nd is company, 3rd is vacancy

In Vacancy table two companies posted their requirements. The 1st company have matching profile of rini , also 2nd remya

If we filter the data and it should send to the corresponding company like rini details to dell and remya details to redmi

Could be a better way! but tried. and this block of code will mail the path of the resume, not the Actual File.

from django.core.mail import send_mass_mail
from django.http.response import JsonResponse


#its support function to convert queryset into dict
def get_dict_from_quryset(key, val, records):
    dict = {}
    for record in records:
        dict[record[key]] = record[val]

    return dict


def your_function_name(request):
    mail_data = []
    vacancies = Vacanccy.objects.all().values("com_name_id", "com_name__comp_name", "skillreq", "skillreq__name", "yearofpass__year")
    skills_ids = list(vacancies.values_list("skillreq", flat=True))
    comp_ids = list(set(vacancies.values_list("com_name_id", flat=True)))
    pass_years = list(vacancies.values_list("yearofpass", flat=True))
    comp_details = {}
    companies = Company.objects.filter(id__in=comp_ids).values("id","hr_email")
    dict_companies = get_dict_from_quryset("id", "hr_email", companies)
    vacancy_dict = {}
    for data in vacancies:
        vacancy_dict[data['skillreq']] = data['com_name_id']


    applicable_student_data = Student.objects.filter(skills__in=skills_ids, passout__in=pass_years).values("id","name", "resume", 'skills')

    repeat_student_id = []
    for student in applicable_student_data:
        if student['skills'] in vacancy_dict and student['id'] not in repeat_student_id:
            company_id = vacancy_dict[student['skills']]
            if company_id in dict_companies:
                email_id_hr = dict_companies[company_id]
                mail_content = ('Subject', student['resume'], 'yourrrr.from@gmail.com', [email_id_hr])
                mail_data.append(
                        mail_content
                )
                repeat_student_id.append(student['id'])
                continue

    tupple_mail_data = tuple(mail_data)

    if tupple_mail_data:
        send_mass_mail(tupple_mail_data, fail_silently=False)
    return JsonResponse({"status": "Succeed"})
Back to Top