def post(self, group_name=None):
        session = authorize(request.headers["Authorization"])
        print session

        contacts = post_parser.parse_args().get("contacts")
        if contacts is None:
            abort(403)

        if group_name is None: group_name = "default"

        new_contacts = 0
        existing_contact = 0
        for contact in contacts:
            print contact
            contact = Contact.contact_from_dict(contact)
            existing = mongo.db.contacts.find_one({"email": contact.email})

            if existing is None:
                contact._id = get_id("contacts")
                contact.creationDate = datetime.now()
                contact.createdBy = session.get("user").get("login")
                if contact.language is None:
                    contact.language = "fr"

                mongo.db.contacts.insert(contact.format())

                self.update_group(group_name, contact.createdBy, contact)

                new_contacts += 1
            else:
                existing_contact += 1
                contact = Contact.contact_from_dict(existing)
                self.update_group(group_name, contact.createdBy, contact)

        return {"inserted": new_contacts, "existing": existing_contact}, 200
    def post(self, quiz_id, contact_group=None):
        session = authorize(request.headers["Authorization"])

        if contact_group is None: contact_group = "default"

        contacts = self.get_contacts(contact_group, session.get("user").get("login"))

        if contacts is None:
            return {"message": "no contact to send"}, 400

        criteria = {"createdBy": session.get('user').get('login'), "_id": int(quiz_id)}
        quiz = Quiz.quiz_from_dict(mongo.db.quiz.find_one_or_404(criteria))

        for contact in contacts:
            c = Contact.contact_from_dict(contact)
            p = Publication()
            p._id = get_id("publication")
            p.creationDate = datetime.now()
            p.hash = hashlib.sha256("%d.%s" % (p._id, str(p.creationDate))).hexdigest()
            p.by = session.get('user').get('login')
            p.quiz = quiz
            p.to = c
            mongo.db.publications.insert(p.format())
            self.send_email(quiz.title, p.hash, c.email, c.language)

        return {"message": "quiz %s published to %d contacts" % (quiz.title, len(contacts))}, 200
Ejemplo n.º 3
0
    def publication_from_dict(pubDict):
        p = Publication()
        p.by = pubDict.get('by')
        p.to = Contact.contact_from_dict(pubDict.get('to'))
        p.creationDate = pubDict.get('creationDate')
        p.hash = pubDict.get('hash')
        p._id = pubDict.get('_id')

        return p
Ejemplo n.º 4
0
    def participation_from_dict(pDict):
        p = Participation()
        p.quiz_id = pDict.get('quiz_id')
        p.pub_hash = pDict.get('pub_hash')
        p.pub_date = pDict.get('pub_date')
        p.creationDate = pDict.get('creationDate')
        p.contact = Contact.contact_from_dict(pDict.get('contact'))
        p._id = pDict.get('_id')

        if not (pDict.get('answers') is None):
            for a in pDict.get('answers'):
                p.answers.append(Answer.answer_from_dict(a))

        return p
    def participation_from_dict(pDict):
        p = Participation()
        p.quiz_id = pDict.get('quiz_id')
        p.pub_hash = pDict.get('pub_hash')
        p.pub_date = pDict.get('pub_date')
        p.creationDate = pDict.get('creationDate')
        p.contact = Contact.contact_from_dict(pDict.get('contact'))
        p._id = pDict.get('_id')

        if not (pDict.get('answers') is None):
            for a in pDict.get('answers'):
                p.answers.append(Answer.answer_from_dict(a))

        return p
Ejemplo n.º 6
0
    def post(self, group_name=None):
        session = authorize(request.headers["Authorization"])
        print session

        contacts = post_parser.parse_args().get("contacts")
        if contacts is None:
            abort(403)

        if group_name is None: group_name = "default"

        new_contacts = 0
        existing_contact = 0
        for contact in contacts:
            print contact
            contact = Contact.contact_from_dict(contact)
            existing = mongo.db.contacts.find_one({"email": contact.email})

            if existing is None:
                contact._id = get_id("contacts")
                contact.creationDate = datetime.now()
                contact.createdBy = session.get("user").get("login")
                if contact.language is None:
                    contact.language = "fr"

                mongo.db.contacts.insert(contact.format())

                self.update_group(group_name, contact.createdBy, contact)

                new_contacts += 1
            else:
                existing_contact += 1
                contact = Contact.contact_from_dict(existing)
                self.update_group(group_name, contact.createdBy, contact)



        return {"inserted": new_contacts, "existing": existing_contact}, 200
Ejemplo n.º 7
0
    def get_all_contacts(self):
        contacts = []
        for c in mongo.db.contacts.find():
            contacts.append(Contact.contact_from_dict(c).format())

        return contacts, 200
    def get_all_contacts(self):
        contacts = []
        for c in mongo.db.contacts.find():
            contacts.append(Contact.contact_from_dict(c).format())

        return contacts, 200