I have a very standard user/group many to many relationship with the following simplified definition:
class GroupResource(ModelResource):
users = fields.ToManyField(UserResource, 'group_set')
class Meta:
cache = SimpleCache()
queryset = Group.objects.all()
resource_name = "hr/group"
When I get a list groups it returns the group along with the list of uris of users connected to the group. However, our production system is starting to get quite large and a group can have hundreds of users in it. It is becoming painstakingly slow to return a list of groups, since each group also returns with it a list of users connected to that group.
Is there a way to exclude the linked users only for obj_get_list, but include the users when viewing the group instance? Or is there another solution to this problem that will fit better?
You need to specify a callable as use_in argument to fields.ToManyField which returns False if current request corresponds to get_list end point.
Example code
def is_not_group_get_list_end_point(bundle):
# TODO: use dynamically generated path here.
if bundle.request.get_full_path() == '/api/v1/hr/group/':
return False
return True
class GroupResource(ModelResource):
users = fields.ToManyField(UserResource, 'group_set',
use_in=is_not_group_get_list_end_point)
class Meta:
cache = SimpleCache()
queryset = Group.objects.all()
resource_name = "hr/group"
Related
I have an old odoo version(v6) and I am migrating it to odoo-10, Issue I am facing is for binary field data migration. As odoo-10 has attribute "attachment=True", but for older versions this was not there.
So can I get little idea from stack community, about how can I achieve my task and how can I migrate that postgres table to odoo-10 compatible data. Thanks in advance.
Just migrate the data as is, let them exist in database. I had to write a module to achieve the same requirement, because a customer had attachments in database instead of using attachments.
The following code works, it's not offically in my company's apps in Odoo's App Store, but eventually will find its way into it ;-)
from odoo import api, models, exceptions
from odoo.osv import expression
class IrAttachment(models.Model):
""" Attachment Extensions"""
_inherit = 'ir.attachment'
#api.model
def _relocate_binary_data(
self, model=None, fields=None, domain=None, limit=0):
""" Relocates binary data into attachments. This method
has no functionality to reverse the process.
Use this to change binary fields to attachment usage,
which is done by using the parameter attachment=True
#param model: Model Name (required)
#param fields: List of binary field names (required)
#param domain: optional search domain to filter treated records
(default: []==no filter)
#param limit: optional filter limit (default: 0==unlimited)"""
if not model or not fields:
raise exceptions.Warning(
"model and fields are required parameters")
# only touch records with binary data in one of the provided fields
default_domain = [[(f, '!=', False)] for f in fields]
default_domain = expression.OR(default_domain)
domain = expression.AND([domain, default_domain])
records = self.env[model].with_context(active_test=False).search(
domain, limit=limit)
# relocate the binary data to attachments
for record in records:
for field in fields:
# search for existing attachments (for re-runs)
attachment = records.env['ir.attachment'].sudo().search([
('res_model', '=', record._name),
('res_field', '=', field),
('res_id', '=', record.id),
])
# write the binary value to existing attachment or create one
if attachment:
attachment.write({'datas': getattr(record, field)})
else:
self.env['ir.attachment'].create({
'name': record.name,
'res_model': record._name,
'res_field': field,
'res_id': record.id,
'type': 'binary',
'datas': getattr(record, field)
})
# empty the database binary data
records.write({f: None for f in fields})
You have to write a ir.cron or a ir.actions.server to use this method.
If you look at the read function for the Binary class (<path_to_v12>/odoo/fields.py lines 1786-1800, cited below) you'll notice that it searches ir.attachment for records having the right model, field and id.
def read(self, records):
# values are stored in attachments, retrieve them
assert self.attachment
domain = [
('res_model', '=', records._name),
('res_field', '=', self.name),
('res_id', 'in', records.ids),
]
# Note: the 'bin_size' flag is handled by the field 'datas' itself
data = {att.res_id: att.datas
for att in records.env['ir.attachment'].sudo().search(domain)}
cache = records.env.cache
for record in records:
cache.set(record, self, data.get(record.id, False))
So, my educated guess is that you can update your 'ir_attachment' records and adding res_model (note that this is a string!), res_field (also a string) and res_id (this is the integer saved on the id field of the referring record).
Best is to use XMLRPC to read the data from SRC and write the data in DEST. Which would take care of your issue. It would read the data from the binary field while creating attachment it would store in the filesystem.
I am using Python 3.7 and ldap3. I can make a connection and retrieve a list of the groups in which I am interested. I am having trouble getting group members though.
server = Server('ldaps.ad.company.com', use_ssl=True, get_info=ALL)
with Connection(server, 'mydomain\\ldapUser', '******', auto_bind=True) as conn:
base = "OU=AccountGroups,OU=UsersAndGroups,OU=WidgetDepartment," \
+ "OU=LocalLocation,DC=ad,DC=company,DC=com"
criteria = """(
&(objectClass=group)
(
|(sAMAccountName=grp-*widgets*)
(sAMAccountName=grp-oldWidgets)
)
)"""
attributes = ['sAMAccountName', 'distinguishedName']
conn.search(base, criteria, attributes=attributes)
groups = conn.entries
At this point groups contains all the groups I want. I want to itterate over the groups to collect the members.
for group in groups:
# print(cn)
criteria = f"""
(&
(objectClass=person)
(memberof:1.2.840.113556.1.4.1941:={group.distinguishedName})
)
"""
# criteria = f"""
# (&
# (objectClass=person)
# (memberof={group.distinguishedName})
# )
# """
attributes = ['displayName', 'sAMAccountName', 'mail']
conn.search(base, criteria, attributes=attributes)
people = conn.entries
I know there are people in the groups but people is always an empty list. It doesn't matter if I do a recirsive search or not.
What am I missing?
Edit
There is a longer backstory to this question that is too long to go into. I have a theory about this particular issue though. I was running out of time and switched to a different python LDAP library -- which is working. I think the issue with this question might be that I "formated" the query over multiple lines. The new ldap lib (python-ldap) complained and I stripped out the newlines and it just worked. I have not had time to go back and test that theory with ldap3.
people is overwritten in each iteration of your loop over groups.
Maybe the search result for the last group entry in groups is just empty.
You should initialise an empty list outside of your loop and extend it with your results:
people = []
for group in groups:
...
conn.search(...)
people.extend(conn.entries)
Another note about your code snippet above. When combining objectClass definitions with attribute definitions in your search filter you may consider using the Reader class which will combine those internally.
Furthermore I would like to point out that I've created an object relational mapper where you can simply define your queries using declarative python syntax, e.g.:
from ldap3_orm import ObjectDef, Reader
from ldap3_orm.config import config
from ldap3_orm.connection import conn
PersonDef = ObjectDef("person", conn)
r = Reader(conn, PersonDef, config.base_dn, PersonDef.memberof == group.distinguishedName)
r.search()
ldap3-orm documentation can be found at http://code.bsm-felder.de/doc/ldap3-orm
I'm developing alfresco activiti workflow and i want to assign a task to users who simultaneously are in two specific groups. Separating groups by comma in activiti:candidateGroups gives the union of groups. Is it even possible to resolve this problem?
First you need to setup some service task / or some execution listener before getting to your user task to assign to your set of users, and there you should be using this and this to fetch your groups and their underlying users.
var group1 = people.getGroup("GROUP_DUMMY");
var g1Users = [];
if(group1){
g1Users = g1Users.concat(people.getMembers(group1));
}
var group2 = people.getGroup("GROUP_SAMPLE");
var g2Users = [];
if(group2){
g2Users = g2Users.concat(people.getMembers(group2));
}
Once you do so, you should setup a new array to contain only users that belong to both groups, but instead of putting the user nodes, you should be putting user.properties.userName instead.
You must have a String array with each and every value representing a username !
And finally export that array to your execution like this execution.setVariable('scwf_candidates', users); setup your user task like this:
<userTask id="..." name="My Task" activiti:candidateUsers="${scwf_candidates}">
</userTask>
I can see from the following example how to get the table name of an OSpace type:
https://lowrymedia.com/2014/06/10/ef6-1-mapping-between-types-tables-including-derived-types/
But how do I go about getting the SSpace column name from an OSpace property name (i.e. CLR type property)?
By browsing the MetadataProperties from the corresponding CSpace property, I can see there is a "Configuration" entry containing the column name if changed using the Fluid API or ColumnAttribute, but the value of the entry is an internal class on EF's part. Is it at all possible?
I have browsed a few answers regarding this topic, but none of them take into account the Fluid API configuration.
P.S. the specific property I'm looking for is scalar, if that can simplify things...
Column Name
To get the column name, you have to first get the EdmProperty associated with that column in the “structural space” (SSpace). I provide code to do that below. Once you have the EdmProperty, the name of the column is simply EdmProperty.Name:
string GetColumnName(DbContext context, PropertyInfo property) {
return GetStructuralSpaceEdmProperty(context, property).Name;
}
Structural Space Property
This is based on an article. That article gives you enough information to map all the way to the structural EntityType. I added a bit at the end to do the actual property mapping to get the EdmProperty representing the column. As the article states, these APIs require ≥EntityFramework-6.1.
EdmProperty GetStructuralSpaceEdmProperty(DbContext context, PropertyInfo property) {
IObjectContextAdapter adapter = context;
var metadata = adapter.ObjectContext.MetadataWorkspace;
// First, you map the Object Space to the Conceptual Space.
var objectItemCollection = (ObjectItemCollection)metadata.GetItemCollection(DataSpace.OSpace);
var objectEntityType = metadata.GetItems<EntityType>(DataSpace.OSpace)
.Single(oet => objectItemCollection.GetClrType(oet) == property.DeclaringType);
// Note: we are assuming that CSpace and OSpace name their properties the
// same instead of trying to use EF’s own OSSpace mappings here.
var conceptualEntityType = metadata.GetItems<EntityType>(DataSpace.CSpace)
.Single(cet => objectEntityType.Name == cet.Name);
var conceptualEdmProperty = conceptualEntityType.Properties
.Single(ep => ep.Name == property.Name);
// Then you map the conceptual space onto the structural space.
var entitySet = metadata.GetItems<EntityContainer>(DataSpace.CSpace)
.Single().EntitySets
.Single(es => es.ElementType.Name == conceptualEntityType.Name);
var entityMapping = metadata.GetItems<EntityContainerMapping>(DataSpace.CSSpace)
.Single().EntitySetMappings
.Single(esm => esm.EntitySet == entitySet);
// The entity may be split to different tables or fragments.
var fragments = entityMapping.EntityTypeMappings
.SelectMany(etm => etm.Fragments);
var propertyMappings = fragments.SelectMany(f => f.PropertyMappings);
// Normal properties will be “ScalarPropertyMapping”.
// Depending on what information you are seeking or your
// model, you may be interested in other PropertyMapping.
var structuralSpaceProperty = propertyMappings
.OfType<ScalarPropertyMapping>()
.Single(pm => pm.Property == conceptualEdmProperty).Column;
return structuralSpaceProperty;
}
Note that once you have EdmProperty in structural space, there are a bunch of other useful properties you can read from it. For example, for SQL Server, EdmProperty.IsUnicode will be true for NVARCHAR/NCHAR and false for VARCHAR/CHAR types whereas this property is not set to a useful value in the conceptual space.
Random Information
Spaces in EF
Alex D. James’s blog post “Tip 10 — How to understand Entity Framework jargon” explains some of the terms of the API which do not make sense on their own. DataSpace.OSpace stands for “Object Space”, meaning the .net POD classes. DataSpace.SSpace stands for “Structural Space”, probably named after “structured” in the term “SQL” and thus meaning it most directly describes the backend database. DataSpace.CSpace stands for “Conceptual Space” which seems intended to be a neutral space which both the “Object Space” and “Structural Space” can map into. DataSpace.OCSpace stands for the mapping from the object space onto the conceptual space. We bypass this mapping because we assume that property names in the object space are the same as in our .net types. DataSpace.CSSpace stands for the mapping of conceptual space onto structural space. We use this mapping because the model may be configured to use a different column name via the fluent API or ColumnAttribute.
API Confusion
The metadata API of EF seems to assume that the consumer of the API has an understanding of the internals of EF to an extent. It is not made in a very type safe way which helps consumers. For example, the fact that we had to use Enumerable.OfType<TResult> to get to ScalarPropertyMapping means that one has to know to expect the collection to have ScalarPropertyMapping instances in it. Likewise, the MetadataWorkspace.GetItems<T>() method requires us to know that the sorts of items one would find in the metadata include EntityType. Thus, a deep understanding of the internals of EF or complete examples are necessary to write code that consumes the mapping portion of these APIs.
I'm using Symfony 1.2 with Doctrine. I have a Place model with translations in two languages. This Place model has also a nested set behaviour.
I'm having problems now creating a new place that belongs to another node. I've tried two options but both of them fail:
1 option
$this->mergeForm(new PlaceTranslationForm($this->object->Translation[$lang->getCurrentCulture()]));
If I merge the form, what happens is that the value of the place_id field id an array. I suppose is because it is waiting a real object with an id. If I try to set place_id='' there is another error.
2 option
$this->mergeI18n(array($lang->getCurrentCulture()));
public function mergeI18n($cultures, $decorator = null)
{
if (!$this->isI18n())
{
throw new sfException(sprintf('The model "%s" is not internationalized.', $this->getModelName()));
}
$class = $this->getI18nFormClass();
foreach ($cultures as $culture)
{
$i18nObject = $this->object->Translation[$culture];
$i18n = new $class($i18nObject);
unset($i18n['id']);
$i18n->widgetSchema['lang'] = new sfWidgetFormInputHidden();
$this->mergeForm($i18n); // pass $culture too
}
}
Now the error is:
Couldn't hydrate. Found non-unique key mapping named 'lang'.
Looking at the sql, the id is not defined; so it can't be a duplicate record (I have a unique key (id, lang))
Any idea of what can be happening?
thanks!
It looks like the issues you are having are related to embedding forms within each other, which can be tricky. You will likely need to do things in the updateObject/bind methods of the parent form to get it to pass its values correctly to its child forms.
This article is worth a read:
http://www.blogs.uni-osnabrueck.de/rotapken/2009/03/13/symfony-merge-embedded-form/comment-page-1/
It gives some good info on how embedding (and mergeing) forms work. The technique the article uses will probably work for you, but I've not used I18n in sf before, so it may well be that there is a more elegant solution built in?