示例#1
0
class Chat_History(EmbeddedMongoModel):
    datetimestamp = fields.DateTimeField()
    c_id = fields.CharField()
    chats = fields.EmbeddedDocumentListField(Chat)
示例#2
0
class Renamed(User):
    # Override existing field and change mongo_name
    address = fields.CharField(mongo_name='mongo_address')
示例#3
0
class CCAvenueConfig(BaseMongoModel):
    id = fields.ObjectIdField(primary_key=True)
    merchant_id = fields.CharField(required=True)
    SECRET_working_key = fields.CharField(required=True)
    access_code = fields.CharField(required=True)
class MultipleInheritanceModel(User, AnotherUser):
    phone = fields.CharField()  # Shadow phone field from ParentModel.
示例#5
0
class Orders(MongoModel):
    notification = fields.BooleanField(default = False)
    invoiceURL = fields.CharField(default = "null")
    userId = fields.CharField(default = "rootuser")
示例#6
0
class Comment(MongoModel):
    body = fields.CharField()
    post = fields.ReferenceField(Post)
示例#7
0
class Workflow(MongoModel):
    name = fields.CharField(required=True)
    display_name = fields.CharField(required=True)
    steps = fields.ListField(fields.ReferenceField(Step))
    valid_plate_types = fields.ListField(fields.CharField(choices=["96plate"]))
    steps_denormalized = fields.ListField(fields.DictField())

    def denormalize_steps(self):
        steps_denormalized = []
        for step in self.steps:
            steps_denormalized.append({
                "_id": step._id,
                "name": step.name,
                "category": step.category
            })
        self.steps_denormalized = steps_denormalized

    class Meta:
        indexes = [IndexModel([("name", 1)], unique=True)]

    @classmethod
    def get_workflows(cls):
        """
        Returns list of available workflows
        """
        workflows = cls.objects.raw({}).project({
            "name": 1,
            "display_name": 1,
            "valid_plate_types": 1,
            "_id": 0
        })
        return [x.to_son().to_dict() for x in workflows]

    @staticmethod
    def find_steps(step_names):
        """
        Provided a list of step names, it will search and
        add them to self.steps as references.
        Steps must exist already.
        """
        step_o_list = []
        for step in step_names:
            step_o_list.append(Step.objects.get({"name": step}))
        return step_o_list

    def valid_next_step(self, last_step, next_step):

        i = None
        if last_step == "root":
            i = -1
        else:
            for i in range(len(self.steps_denormalized)):
                if self.steps_denormalized[i]["category"] == last_step:
                    break
        if i is None or i == len(
                self.steps_denormalized) - 1:  # none or last step
            return False
        next_s = self.steps_denormalized[i + 1]
        if next_step == next_s["category"]:
            return True
        else:
            return False

    def get_prev_step(self, step_name):
        """
        Returns previous step to the one provided.
        """
        index = None
        for i in range(len(self.steps_denormalized)):
            step = self.steps_denormalized[i]
            if step["name"] == step_name:
                index = i
        if index is None:
            raise ValueError("Step not in workflow")
        if index == 0:
            return "root"
        else:
            return self.steps_denormalized[index - 1]["name"]

    def next_step(self, step_name):
        """
        Returns next step to the one provided. None if last step 
        """
        index = None
        for i in range(len(self.steps_denormalized)):
            step = self.steps_denormalized[i]
            if step["name"] == step_name:
                index = i
        if index is None:
            raise ValueError("Step not in workflow")
        if index == len(self.steps_denormalized) - 1:
            return "_workflow_finished"
        else:
            return self.steps_denormalized[index + 1]["name"]
class WebPage(MongoModel):
    id = fields.CharField(primary_key=True)
    url = fields.CharField()
    title = fields.CharField()
    web_link = fields.ListField(fields.CharField())
示例#9
0
class Step_instance(MongoModel):
    step = fields.ReferenceField(Step, required=True)
    start_date = fields.DateTimeField(required=True,
                                      default=datetime.datetime.now())
    finish_date = fields.DateTimeField()
    instrument = fields.CharField(required=True)
    status = fields.CharField(required=True,
                              default="started",
                              choices=("started", "finished", "cancelled"))
    user_started = fields.ReferenceField(User, required=True)
    user_ended = fields.ReferenceField(User)
    comment = fields.CharField()
    # result_input_ = fields.DictField()
    # outputFiles = fields.DictField()
    samples = fields.ListField(fields.ReferenceField(Sample), required=True)
    result_samples = fields.DictField(required=True)
    result_all = fields.DictField(blank=True)
    qc_actions = fields.DictField(blank=True)
    batch = fields.CharField()
    workflow = fields.ReferenceField(Workflow, required=True)

    @staticmethod
    def _clean_param(p):
        return p.replace(".", "_").replace("-", "_")

    @staticmethod
    def invalid_comb(t, s):
        raise ValueError(
            "Invalid param combination: type {}. scope: {}".format(t, s))

    @staticmethod
    def invalid_param(s):
        raise ValueError("Invalid param: {}".format(s))

    def _get_result(self, fieldname, barcode=None):
        try:
            if barcode is None:
                return self.result_all[fieldname]
            else:
                return self.result_samples[barcode][fieldname]
        except KeyError:
            return None
            # raise minilims.errors.MissingValueError(
            #     "Value {} for barcode {} not found in results.".format(fieldname, barcode))

    def set_batch(self, batch_list):
        self.batch = batch_list
        self.save()

    def _get_param(self, param_name: str, scope: str):
        """Parses the param name and gets the value back
        

        Args:
            param_name (str): Right now the format is step.field, but can be expanded to workflow.step.field
            scope (str): "all" or "sample"

        Raises:
            ValueError: [description]

        Returns:
            [type]: [description]
        """

        fields = param_name.split(".")
        sample_dict = {}
        if len(fields) == 1:
            # Same step
            workflow_name = None
            step_name = None
            field_name = param_name

            if scope == "all":
                for sample in self.samples:
                    sample_dict[sample.barcode] = self._get_result(param_name)
            else:  # "sample"
                for sample in self.samples:
                    sample_dict[sample.barcode] = self._get_result(
                        field_name, sample.barcode)

        elif len(fields) == 2:
            # other step, any workflow
            workflow_name = None
            step_name = fields[0]
            field_name = fields[1]

            for sample in self.samples:
                potential_workflows = sample.find_workflow_for_step(step_name)
                if len(potential_workflows) > 1:
                    current_app.logging.warning(
                        ("Sample {} requests data for step {} but more than "
                         "one workflow is available ({}), using the first one"
                         ).format(sample.barcode, param_name,
                                  potential_workflows))
                workflow_name = potential_workflows[0]
                sample_dict[sample.barcode] = sample.find_result(
                    workflow_name, step_name, scope, field_name, self)

        elif len(fields) == 3:
            # other step, other workflow
            workflow_name = fields[0]
            step_name = fields[1]
            field_name = fields[2]

            for sample in self.samples:
                sample_dict[sample.barcode] = sample.find_result(
                    workflow_name, step_name, scope, field_name, self)

        else:
            raise ValueError(
                "Invalid param name. Max 2 '.' allowed. Used to separate workflow.step_name.fieldname"
            )

        return sample_dict

    def _get_params(self, script):
        """
        Get parameters according to the script instructions
        """
        params = {}
        ivs = script.input_values
        for iv in ivs:
            if iv.type_ == "sample_properties":
                if iv.scope == "sample":
                    if iv.name == "batch":
                        params[self._clean_param(
                            iv.name)] = self._get_sample_batch_info()
                    else:
                        self.invalid_comb(iv.type_, iv.scope)
                else:
                    if iv.name == "samples":
                        params[self._clean_param(iv.name)] = self.samples
                    else:
                        self.invalid_comb(iv.type_, iv.scope)
            elif iv.type_ == "user_started":
                if iv.scope == "all":
                    params[self._clean_param(iv.name)] = self.user_started
                else:
                    self.invalid_comb(iv.type_, iv.scope)
            elif iv.type_ == "user_ended":
                if iv.scope == "all":
                    params[self._clean_param(iv.name)] = self.user_ended
                else:
                    self.invalid_comb(iv.type_, iv.scope)
            elif iv.type_ == "file":
                if iv.scope == "all":
                    if iv.name.count(".") != 0:
                        self.invalid_param(
                            "Invalid file field name '{}'. Character '.' is only allowed to specify 'step.field', but 'file' values with 'all' scope can only be from the same step."
                            .format(iv.name))
                    else:
                        fileid = self._get_result(iv.name)
                        # params[iv.name] = fileutils.get_file(fileid)
                        params[self._clean_param(iv.name)] = fileid
                else:
                    self.invalid_comb(iv.type_, iv.scope)
            else:
                params[self._clean_param(iv.name)] = self._get_param(
                    iv.name, iv.scope)
            # else:
            #     self.invalid_comb(iv.type_, iv.scope)
        # Much more work to do here.
        return params

    def save_files(self, files):
        for fieldname, fileobject in files.items():
            _id = fileutils.save_file(fieldname, fileobject)
            fieldname = fieldname.replace(".", "_")
            self.result_all[fieldname] = _id

    def delete_files(self, files):
        """
        Reverse save_files
        """
        for fieldname, fileobject in files.items():
            fieldname = fieldname.replace(".", "_")
            _id = self.result_all[fieldname]
            fileutils.delete_file(_id)
            self.result_all.pop(fieldname)

    def save_params(self, params):
        """Saves params in step_results.

        params format::

            {
                "files": {
                    "fieldname": fileobject,
                    "fieldname2": fileobject2,
                },
                "params": {
                    "all": {
                        "fieldname": value1,
                        "fieldname2": value2,
                    },
                    "samples" : {
                        "barcode1" : {
                            "fieldname1": value1,
                            ...
                        },
                        ...
                    }
                }
            }

        Args:
            params (Dict): See format in description
        """
        if "files" in params:
            self.save_files(params["files"])
        if "params" in params:
            if "all" in params["params"]:
                for fieldname, fieldvalue in params["params"]["all"].items():
                    self.result_all[fieldname] = fieldvalue
            if "samples" in params["params"]:
                for barcode, sampleparams in params["params"]["samples"].items(
                ):
                    for fieldname, fieldvalue in params["params"]["samples"][
                            barcode]:
                        self.result_samples[barcode][fieldname] = fieldvalue
        self.save()

    def remove_params(self, params):
        """Reverse save_params

        Args:
            params (Dict): Same as params from save_params.
        """
        if "files" in params:
            self.save_files(params["files"])
        if "params" in params:
            if "all" in params["params"]:
                for fieldname in params["params"]["all"].keys():
                    self.result_all.pop(fieldname, None)
            if "samples" in params["params"]:
                for barcode in params["params"]["samples"].keys():
                    for fieldname, fieldvalue in params["params"]["samples"][
                            barcode]:
                        self.result_samples[barcode].pop(fieldname, None)
        self.save()

    def run_scripts(self, stage: str) -> None:
        """Run all scripts for this step in specified stage.

        Args:
            stage (str): Which step stage to run scripts for. "stepstart" or "stepend"
        """
        for script in self.step.input_output:
            if stage in ("stepstart", "stepend"):
                if script.stage == stage and script.script:
                    params = self._get_params(script)
                    results = self.step.run_script(script, params)
                    print(results)
                    # Right now the "output_values" property from
                    # input_output object is not used
                    # Save "all" results
                    for key, value in results.get("all", {}).items():
                        self.result_all[key] = value
                    # Save "samples" results
                    if "samples" in results:
                        for barcode, sample_result in results["samples"].items(
                        ):
                            for key, value in sample_result.items():
                                self.result_samples[barcode][key] = value
                    if "qc_actions" in results:
                        self.qc_actions = results["qc_actions"]
            elif stage == "qc" and script.stage == stage:
                self.step.run_qc_script(script, self)
        self.save()

    def qc_actions_msg(self):
        if len(self.qc_actions) == 0:
            return []
        to_steps = {}
        for bc, step in self.qc_actions.items():
            if step in to_steps:
                to_steps[step].append(bc)
            else:
                to_steps[step] = [bc]
        messages = []
        for step, samples in to_steps.items():
            step_o = Step.objects.get({"name": step})
            messages.append("Samples {}: {}".format(
                ", ".join(samples),
                "Unassigned from workflow. Recommended to send to: {}.".format(
                    step_o.display_name)))
        return "QC issues: " + ".".join(messages)

    def _get_sample_batch_info(self):
        batch_info = {}
        for sample in self.samples:
            for batch in sample.batches:
                if batch.batch_name == self.batch:
                    batch_info[sample.barcode] = batch
        return batch_info

    def qc_reassign_data(self):
        data = {}
        #Only takes one suggested step per step instance
        reassign = list(set(self.qc_actions.values()))
        if len(reassign):

            data["suggested_step"] = reassign[0]
            data["samples"] = list(self.qc_actions.keys())
            data["workflow"] = {
                "name": self.workflow.name,
                "display_name": self.workflow.display_name,
                "plate_types": self.workflow.valid_plate_types
            }
            return data
        else:
            return {}

    def summary_values(self):
        summary = {}
        summary["values_samples"] = {}
        summary["fields_samples"] = []
        summary["values_all"] = []
        for s, v_dict in self.result_samples.items():
            s_dict = {}
            for k, v in v_dict.items():
                if isinstance(v, ObjectId):
                    v_type = "file"
                    v = url_for('lims.getfile', fileid=v)
                else:
                    v_type = "value"
                s_dict[k] = v
                if (k, v_type) not in summary["fields_samples"]:
                    summary["fields_samples"].append((k, v_type))
            summary["values_samples"][s] = s_dict

        for k, v in self.result_all.items():
            if isinstance(v, ObjectId):
                v_type = "file"
                v = url_for('lims.getfile', fileid=v)
            else:
                v_type = "value"
            summary["values_all"].append({
                "name": k,
                "value": v,
                "type": v_type
            })
        return summary

    def summary(self, values=False):
        """
        Summary generated for finished_step view and step summary view (not done yet)
        """
        summary = {
            "display_name": self.step.display_name,
            "start_date": self.start_date,
            "finish_date": self.finish_date,
            "num_samples": len(self.samples),
            "id": self._id,
            "batch": self.batch,
            "samples": [s.barcode for s in self.samples]
        }
        if values:
            val = self.summary_values()
            summary["values_samples"] = val["values_samples"]
            summary["values_all"] = val["values_all"]
            summary["fields_samples"] = val["fields_samples"]

        return summary
示例#10
0
class User(MongoModel):
    username = fields.CharField()

    def get_posts(self):
        return list(Post.manager.posts_by_user(self._id))
示例#11
0
class BookCredit(MongoModel):
    first_name = fields.CharField()
    last_name = fields.CharField()
    role = fields.CharField(choices=[('A', 'author'), ('E', 'editor')])
    contributors = CustomManager()
    more_contributors = CustomManager()
示例#12
0
class IterAssitant(MongoModel):

    index_key = fields.CharField(default=None, required=True, blank=False)
    file_name = fields.CharField(default=None, blank=True)
    summary = fields.DictField(default=None, blank=True)
    boundary_lower = fields.IntegerField(default=None, blank=True)
    boundary_upper = fields.IntegerField(default=None, blank=True)
    max_num = fields.IntegerField(default=None, blank=True)
    # iter_index should fall in [boundary_lower, boundary_upper)
    start_index = fields.IntegerField(default=None, blank=True)
    error_indices = fields.ListField(default=[], blank=True)
    # cursor is next cursor because cursor is both input and output
    cursor = fields.CharField(default=None, blank=True)
    error_cursors = fields.ListField(default=[], blank=True)
    # query is last query because query is input
    query = fields.DictField(default=None, blank=True)
    error_queries = fields.ListField(default=[], blank=True)

    class Meta:
        # https://pymodm.readthedocs.io/en/stable/api/index.html#metadata-attributes
        collection_name = ''
        indexes = [
            IndexModel(keys=[('index_key', HASHED)]),
        ]
        ignore_unknown_fields = True
        final=False

    def save_entry(self, save_to='mongo_db'):
        if save_to == 'mongo_db':
            self.save()
        else:
            if not getattr(self, '_id'):
                setattr(
                    self,
                    '_id',
                    ObjectId(),
                )
            if os.path.exists(save_to):
                with open(save_to, 'r') as fr:
                    all_entries = json.load(fr)
            else:
                all_entries = {}
            all_entries[str(self._id)] = self.to_son()
            with open(save_to, 'w') as fw:
                json.dump(all_entries, fw, indent=2, default=json_util.default)

    def refresh_entry(self, save_to='mongo_db'):
        if save_to == 'mongo_db':
            self.refresh_from_db()
        else:
            if not getattr(self, '_id'):
                return
            if not os.path.exists(save_to):
                return

            with open(save_to, 'r') as fr:
                all_entries = json.load(fr)
            if str(self._id) not in all_entries:
                return
            for k, v in all_entries[str(self._id)].items():
                if k == '_id':
                    continue
                setattr(self, k, v)

    @classmethod
    def find_all(cls, source=None, save_to='mongo_db'):
        if save_to == 'mongo_db':
            return cls.objects.raw(source)
        else:
            if os.path.exists(save_to):
                with open(save_to, 'r') as fr:
                    all_entries = json.load(fr)
                entries_recoveried = []
                for k, v in all_entries.items():
                    del v['_id']
                    entries_recoveried.append(cls(**v, _id=ObjectId(k)))
                return entries_recoveried
            else:
                return []

    @classmethod
    def get_iter_index(
        cls,
        source: dict,
        data_len: int=None,
        data: Any=None,
        segment_len: int=None,
        additional_filter: dict=None,
        **kwargs
    ):
        # TODO: separate this function into two: create and query
        # get data_len
        if (data_len is None) and (data is not None):
            data_len = len(data)

        # get iter_index of the task
        iter_index = list(cls.find_all(source=source, save_to=SAVE_TO))
        if len(iter_index) == 0 and (data_len is not None):
            assert (data_len is not None)
            # get segment_len
            if segment_len is None:
                segment_len = data_len
            # get num_segs
            num_segs = math.ceil(data_len/float(segment_len))
            for i in range(num_segs):
                # create one if no iter_index found
                new_index = cls(
                    index_key=source['index_key'],
                    file_name=source.get('file_name'),
                    summary=source.get('summary'),
                    cursor=kwargs.get('start_cursor'),
                    query=kwargs.get('start_query'),
                    start_index=i*segment_len+kwargs.get('start_index', 0),
                    boundary_lower=i*segment_len+kwargs.get('start_index', 0),
                    boundary_upper=min(
                        (i+1)*segment_len, data_len
                    ) + kwargs.get('start_index', 0),
                    max_num=data_len+kwargs.get('start_index', 0),
                )
                # set extra parameters in sub classes
                self_fields = cls.get_db_fields()
                base_fields = IterAssitant.get_db_fields()
                for f in (set(self_fields) - set(base_fields)):
                    if f not in kwargs:
                        continue
                    setattr(new_index, f, kwargs[f])

                new_index.save_entry(save_to=SAVE_TO)
                iter_index.append(new_index)

        if additional_filter is not None:
            iter_index = list(cls.find_all(
                source={**source, **additional_filter},
                save_to=SAVE_TO
            ))

        if len(iter_index) == 0:
            return None

        # random select one if multiple records found
        iter_index = random.choice(iter_index)

        if (data_len is not None) and (iter_index.max_num < data_len):
            iter_index.max_num = data_len
            iter_index.save_entry(save_to=SAVE_TO)

        return iter_index

    @staticmethod
    def generate_download_url(
        base_url: str,
        basename: Union[int, str],
    ):
        # return example: https://bulkdata.uspto.gov/data/patent/grant/redbook/fulltext/1971
        if not base_url.endswith('/'):
            base_url += '/'
        url = urllib.parse.urljoin(base_url, str(basename))

        return url

    @staticmethod
    def get_url_basename(url):
        if url.endswith('/'):
            url = url.rstrip('/')
        return os.path.basename(url)


    @classmethod
    def get_db_fields(cls):
        db_fields = []
        for f in dir(cls):
            if isinstance(getattr(cls, f), fields.MongoBaseField):
                db_fields.append(f)
        return db_fields

    @classmethod
    def is_db_connected(cls):
        is_connected = False

        connected_db_names = set([
            x.parsed_uri['database'] for x in pymodm.connection._CONNECTIONS.values()
        ])

        db_name = None
        if 's_db_name' in dir(cls.Meta):
            db_name = cls.Meta.s_db_name

        if db_name is not None:
           if db_name in connected_db_names:
               is_connected = True
        else:
            if len(connected_db_names) > 0:
                is_connected = True

        return is_connected
示例#13
0
class CTFModel(MongoModel):
    name = fields.CharField(required=True)
    guild_id = fields.IntegerField()
    category_id = fields.IntegerField()
    role_id = fields.IntegerField()
    description = fields.CharField()
    created_at = fields.DateTimeField(required=True)
    finished_at = fields.DateTimeField()
    start_date = fields.DateTimeField()
    end_date = fields.DateTimeField()
    url = fields.URLField()
    username = fields.CharField()
    password = fields.CharField()
    challenges = fields.EmbeddedDocumentListField(Challenge,
                                                  default=[],
                                                  blank=True)
    pending_reminders = fields.ListField(blank=True, default=[])
    tags = fields.ListField(fields.CharField(), default=[], blank=True)

    def status(self, members_joined_count):

        description_str = self.description + "\n" if self.description else ""

        solved_count = len(
            list(filter(lambda x: x.solved_at is not None, self.challenges)))
        total_count = len(self.challenges)
        status = (
            f":triangular_flag_on_post: **{self.name}** ({members_joined_count} Members joined)\n{description_str}"
            # + f"```CSS\n{draw_bar(solved_count, total_count, style=5)}\n"
            + f" {solved_count} Solved / {total_count} Total")
        if self.start_date:
            fmt_str = "%d/%m %H:\u200b%M"
            start_date_str = self.start_date.strftime(fmt_str)
            end_date_str = self.end_date.strftime(
                fmt_str) if self.end_date else "?"
            status += f"\n {start_date_str} - {end_date_str}\n"
        status += "```"
        return status

    def credentials(self):
        response = f":busts_in_silhouette: **Username**: {self.username}\n:key: **Password**: {self.password}"
        if self.url is not None:
            response += f"\n\nLogin Here: {self.url}"
        return response

    def get_chal_hash(self, name):
        return hashlib.sha1(
            f'{self.category_id}{name}'.encode()).hexdigest()[:7]

    def get_challenge(self, name):
        challenge = next(
            (c for c in self.challenges
             if c.name == name or self.get_chal_hash(c.name) == name), None)
        if not challenge:
            raise NotFound("Challenge not found.")
        return challenge
        # return next((c for c in self.challenges if c.name == name or self.get_chal_hash(c.name) == name), None)

    def challenge_summary(self, category):
        if len(self.challenges) == 0:
            raise NotFound(
                "No challenges found. Add one with `>challenge add <name> <category>`"
            )

        solved_response, unsolved_response = "", ""

        challenges = self.challenges
        if category and category != 'all':
            challenges = filter(lambda c: category in c.tags, challenges)
        challenges = sorted(challenges,
                            key=lambda c:
                            (c.tags, c.name, c.solved_at or c.created_at))

        for challenge in challenges:
            chal_hash = self.get_chal_hash(challenge.name)
            challenge_details = f'[{chal_hash} :: {challenge.name}]'
            notes_url = f'[notes]({challenge.notebook_url})'
            flag = f'{{{challenge.flag or ""}}}'
            tags = ",".join(challenge.tags)

            if challenge.solved_at:
                solved_response += f'> {challenge_details}({tags}) <{",".join(challenge.solved_by)}> {flag}\n'
            else:
                if len(challenge.working_on) > 0:
                    unsolved_response += f'* {challenge_details}({tags}) <{",".join(challenge.working_on)}> {flag}\n'
                else:
                    unsolved_response += f'< {challenge_details}({tags}) < -- > {flag}\n'

        return solved_response, unsolved_response

    class Meta:
        collection_name = "ctf"
        ignore_unknown_fields = True
        indexes = [
            IndexModel([('guild_id', 1), ('name', 1)], unique=True),
            IndexModel([('guild_id', 1), ('category_id', 1)], unique=True)
        ]
示例#14
0
class InventoryItem(EmbeddedMongoModel):
    name = fields.CharField(required=True)
    amount = fields.IntegerField(default=0)
示例#15
0
class Image(EmbeddedMongoModel):
    image_url = fields.CharField(required=True)
    alt_text = fields.CharField()
    photographer = fields.ReferenceField(Contributor)
示例#16
0
class OriginalImages(MongoModel):
    name = fields.CharField()
    b64_string = fields.CharField()
    upload_timestamp = fields.CharField()
    upload_size = fields.ListField()
示例#17
0
class Post(MongoModel):
    body = fields.CharField()
    images = fields.EmbeddedDocumentListField(Image)
示例#18
0
class InvertedImages(MongoModel):
    name_inv = fields.CharField()
    b64_string_inv = fields.CharField()
    processed_timestamp = fields.CharField()
    processed_size = fields.ListField()
示例#19
0
class ShortLink(MongoModel):
    shortLink = fields.CharField(primary_key=True)
    longLink = fields.CharField()
示例#20
0
class AtributoDict(EmbeddedMongoModel):
    nombre = fields.CharField()
    descripcion = fields.CharField()
示例#21
0
class Karma(MongoModel):

    @staticmethod
    def _get_current_net_karma(**kwargs) -> list:
        # aggregate defaults
        projection = {"$project": {"_id": "$_id", "recipient": {"$toLower": "$awarded_to_username"},
                                   "net_karma": {"$cond": [{"$eq": ["$karma_type", str(KarmaType.POZZYPOZ)]}, 1, -1]},
                                   "awarded": "$awarded"}}
        match = {"$match": {'awarded': {'$gt': _get_cut_off_date()}}}
        grouping = {"$group": {"_id": "$recipient", "net_karma": {"$sum": "$net_karma"}}}
        sort = {"$sort": {"net_karma": -1}}
        limit = 3

        # kwarg aggregate overrides
        if "recipient" in kwargs:
            match["$match"]["recipient"] = str(kwargs["recipient"]).lower()
        if "sort" in kwargs and kwargs["sort"] == "asc":
            sort["$sort"]["net_karma"] = 1
        if "limit" in kwargs:
            limit = int(kwargs["limit"])

        # execution
        query_set = Karma.objects.aggregate(projection,
                                            match,
                                            grouping,
                                            sort)
        result_set = list(query_set)[:limit]
        return [{"username": r["_id"], "net_karma": r["net_karma"]} for r in result_set]

    @staticmethod
    def get_leader_board(size: int=3) -> list:
        return Karma._get_current_net_karma(limit=size)

    @staticmethod
    def get_loser_board(size: int=3) -> list:
        return Karma._get_current_net_karma(limit=size, sort="asc")

    @staticmethod
    def get_current_net_karma_for_recipient(recipient: str) -> int:
        net_karma_results = Karma._get_current_net_karma(recipient=recipient)
        if len(net_karma_results) == 0:
            return 0
        recipient_net_karma = net_karma_results[0]
        return recipient_net_karma["net_karma"]

    @staticmethod
    def get_current_karma_reasons_for_recipient(recipient: str) -> dict:
        projection = {"$project": {"_id": "$_id",
                                   "awarded_to_username": {"$toLower": "$awarded_to_username"},
                                   "awarded_by_username": {"$toLower": "$awarded_by_username"},
                                   "karma_type": "$karma_type",
                                   "awarded": "$awarded",
                                   "reason": "$reason"
                                   }}
        match = {'$match': {'awarded_to_username': recipient.lower(),
                            'awarded': {'$gt': _get_cut_off_date()}}}
        query_set = Karma.objects.aggregate(projection, match)
        recent_karma = [Karma(awarded_to_username=k["awarded_to_username"],
                              awarded_by_username=k["awarded_by_username"],
                              karma_type=k["karma_type"],
                              awarded=k["awarded"],
                              reason=k["reason"],
                              _id=k["_id"]) for k in list(query_set)]
        karma_with_reasons = [k for k in recent_karma if k.reason != Karma.default_reason]
        karma_without_reasons = [k for k in recent_karma if k.reason == Karma.default_reason]
        return {'reasonless': len(karma_without_reasons), 'reasoned': karma_with_reasons}

    awarded_to_username = fields.CharField()
    default_reason = ""
    awarded_by_username = fields.CharField()
    reason = fields.CharField(blank=True)
    awarded = fields.DateTimeField()
    karma_type = fields.CharField()

    def validate_auto_pozzypoz(self):
        valid = True
        if self.karma_type == str(KarmaType.POZZYPOZ):
            valid = self.awarded_by_username != self.awarded_to_username  # can't give yourself positive karma
        if not valid:
            raise ValidationError("Can't give yourself positive karma")

    def clean(self):
        self.validate_auto_pozzypoz()


    class Meta:
        write_concern = WriteConcern(j=True)
        connection_alias = Config.Connection
示例#22
0
class AtributoDictList(EmbeddedMongoModel):
    nombre = fields.CharField()
    list_atributos_dict = fields.EmbeddedDocumentListField(AtributoDict,
                                                           default=[])
示例#23
0
class DerivedClass(BaseClass):
    dspecific = fields.CharField()
示例#24
0
class AtributoMagnitudDict(EmbeddedMongoModel):
    nombre = fields.CharField()
    valor = fields.FloatField()
    unidades = fields.FloatField()
    descripcion = fields.CharField()
示例#25
0
class BookmarksVersion(MongoModel):
    page = fields.ReferenceField(BookmarksPage)
    timestamp = fields.DateTimeField()
    sections = fields.EmbeddedDocumentListField(Section, blank=True)
    summary = fields.CharField(blank=True)
    links = fields.ListField(fields.CharField(), default=[], blank=True)
示例#26
0
class SubCategoriaModel(EmbeddedMongoModel):
    codigo = fields.CharField()
    nombre = fields.CharField()
    descripcion = fields.CharField()
    imagen = fields.CharField()
示例#27
0
from pymongo import TEXT
from pymodm import connect, fields, MongoModel, EmbeddedMongoModel
from bson.objectid import ObjectId
import datetime
import os
import pymodm
from bson.json_util import dumps, RELAXED_JSON_OPTIONS, DatetimeRepresentation
import json

currency_field = fields.CharField(required=True, choices=("USD", "INR"))
money_field = fields.CharField(required=True)


class BaseMongoModel(MongoModel):
    def save(self, *args, **kwargs):
        if not self.id:
            self.id = ObjectId()
            self.date_created = datetime.datetime.now()
        self.date_modified = datetime.datetime.now()
        super(BaseMongoModel, self).save(*args, **kwargs)

    class Meta:
        collection_name = os.getenv("DB_NAME", "cff_dev")


class Center(EmbeddedMongoModel):
    name = fields.CharField()


class User(BaseMongoModel):
    name = fields.CharField(blank=True)
示例#28
0
class Contributor(MongoModel):
    name = fields.CharField()
    thumbnail = fields.EmbeddedDocumentField('Image')
示例#29
0
class Center(EmbeddedMongoModel):
    name = fields.CharField()
示例#30
0
class Agent(MongoModel):
    course_id = fields.CharField()
    file_name = fields.CharField()
    project_id = fields.CharField()