示例#1
0
 def dehydrate(self, bundle):
     bundle.data["users"] = bundle.obj.users
     for att in bundle.obj.__dict__:
         if att not in bundle.data:
             bundle.data[att] = getattr(bundle.obj, att)
     for att in self.HIDDEN_ATTRIBUTES:
         bundle.data.pop(att)
     bundle.data["type"] = draw_factory.get_draw_name(bundle.data["type"])
     return bundle
示例#2
0
def display_draw(request, draw_id):
    """Returns the data to display a draw
    Given a draw id, retrieves it and returns the data required to display it
    """
    bom_draw = MONGO.retrieve_draw(draw_id)
    draw_type = draw_factory.get_draw_name(bom_draw.draw_type)
    if bom_draw.check_read_access(request.user):
        prev_draw_data = bom_draw.__dict__.copy()
        draw_form = draw_factory.create_form(draw_type, prev_draw_data)
        return render(request, "draws/display_draw.html", {"draw": draw_form, "bom": bom_draw})
    else:
        return render(request, "draws/secure_draw.html", {"bom": bom_draw})
示例#3
0
def update_draw(request, draw_id):
    """Serves the update of a draw
    @draw_id: pk of the draw to update
    Given the draw details through the POST data, updates the draw.
    If success, redirects to display the view, otherwise, returns
        the form with the errors. It always create a new version
        of the draw. Use ws to update parts of the draw without
        creating a new version
    """
    prev_bom_draw = MONGO.retrieve_draw(draw_id)
    draw_type = draw_factory.get_draw_name(prev_bom_draw.draw_type)
    user_can_write_draw(request.user, prev_bom_draw)

    LOG.debug("Received post data: {0}".format(request.POST))
    draw_form = draw_factory.create_form(draw_type, request.POST)
    if not draw_form.is_valid():
        LOG.info("Form not valid: {0}".format(draw_form.errors))
        return render(request, "draws/display_draw.html", {"draw": draw_form, "bom": prev_bom_draw})
    else:
        bom_draw = prev_bom_draw
        raw_draw = draw_form.cleaned_data
        LOG.debug("Form cleaned data: {0}".format(raw_draw))
        # update the draw with the data coming from the POST
        for key, value in raw_draw.items():
            if key not in ("_id", "pk") and value != "":
                setattr(bom_draw, key, value)
        if not bom_draw.is_feasible():
            LOG.info("Draw {0} is not feasible".format(bom_draw))
            draw_form.add_error(None, _("Draw not feasible"))
            draw_form = draw_factory.create_form(draw_type, bom_draw.__dict__.copy())
            return render(request, "draws/display_draw.html", {"draw": draw_form, "bom": bom_draw})
        else:
            bom_draw.add_audit("DRAW_PARAMETERS")
            # generate a result if a private draw
            if not bom_draw.is_shared:
                bom_draw.toss()

            MONGO.save_draw(bom_draw)
            LOG.info("Updated draw: {0}".format(bom_draw))
            return redirect('retrieve_draw', draw_id=bom_draw.pk)
示例#4
0
    def __init__(self,
                 creation_time=None,
                 owner=None,
                 number_of_results=1,
                 results=None,
                 _id=None,
                 draw_type=None,
                 users=None,
                 title=None,
                 is_shared=False,
                 enable_chat=True,
                 last_updated_time=None,
                 audit=None,
                 description=None,
                 **kwargs):
        if kwargs:
            logger.info("Unexpected extra args: {0}".format(kwargs))

        self.number_of_results = number_of_results
        """Number of results to generate"""

        self.results = results if results else []
        """List of results (list of list of items)"""

        self._id = _id
        """Unique identifier of the draw"""

        self.owner = owner
        """ID of the owner of the draw"""

        from server import draw_factory

        self.draw_type = draw_factory.get_draw_name(type(self).__name__)
        """Type of the draw"""

        self.creation_time = creation_time if creation_time is not None else datetime.datetime.utcnow(
        ).replace(tzinfo=pytz.utc)
        """Time the draw was created"""

        self.last_updated_time = last_updated_time if last_updated_time else self.creation_time
        """last time this draw was updated"""

        self.users = users if users else []
        """List of users with access to the draw"""

        self.title = title
        """Title of the concrete draw"""

        self.description = description
        """Description of the draw"""

        self.audit = audit if audit else []
        """List of changes in the draw main config, user add_audit to add items"""

        self.enable_chat = enable_chat
        """Whether or not to display the chat"""

        self.is_shared = is_shared is True
        """Whether other users can see the draw"""

        # TODO: backward compat
        if "shared_type" in kwargs and kwargs["shared_type"]:
            self.is_shared = True

        if draw_type and draw_type != self.draw_type:
            logger.warning("A draw was built with type {0} but type {1} was "
                           "passed as argument! Fix it!".format(
                               draw_type, self.draw_type))
        if self.last_updated_time.tzinfo is None:
            self.last_updated_time = self.last_updated_time.replace(
                tzinfo=pytz.utc)
        if self.creation_time.tzinfo is None:
            self.creation_time = self.creation_time.replace(tzinfo=pytz.utc)
        for result in self.results:
            if "datetime" in result:
                result["datetime"] = result["datetime"].replace(
                    tzinfo=pytz.utc)
            if "publication_datetime" in result:
                result["publication_datetime"] = result[
                    "publication_datetime"].replace(tzinfo=pytz.utc)
示例#5
0
    def __init__(self, creation_time=None, owner=None, number_of_results=1,
                 results=None, _id=None, draw_type=None,
                 users=None, title=None, is_shared=False,
                 enable_chat=True, last_updated_time=None,
                 audit=None, description=None, **kwargs):
        if kwargs:
            logger.info("Unexpected extra args: {0}".format(kwargs))

        self.number_of_results = number_of_results
        """Number of results to generate"""

        self.results = results if results else []
        """List of results (list of list of items)"""

        self._id = _id
        """Unique identifier of the draw"""

        self.owner = owner
        """ID of the owner of the draw"""

        from server import draw_factory

        self.draw_type = draw_factory.get_draw_name(type(self).__name__)
        """Type of the draw"""

        self.creation_time = creation_time if creation_time is not None else datetime.datetime.utcnow().replace(
            tzinfo=pytz.utc)
        """Time the draw was created"""

        self.last_updated_time = last_updated_time if last_updated_time else self.creation_time
        """last time this draw was updated"""

        self.users = users if users else []
        """List of users with access to the draw"""

        self.title = title
        """Title of the concrete draw"""

        self.description = description
        """Description of the draw"""

        self.audit = audit if audit else []
        """List of changes in the draw main config, user add_audit to add items"""

        self.enable_chat = enable_chat
        """Whether or not to display the chat"""

        self.is_shared = is_shared is True
        """Whether other users can see the draw"""

        # TODO: backward compat
        if "shared_type" in kwargs and kwargs["shared_type"]:
            self.is_shared = True

        if draw_type and draw_type != self.draw_type:
            logger.warning("A draw was built with type {0} but type {1} was "
                           "passed as argument! Fix it!"
                           .format(draw_type, self.draw_type))
        if self.last_updated_time.tzinfo is None:
            self.last_updated_time = self.last_updated_time.replace(tzinfo=pytz.utc)
        if self.creation_time.tzinfo is None:
            self.creation_time = self.creation_time.replace(tzinfo=pytz.utc)
        for result in self.results:
            if "datetime" in result:
                result["datetime"] = result["datetime"].replace(tzinfo=pytz.utc)
            if "publication_datetime" in result:
                result["publication_datetime"] = result["publication_datetime"].replace(tzinfo=pytz.utc)