def email(): body = request.json if 'email' not in body or 'link_id' not in body or 'username' not in body: return Response(json.dumps({ 'error': 'need both email and which link this email was given to' }), mimetype='application/json', status=400) email = body['email'] new_submit = Email(email) link_id = body['link_id'] username = body.get('username') link = Link.query.filter_by(id=int(link_id)).first() new_submit.parent_link_id = link.id new_submit.name = body.get('name', '') new_submit.username = username db.session.add(new_submit) db.session.commit() submitted_email = Email.query.filter_by(parent_link_id=link.id, email=email).first() aflink = create_link(username, link_id) return Response(json.dumps({ 'email': submitted_email.to_dict(), 'aflink': aflink.to_dict() }), mimetype='application/json', status=200)
def create_person(): data = request.get_json() logger.info("Data recieved: %s", data) if "name" not in data: msg = "No name provided for person." logger.info(msg) return create_response(status=422, message=msg) if "email" not in data: msg = "No email provided for person." logger.info(msg) return create_response(status=422, message=msg) # create SQLAlchemy Objects new_person = Person(name=data["name"]) email = Email(email=data["email"]) new_person.emails.append(email) # commit it to database db.session.add_all([new_person, email]) db.session.commit() return create_response( message= f"Successfully created person {new_person.name} with id: {new_person.id}" )
def writer(header, data, filename, option): with open(filename, "w", newline="") as csvfile: if option == "write": movies = csv.writer(csvfile) movies.writerow(header) for x in data: movies.writerow(x) elif option == "update": writer = csv.DictWriter(csvfile, fieldnames=header) writer.writeheader() writer.writerows(data) else: print("Option is not known") # create SQLAlchemy Objects new_person = Person(name=data["name"]) email = Email(email=data["email"]) new_person.emails.append(email) # commit it to database db.session.add_all([new_person, email]) db.session.commit() return create_response( message= f"Successfully created person {new_person.name} with id: {new_person._id}" )
def assertMailEqual(self, email: EmailMessage, expected_email: Email): self.assertEqual(email.subject, expected_email.subject) self.assertEqual(email.body, expected_email.body) self.assertEqual(email.from_email, expected_email.sender.email) self.assertEqual(email.extra_headers, expected_email.get_headers()) self.assertEqual( email.to, list(expected_email.recipients.values_list('email', flat=True)))
def test_should_filter_by_pending_emails(self): EmailFactory(status=Email.Status.SENT) EmailFactory(status=Email.Status.FAILED_SENDING) expected_pending_emails = EmailFactory.create_batch(5) pending_emails = Email.get_pending_emails() self.assertListEqual( list(pending_emails.values_list('pk', flat=True)), list(map(lambda n: n.pk, expected_pending_emails)))
def create(self, validated_data: dict): connection = self.get_smtp_connection(validated_data) pending_emails = Email.get_pending_emails() sent_email_ids, failed_email_ids = self.send_emails( connection, pending_emails) sent_emails = Email.objects.filter(pk__in=sent_email_ids) failed_emails = Email.objects.filter(pk__in=failed_email_ids) sent_emails.update(status=Email.Status.SENT) failed_emails.update(status=Email.Status.FAILED_SENDING) connection.close() return EmailSerializer(sent_emails.union(failed_emails), many=True).data
def post(self, request): serializer = self.serializer_class(data=request.data) if serializer.is_valid() is False: logger.error('Email api bad request') return Response({"status": False, "message": serializer.errors}, status=status.HTTP_400_BAD_REQUEST) data = serializer.data logger.info('Bulk Email API in progress') new_email = Email() try: helpers.send_email(data) new_email.status = 'success' logger.debug('Email sent, id : ' + str(new_email.id)) new_email.save() return Response({"status": True, "message": "Email Sent Successfully !"}, status=200) except: new_email.status = 'fail' new_email.save() logger.error('Email failed, id : ' + str(new_email.id)) return Response({"status": True, "message": "Email Not Sent!"}, status=500)
def create_person(): data = request.get_json() logger.info("Data recieved: %s", data) if "name" not in data: msg = "No name provided for person." logger.info(msg) return create_response(status=422, message=msg) if "emails" not in data: msg = "No email provided for person." logger.info(msg) return create_response(status=422, message=msg) # create MongoEngine objects new_person = Person(name=data["name"]) for email in data["emails"]: email_obj = Email(email=email) new_person.emails.append(email_obj) new_person.save() return create_response( message=f"Successfully created person {new_person.name} with id: {new_person.id}" )