def chrome_extension_redirect(): collection_id = request.args.get("id") url = request.args.get("url") collection = None if collection_id: collection = Collection.query.filter( Collection.id == collection_id).first() else: collection = Collection() user = g.user collection.creator = user collection.title = request.args.get('title') collection.append_child( CollectionItem(parent=collection, content=str(url))) db.session.add(collection) db.session.commit() return redirect(url) if collection and url and g.user == collection.creator: collection.append_child( CollectionItem(parent=collection, content=str(url))) cache_request(url) db.session.add(collection) db.session.commit() return redirect(url) else: #this should be improved return abort(404)
def add_book(self, user_num, book): for item in self.items: if item.book.id == book.id and item.user_num == user_num: collection_list = CollectionItem.objects.filter(user_num=user_num, book_id__exact=book.id) collection_list.update(dis_collect=False) collection_list.update(add_date = datetime.now()) return collection_item = CollectionItem(user_num=user_num, book=book, dis_collect=False) collection_item.save() self.items.append(collection_item)
def register_menu(subitem_url, subitem_text, parent_path = "/", permmited_group = None): """ If subitem_test contains dynamic, subitem_url is not used. Otherwise, subitem_url is the content of this menu item. Register a menu item in the left tree in object manager, the info is stored in obj_sys.models.Collection. :param subitem_url: menu item's URL. When the item is clicked, the URL will be loaded to the content pane :param subitem_text: menu item's text. It is stored in to id_in_col field for Collection and if it is "dynamic://xxxx", the parent item's children will be dynamically generated by opening URL: xxxx. xxxx should return a collection of items as in tags.tag_list. The format is described in tags.tag_list as well. :param parent_path: the parent for this menu item. Root item is "/", sub menus should start with "/" as well. :param permmited_group: :return: N/A """ try: root_uuid = get_item_id(parent_path) url = u"view://%s"%(subitem_url) qs = UfsObj.objects.filter(ufs_url = url) if 0 == qs.count(): print 'creating new ufs obj' o = UfsObj(ufs_url = url, uuid = unicode(uuid.uuid4()), timestamp=timezone.now(), user=User.objects.filter(username="******")[0]) o.save() else: #print 'use existing item' o = qs[0] except django.db.utils.DatabaseError: #Database is not created yet, just return, items will be created after syncdb is executed return #print 'creating collection item for root: ', root_uuid if permmited_group is None: #If no permission requested, set anonymous user accessable. permitted_user_or_group = User.objects.filter(pk=settings.ANONYMOUS_USER_ID)[0] else: try: permitted_user_or_group = Group.objects.filter(name = permmited_group)[0] except: #Group not exist, create it permitted_user_or_group = Group.objects.create(name = permmited_group) collqs = CollectionItem.objects.filter(uuid = root_uuid, id_in_col = subitem_text) if 0 == collqs.count(): c = CollectionItem(obj = o, uuid = root_uuid, id_in_col = subitem_text, timestamp=timezone.now(), user=User.objects.filter(username="******")[0]) c.save() else: c = collqs[0] #Assign group permission assign_perm('view_collection_item', permitted_user_or_group, c)
def create_item(collection_id): collection = Collection.query.get(collection_id) item = CollectionItem(**request.form) collection.items.append(item) collection.save() return redirect(f'/collections/{collection_id}')
def api_create_collection(): data = request.get_json() print data if data.get('unique_id'): collection = Collection.query.filter_by( unique_id=data.get("unique_id")).first() collection.title = data.get('title', None) collection.is_public = data.get('is_public') collection.timestamp = datetime.utcnow() layout = data.get('layout') if layout == 'one': collection.collection_layout = 'col-sm-6 col-sm-offset-3' elif layout == 'two': collection.collection_layout = 'col-sm-6' elif layout == 'three': collection.collection_layout = 'col-sm-4' if g.user == collection.creator: collection.collection_items.delete() if data.get('items'): for item in data.get('items'): collection.append_child( CollectionItem(parent=collection, content=str(item))) cache_request(data.get('items')) db.session.add(collection) db.session.commit() else: collection = Collection() user = g.user if user: collection.creator = user collection.title = data.get('title', None) collection.is_public = data.get('is_public') layout = data.get('layout') if layout == 'one': collection.collection_layout = 'col-sm-6 col-sm-offset-3' elif layout == 'two': collection.collection_layout == 'col-sm-6' elif layout == 'three': collection.collection_layout = 'col-sm-4' for item in data["items"]: collection.append_child( CollectionItem(parent=collection, content=str(item))) db.session.add(collection) db.session.commit() return jsonify(ok=True, uri=str(url_for('board', unique_id=collection.unique_id)))
def register(objectClass, group_name = "scheduling"): module_name = objectClass.__module__.split(".")[0].lower() class_name = objectClass.__name__.lower() url = u"view://admin/%s/%s/add"%(module_name, class_name) try: for i in UfsObj.objects.filter(ufs_url = url): return except django.db.utils.DatabaseError: #Database is not created yet, just return, items will be created after syncdb is executed return o = UfsObj(ufs_url = url, uuid = unicode(uuid.uuid4()), timestamp=timezone.now(), user=User.objects.filter(username="******")[0]) o.save() c = CollectionItem(obj = o, uuid = gRootUuid, id_in_col="%s_%s_add"%(module_name, class_name), timestamp=timezone.now(), user=User.objects.filter(username="******")[0]) c.save() #Add to group try: group = Group.objects.filter(name=group_name)[0] except: #Group not exist, create it group = Group.objects.create(name=group_name) #print 'assigning: ', group, c assign_perm('view_collection_item', group, c)
def parse_to_ABCDModel(self): """Very raw implementation of the parser""" if self.xml_str is None: raise TypeError('Expected \'str\' type \'None\' provided!') e = xml.etree.ElementTree.fromstring(self.xml_str.encode('utf-8')) content = e.find(c.abcd_path_content.format(self.biocase_ns_str), self.ns) record_count = content.attrib['recordCount'] record_dropped = content.attrib['recordDropped'] total_searched_hits = content.attrib['totalSearchHits'] if record_count is None: raise ValueError('\'recordCount\' can\'t be None.') if record_dropped is None: raise ValueError('\'recordDropped\' can\'t be None.') if total_searched_hits is None: raise ValueError('\'totalSearchHits\' Count can\'t be None.') try: record_count = int(record_count) record_dropped = int(record_dropped) total_searched_hits = int(total_searched_hits) except ValueError: raise ( 'failed to parse attributes to int. provided: ' '{recordCount: {0}, recordDropped: {1}, totalSearchHits: {2}}'. format(record_count, record_dropped, total_searched_hits)) technical_contact_name = e.find( c.abcd_path_technical_contact_name.format(self.biocase_ns_str, self.abcd_ns_str), self.ns) technical_contact_name = technical_contact_name.text if technical_contact_name is not None else None technical_contact_email = e.find( c.abcd_path_technical_contact_email.format(self.biocase_ns_str, self.abcd_ns_str), self.ns) technical_contact_email = technical_contact_email.text if technical_contact_email is not None else None organisation_name = e.find( c.abcd_path_org_representation_name.format(self.biocase_ns_str, self.abcd_ns_str), self.ns) organisation_name = organisation_name.text if organisation_name is not None else None organisation_abbrv = e.find( c.abcd_path_org_representation_abbrv.format( self.biocase_ns_str, self.abcd_ns_str), self.ns) organisation_abbrv = organisation_abbrv.text if organisation_abbrv is not None else None organisation_address = e.find( c.abcd_path_org_address.format(self.biocase_ns_str, self.abcd_ns_str), self.ns) organisation_address = organisation_address.text if organisation_address is not None else None units = e.findall( c.abcd_path_unit.format(self.biocase_ns_str, self.abcd_ns_str), self.ns) if organisation_name is None: return None data_provider = DataProvider() data_provider.org_name = organisation_name data_provider.org_abbrv = organisation_abbrv data_provider.address = organisation_address data_provider.contact_name = technical_contact_name data_provider.contact_email = technical_contact_email abcd_model = ABCDModel() abcd_model.provider = data_provider abcd_model.record_count = record_count abcd_model.record_dropped = record_dropped abcd_model.total_searched_hits = total_searched_hits for child in units: unit = {} collection_item = CollectionItem() collection_item.ABCD_model = abcd_model for key, value in c.attrs.iteritems(): name = c.attrs[key]['name'] _relative_path = c.attrs[key][ 'relative_path'] # this should be iterated. _full_path = c.attrs[key][ 'full_path'] # this should be iterated. value = child.find(_relative_path.format(self.abcd_ns_str), self.ns) value = value.text if value is not None else None unit[name] = value attribute = Attribute() attribute.id = key # this will be 'k' for key (in the dict) attribute.name = name attribute.relative_path = _relative_path attribute.full_path = _full_path attribute.values = [value] collection_item.add_attribute(attribute) if name == 'SourceInstitutionID' and data_provider.org_abbrv is None: data_provider.org_abbrv = value abcd_model.add_collection_item(collection_item) abcd_model.flag = max([ int( collection_item.get_attribute( c.attrs[c.name_unit_id]['name']).values[0]) for collection_item in abcd_model.collection_items ]) return abcd_model
def addArticleToCollection(self, col, article): colItem = CollectionItem() colItem.article_id = article.id colItem.collection_id = col.id db.session.add(colItem) db.session.commit()
def addArticleToCollection(self,col,article): colItem = CollectionItem() colItem.article_id=article.id colItem.collection_id = col.id db.session.add(colItem) db.session.commit()