Exemplo n.º 1
0
def i_create_one_contact_with_tag_group1(step, tag):
    contact = Contact(
        url = "http://localhost/1/",
        slug = slugify(unicode("http://localhost/1/")),
        state = STATE_PENDING,
        key = "key1",
        tags = ["all", tag]
    )
    contact.save()
Exemplo n.º 2
0
def create_a_default_contact(step):
    contact = Contact()
    contact.url = u"http://default:8000/"
    contact.slug = slugify(contact.url)
    contact.state = STATE_TRUSTED
    contact.description = "desc 1"
    contact.name = "default contact 1"
    contact.save()
Exemplo n.º 3
0
    def post(self):
        '''
        Creates a new contact from web client data 
        (contact object at JSON format). And send a contact request to the
        newly created contact. State of contact is set to PENDING.
        '''

        logger = logging.getLogger("newebe.contact")

        data = self.get_body_as_dict(["url"])

        if data:
            url = data["url"]
            owner = UserManager.getUser()

            if owner.url != url:
                slug = slugify(url)

                self.contact = Contact(
                  url = url,
                  slug = slug
                )
                self.contact.save()

                try:
                    data = UserManager.getUser().asContact().toJson()

                    client = ContactClient()
                    client.post(self.contact, "contacts/request/",
                                data, self.on_contact_response)

                except Exception:
                    import traceback
                    logger.error("Error on adding contact:\n %s" % 
                            traceback.format_exc())

                    self.contact.state = STATE_ERROR
                    self.contact.save()

                return self.return_one_document(self.contact, 201)

            else:
                return self.return_failure(
                        "Wrong data. Url is same as owner URL.", 400)
        else:
            return self.return_failure(
                    "Wrong data. Contact has not been created.", 400)
Exemplo n.º 4
0
    def asContact(self):
        """
        Return current user data as a Contact object.
        """

        contact = Contact()
        contact.url = self.url
        contact.key = self.key
        contact.name = self.name
        contact.description = self.description
        contact.date = self.date

        return contact
Exemplo n.º 5
0
    def post(self):
        '''
        Create a new contact from sent data (contact object at JSON format).
        Sets its status to Wait For Approval
        '''
        data = self.get_body_as_dict(expectedFields=["url"])

        if data:
            url = data["url"]
            owner = UserManager.getUser()

            if owner.url != url:
                slug = slugify(url)

                contact = ContactManager.getContact(slug)
                owner = UserManager.getUser()

                if contact is None:
                    contact = Contact(
                        name = data["name"], 
                        url = url,
                        slug = slug,
                        key = data["key"],
                        state = STATE_WAIT_APPROVAL,
                        requestDate = datetime.datetime.utcnow(),
                        description = data["description"]
                    )
                    contact.save()

                contact.state = STATE_WAIT_APPROVAL
                contact.save()

                self.return_success("Request received.")

            else:
                self.return_failure("Contact and owner have same url.")

        else:
            self.return_failure("Sent data are incorrects.")
Exemplo n.º 6
0
def creates_contacts(step):
    contact = Contact()
    contact.url = "http://localhost/1/"
    contact.slug = slugify(contact.url)
    contact.state = STATE_PENDING
    contact.key = "key1"
    contact.save()
    contact2 = Contact()
    contact2.url = "http://localhost/2/"
    contact2.slug = slugify(contact2.url)
    contact2.state = STATE_TRUSTED
    contact2.key = "key2"
    contact2.save()
    contact3 = Contact()
    contact3.url = "http://localhost/3/"
    contact3.slug = slugify(contact3.url)
    contact3.state = STATE_WAIT_APPROVAL
    contact.key = "key3"
    contact3.save()
Exemplo n.º 7
0
class ContactsHandler(NewebeAuthHandler):
    '''
    This is the resource for contact data management. It allows :
     * GET : retrieves all contacts data.
     * POST : creates a new contact.
    '''


    def get(self):
        '''
        Retrieves whole contact list at JSON format.
        '''
        contacts = ContactManager.getContacts()

        self.return_documents(contacts)


    @asynchronous
    def post(self):
        '''
        Creates a new contact from web client data 
        (contact object at JSON format). And send a contact request to the
        newly created contact. State of contact is set to PENDING.
        '''

        logger = logging.getLogger("newebe.contact")

        data = self.get_body_as_dict(["url"])

        if data:
            url = data["url"]
            owner = UserManager.getUser()

            if owner.url != url:
                slug = slugify(url)

                self.contact = Contact(
                  url = url,
                  slug = slug
                )
                self.contact.save()

                try:
                    data = UserManager.getUser().asContact().toJson()

                    client = ContactClient()
                    client.post(self.contact, "contacts/request/",
                                data, self.on_contact_response)

                except Exception:
                    import traceback
                    logger.error("Error on adding contact:\n %s" % 
                            traceback.format_exc())

                    self.contact.state = STATE_ERROR
                    self.contact.save()

                return self.return_one_document(self.contact, 201)

            else:
                return self.return_failure(
                        "Wrong data. Url is same as owner URL.", 400)
        else:
            return self.return_failure(
                    "Wrong data. Contact has not been created.", 400)


    def on_contact_response(self, response, **kwargs):
        '''
        On contact response, checks if no error occured. If error occured,
        it changes the contact status from Pending to Error.
        '''

        try:
            newebeResponse = json_decode(response.body)
            print newebeResponse
            if not "success" in newebeResponse or not newebeResponse["success"]:
                self.contact.state = STATE_ERROR
                self.contact.save()

        except:
            import traceback
            logger.error("Error on adding contact, stacktrace :\n %s" % 
                    traceback.format_exc())

            self.contact.state = STATE_ERROR
            self.contact.save()