예제 #1
0
파일: orm.py 프로젝트: hzx/wender
  def checkPaging(self, coll, wherePrev, whereNext):
    names = self.collToNames(coll)
    dotcoll = '.'.join(names)

    po = mongodb.selectOne(dotcoll, wherePrev)
    no = mongodb.selectOne(dotcoll, whereNext)
    return { 'prev': po != None, 'next': no != None}
예제 #2
0
파일: orm.py 프로젝트: hzx/wender
  def selectFromRefLink(self, src, collNames, where, parentid):
    if not parentid:
      raise Exception('select from ref, link must provide parent id')

    parentNames = collNames[:-1]
    if len(parentNames) == 1:
      parentColl = parentNames[0]
    else:
      parentColl = self.getSrcColl(parentNames)
    fieldName = collNames[len(collNames) - 1]

    # get parent object
    parentObj = mongodb.selectOne(parentColl, {'id': parentid})

    # get ids field
    idsField = parentObj[fieldName]

    # get coll with ids
    items = []
    for curr in idsField:
      item = mongodb.selectOne(src, {'id': curr})
      items.append(item)
    # items = mongodb.selectFrom(src, {'id':{'$in': idsField}})

    return items
예제 #3
0
파일: orm.py 프로젝트: hzx/wender
  def selectOne(self, coll, where):
    names = self.collToNames(coll)
    if not names:
      return None
    dotcoll = '.'.join(names)

    if dotcoll in self.meta.refToColl:
      src = self.meta.refToColl[dotcoll]
      return mongodb.selectOne(src, where)
    elif dotcoll in self.meta.linkToColl:
      src = self.meta.linkToColl[dotcoll]
      return mongodb.selectOne(src, where)
    elif dotcoll in self.meta.colls:
      return mongodb.selectOne(dotcoll, where)
    return None
예제 #4
0
파일: orm.py 프로젝트: hzx/wender
  def deleteFromInnerColl(self, coll, parentId, field, objid):
    """
    In coll object with parentId search array field and remove objid.
    """
    # get parent object
    parentObj = mongodb.selectOne(coll, {'id': parentId})
    # get coll fields
    parentFields = self.meta.getCollType(coll.split('.'))
    # get inner coll fields
    innerFields = self.meta.getStruct(parentFields[field]['type'])
    # get field
    arr = parentObj[field]
    # from arr remove objid element
    index = -1
    for i, item in enumerate(arr):
      if item['id'] == objid:
        index = i
        break
    # if index not found just return
    if index == -1:
      return

    # remove images from item
    item = arr[index]
    for name, params in innerFields.items():
      if 'imageSizes' in params:
        filename = item[name]
        self.deleteImage(filename, params)
    # remove element from arr
    arr.pop(index)
    # update parentObj
    mongodb.update(coll, {field: arr}, {'id': parentId})
예제 #5
0
파일: orm.py 프로젝트: hzx/wender
 def appendInnerColl(self, coll, parentId, field, obj):
   """
   In coll object with parentId search array field and append obj.
   """
   # print coll
   # print parentId
   # print field
   # print repr(obj)
   # get parent object
   parentObj = mongodb.selectOne(coll, {'id': parentId})
   # get field
   arr = parentObj[field]
   # to arr add obj
   arr.append(obj)
   # update parentObj
   mongodb.update(coll, {field: arr}, {'id': parentId})
예제 #6
0
파일: orm.py 프로젝트: hzx/wender
  def createBase(self):
    """
    For each field in the world structure create default object
    with default fields
    """
    fields = self.meta.getStruct('World')
    for name, params in fields.items():
      if params['isValueType'] or params['isArray']:
        continue

      collobj = mongodb.selectOne(name, {})
      if collobj:
        continue

      # if default object not found, create it
      default = self.composeDefaultObject(params['type'])

      mongodb.insert(name, default)
예제 #7
0
파일: orm.py 프로젝트: hzx/wender
  def deleteRefLink(self, names, objid, parentid):
    if not parentid:
      raise Exception('delete from ref, link must provide parent id')

    parentNames = names[:-1]
    if len(parentNames) == 1:
      parentColl = parentNames[0]
    else:
      parentColl = self.getSrcColl(parentNames)
    fieldName = names[len(names) - 1]

    # get parent object
    parentObj = mongodb.selectOne(parentColl, {'id': parentid})

    # get ids field
    idsField = parentObj[fieldName]

    # remove id from idsField
    if objid in idsField:
      idsField.remove(objid)
      # update ids field
      mongodb.update(parentColl, {fieldName: idsField}, {'id': parentid})
예제 #8
0
파일: orm.py 프로젝트: hzx/wender
  def appendIdToRefLink(self, collNames, objid, parentid):
    if not parentid:
      raise Exception('append to ref, link must provide parent id')

    parentNames = collNames[:-1]
    if len(parentNames) == 1:
      parentColl = parentNames[0]
    else:
      parentColl = self.getSrcColl(parentNames)
    fieldName = collNames[len(collNames) - 1]

    # get parent object
    parentObj = mongodb.selectOne(parentColl, {'id': parentid})

    # get ids field
    idsField = parentObj[fieldName]

    # add id to ids field
    if not (objid in idsField):
      idsField.append(objid)
      # update ids field
      mongodb.update(parentColl, {fieldName: idsField}, {'id': parentid})
예제 #9
0
파일: orm.py 프로젝트: hzx/wender
  def load(self, useraccess):
    """
    Load database for userKind
    """
    db = {}

    accessre = self.accessToRe[useraccess]
    accessReadRe = accessre['read']
    # accessWriteRe = accessre['write']

    for docname, params in self.meta.docs.items():
      access = params.get('access', '----')
      if not accessReadRe.match(access):
        continue

      if params['isArray']:
        if self.isArrayLazy(params):
          continue
        db[docname] = dbutil.cursorToList(mongodb.selectFrom(docname, {}))
      else:
        db[docname] = mongodb.selectOne(docname, {})
    return db
예제 #10
0
파일: orm.py 프로젝트: hzx/wender
  def insertBeforeToRefLink(self, names, objid, parentid, beforeid):
    if not parentid:
      raise Exception('insert to ref, link must provide parent id')

    parentNames = names[:-1]
    if len(parentNames) == 1:
      parentColl = parentNames[0]
    else:
      parentColl = self.getSrcColl(parentNames)
    fieldName = names[len(names) - 1]

    # get parent object
    parent = mongodb.selectOne(parentColl, {'id': parentid})

    # get ids field
    idsField = parent[fieldName]

    # add id before beforeid
    if (beforeid in idsField):
      beforeIndex = idsField.index(beforeid)
      # insert in index position (before)
      idsField.insert(beforeIndex, objid)
      # update ids field
      mongodb.update(parentColl, {fieldName: idsField}, {'id': parentid})
예제 #11
0
파일: orm.py 프로젝트: hzx/wender
  def deleteCollItem(self, coll, itemId):
    """
    Do work in 2 stages:
      1. search what to do
      2. process search result
    Delete inner arrays.
    Delete images.
    """
    # store search results of [name, params]
    imageFields = []
    images = []
    # store search results of [name, params]
    links = []
    # store search results of name, name, ...
    internalColls = []

    # get coll type
    names = coll.split('.')
    fields = self.meta.getCollType(names)
    # search array field, image field in fields
    for name, params in fields.items():
      # found image by imageSizes
      if 'imageSizes' in params:
        imageFields.append([name, params])
        continue
      # found array field
      if params['isArray']:
        # ignore ref array
        if 'ref' in params:
          continue
        # for link save
        if 'link' in params:
          links.append([name, params])
          continue
        # for internal collection save
        else:
          internalColls.append(name)
          continue

    # get item from coll
    item = mongodb.selectOne(coll, {'id': itemId})

    for imgf in imageFields:
      imgfFilename = item[imgf[0]]
      imgfParams = fields[imgf[0]]
      images.append([imgfFilename, imgfParams])

    # process internalColls
    for collname in internalColls:
      intcoll = item[collname]
      # for each obj in intcoll search images
      collnames = names + [collname]
      collfields = self.meta.getCollType(collnames)
      # in coll fields search images
      for tmpname, tmpparams in collfields.items():
        # found image field
        if 'imageSizes' in tmpparams:
          # for every intcoll add images
          for tmpitem in intcoll:
            tmpfilename = tmpitem[tmpname]
            images.append([tmpfilename, tmpparams])


    # process images

    for image in images:
      filename = image[0]
      params = image[1]
      self.deleteImage(filename, params)

    # process links
    for link in links:
      linkname = link[0]
      linkparams = link[1]
      # get link ids from item
      ids = item[linkname]
      # get src coll
      src = linkparams['link']
      # delete from src coll for every id
      for linkid in ids:
        self.deleteCollItem(src, linkid)

    # delete from coll
    mongodb.delete(coll, {'id': itemId})
예제 #12
0
파일: orm.py 프로젝트: hzx/wender
  def setValue(self, seq):
    # parse sequences
    # print 'orm.setValue, seq:'
    # print repr(seq)

    # get last list
    lastList = self.getLastList(seq)

    # global update
    if not lastList:
      # print 'global update'

      names = self.seqToNames(seq)
      value = self.getSeqValue(seq)
      coll = names[0]
      field = names[1]

      # update slugs
      slugs = self.updateSlug([coll], field, value['value'])
      values = {field: value['value']}
      if slugs:
        values[slugs[0]] = slugs[1]

      # get value params
      fieldnames = coll + '.' + field
      params = self.meta.getValueParams(fieldnames)

      # if imageSizes in params - remove old image value
      if 'imageSizes' in params:
        # get object with field
        obj = mongodb.selectOne(coll, {'id': value['parentid']})
        filename = obj[field]
        self.deleteImage(filename, params)

      mongodb.update(coll, values, {'id': value['parentid']})

    # list update
    else:
      listNames = self.getLastListNames(seq, lastList)
      coll = '.'.join(listNames)

      # search src list and update
      # search src in refs
      if coll in self.meta.refToColl:
        # print 'update ref value:'
        # print coll
        src = self.meta.refToColl[coll]
        # print src
        self.updateListValue(src, seq, lastList)
      # search src in links
      elif coll in self.meta.linkToColl:
        # print 'update link value:'
        # print coll
        src = self.meta.linkToColl[coll]
        # print src
        self.updateListValue(src, seq, lastList)
      # search src in colls
      elif coll in self.meta.colls:
        # print 'update list value:'
        # print coll
        # print repr(lastList)
        self.updateListValue(coll, seq, lastList)
      else:
        raise Exception('unknown list type "%s"' % str(coll))