Exemple #1
0
    def find(collectionName,
             query,
             sortingAttr="_id",
             sortOrder=1,
             pageNumber=1,
             numberOfPages=None):
        db = getDb()

        # Getting the question with the parameters: pagination and sort
        offset = (pageNumber - 1) * LIMIT
        cursor = db[collectionName].find(query)

        # TODO: try removing or update the try/except statement with better version
        # If sorting Attribute is not a valid one; do not sort
        try:
            if sortingAttr != "" or sortingAttr is not None:
                cursor = cursor.sort(sortingAttr, sortOrder)
        except Exception as e:
            pass

        print(numberOfPages)

        if numberOfPages is None:
            count = cursor.count()
            results = cursor.skip(offset).limit(LIMIT)
            numberOfPages = math.ceil(count / LIMIT)
        else:
            results = cursor

        return {"data": list(results), "numberOfPages": numberOfPages}
Exemple #2
0
    def check_exists(self):
        db = getDb()

        existing = db[self.collectionName].find_one({"body": self.body})

        if existing is not None:
            return True

        return False
Exemple #3
0
    def get(self, threshold, pageNumber = 1, filterData = None):
        db = getDb()

        if self.questionsData:
            # Filtering according to threshold
            filterThreshold = list(filter(lambda x: x['similarityRate'] >= threshold, self.questionsData))

            # Splitting according to ids and rates
            questionsIds = list(map(lambda x: x['questionId'], filterThreshold))
            similarityRates = list(map(lambda x: x['similarityRate'], filterThreshold))

            if len(questionsIds) == 0:
                return {"data": []}

            # Generatin query
            finalQuery = None
            queryGen = CustomQueryGenerator()
            queryGen.addIdFields(questionsIds[(pageNumber * LIMIT) - LIMIT: pageNumber * LIMIT])
            status, queryDict = queryGen.getCompleteQuery()

            if not status:
                return None

            # Adding filter query
            if filterData:
                filterDict, sortingAttr, sortOrder = filterData

                # Merging two queries
                mergedQueryDict = CustomQueryGenerator.appendTwoQueries(queryDict, filterDict)

                if mergedQueryDict:
                    finalQuery = mergedQueryDict
                else:
                    finalQuery = queryDict
            else:
                finalQuery = queryDict

            numberOfPages = len(questionsIds) // LIMIT
            print(numberOfPages)

            # Sending final query
            results = Question.find(finalQuery, pageNumber = 1, numberOfPages = numberOfPages)
            questions = list(results["data"])

            print("value", results['numberOfPages'])

            # Adding the similarity rates to the result
            for i in range(len(questions)):
                idx = questionsIds.index(questions[i]['_id'])
                questions[i]['similarityRate'] = similarityRates[idx]

            # Updating the questions in the dict
            results["data"] = questions

            return results
        else:
            return None
Exemple #4
0
    def check_exists(self):
        db = getDb()

        usernameExisting = db[COLLECTION_NAME].find_one(
            {"username": self.username})
        emailExisting = db[COLLECTION_NAME].find_one({"email": self.email})

        usernameResult = True if usernameExisting is not None else False
        emailResult = True if emailExisting is not None else False

        return usernameResult, emailResult
Exemple #5
0
    def update_one(self, updateValues, collectionName):
        db = getDb()

        if self._id:
            db[collectionName].update_one({"_id": ObjectId(self._id)},
                                          {"$set": updateValues})
            self.init(vars(self), collectionName)

            return True

        return False
Exemple #6
0
    def check_exists(self):
        db = getDb()

        messageExisting = db[COLLECTION_NAME].find_one({
            "message":
            self.message,
            "questionId":
            self.questionId
        })
        messageResult = True if messageExisting is not None else False

        return messageResult
Exemple #7
0
    def check_exists(self):
        db = getDb()

        existing = db[COLLECTION_NAME].find_one({"body": self.body})

        if existing is not None:
            # Setting the existing data
            self.questionsData = existing['questionsData']

            return True

        return False
Exemple #8
0
    def insert_one(self):
        db = getDb()

        # Statuses for existence
        usernameExists, emailExists = self.check_exists()

        if usernameExists is True:
            return False, "Username already exists"
        elif emailExists is True:
            return False, "Email already exists"
        else:
            # Insertion
            db[COLLECTION_NAME].insert_one(vars(self))

            return True, "User is created successfully"
Exemple #9
0
    def insert_one(self):
        db = getDb()

        # Temporay document indexing
        db[COLLECTION_NAME].create_index([('createdAt', 1), ("expireAfterSeconds", EXPIRE_TIME)])

        if self.check_exists():
            return False, "This question has already been searched"
        else:
            insertData = deepcopy(vars(self))
            insertData['createdAt'] = datetime.utcnow() + timedelta(hours = 3)

            db[COLLECTION_NAME].insert_one(insertData)

        return True, "Question is inserted into searched collection"
Exemple #10
0
    def find(query,
             sortingAttr="_id",
             sortOrder=1,
             pageNumber=1,
             type=QuestionType.SOC,
             numberOfPages=None):
        db = getDb()
        collectionName = getCollectionName(type)
        db[collectionName].create_index([("body", "text")])

        return Model.find(collectionName,
                          query,
                          sortingAttr,
                          sortOrder,
                          pageNumber,
                          numberOfPages=numberOfPages)
Exemple #11
0
    def init(self, obj, collectionName):

        # Getting by some key
        keys = list(obj.keys())
        key = keys[0] if len(keys) == 1 else None

        if key:
            db = getDb()
            value = obj[key] if key != "_id" else ObjectId(obj[key])

            data = db[collectionName].find_one({key: value})

            if data:
                properties = dict(data).keys()
                for property in properties:
                    setattr(self, property, data[property])

                setattr(self, "collectionName", collectionName)
Exemple #12
0
    def insert_one(self, analyze=True):
        db = getDb()

        # Check if the same question exists
        if self.check_exists():
            return False, "Question already exists", None
        else:
            # Analyze
            if analyze is True and self.type == QuestionType.SOC:
                entity_tags, topics, categories = analyzeQuestion(self.body)

                self.entity_tags = entity_tags
                self.topics = topics
                self.categories = categories

            # Insertion
            result = db[self.collectionName].insert_one(self.data())
            questionId = result.inserted_id

        return True, "Question is inserted", questionId
Exemple #13
0
    def insert_one(self):
        db = getDb()

        # Statuses for existence
        postExists = self.check_exists()

        if postExists is True:
            return False, "Post already exists"
        else:
            # Insertion
            result = db[COLLECTION_NAME].insert_one(vars(self))
            postId = ObjectId(result.inserted_id)

            user = User({"_id": self.userId}).data()
            userFriends = user['friends']

            for friendId in userFriends:
                friend = User({"_id": friendId})
                friend.addFeed(postId)

            return True, "Post is created successfully"
Exemple #14
0
    def find_one(email, password):
        db = getDb()

        # Fetching...
        user = db[COLLECTION_NAME].find_one({"email": email})

        # If no user
        if user is None:
            return None

        userPassword = user['password']

        # Password validation
        isCorrectPassword = validatePassword(userPassword, password)

        if isCorrectPassword is True:
            userData = deepcopy(user)
            del userData['password']  # Removing the password

            return userData
        else:
            return None
Exemple #15
0
    def aggregate(query, type=QuestionType.SOC):
        db = getDb()
        collectionName = getCollectionName(type)
        results = db[collectionName].aggregate(query)

        return list(results)
Exemple #16
0
    def findGeneric(query, type=QuestionType.SOC):
        db = getDb()
        collectionName = getCollectionName(type)
        results = db[collectionName].find(query)

        return list(results)