1072 select.add_sort_var(mdvar, asc=False) |
1072 select.add_sort_var(mdvar, asc=False) |
1073 return select.as_string() |
1073 return select.as_string() |
1074 |
1074 |
1075 # generic vocabulary methods ############################################## |
1075 # generic vocabulary methods ############################################## |
1076 |
1076 |
1077 def cw_unrelated_rql(self, rtype, targettype, role, ordermethod=None, |
1077 def cw_linkable_rql(self, rtype, targettype, role, ordermethod=None, |
1078 vocabconstraints=True, lt_infos={}, limit=None): |
1078 vocabconstraints=True, lt_infos={}, limit=None): |
1079 """build a rql to fetch `targettype` entities unrelated to this entity |
1079 """build a rql to fetch targettype entities either related or unrelated |
1080 using (rtype, role) relation. |
1080 to this entity using (rtype, role) relation. |
1081 |
1081 |
1082 Consider relation permissions so that returned entities may be actually |
1082 Consider relation permissions so that returned entities may be actually |
1083 linked by `rtype`. |
1083 linked by `rtype`. |
1084 |
1084 |
1085 `lt_infos` are supplementary informations, usually coming from __linkto |
1085 `lt_infos` are supplementary informations, usually coming from __linkto |
1086 parameter, that can help further restricting the results in case current |
1086 parameter, that can help further restricting the results in case current |
1087 entity is not yet created. It is a dict describing entities the current |
1087 entity is not yet created. It is a dict describing entities the current |
1088 entity will be linked to, which keys are (rtype, role) tuples and values |
1088 entity will be linked to, which keys are (rtype, role) tuples and values |
1089 are a list of eids. |
1089 are a list of eids. |
|
1090 """ |
|
1091 return self._cw_compute_linkable_rql(rtype, targettype, role, ordermethod=None, |
|
1092 vocabconstraints=vocabconstraints, |
|
1093 lt_infos=lt_infos, limit=limit, |
|
1094 unrelated_only=False) |
|
1095 |
|
1096 def cw_unrelated_rql(self, rtype, targettype, role, ordermethod=None, |
|
1097 vocabconstraints=True, lt_infos={}, limit=None): |
|
1098 """build a rql to fetch `targettype` entities unrelated to this entity |
|
1099 using (rtype, role) relation. |
|
1100 |
|
1101 Consider relation permissions so that returned entities may be actually |
|
1102 linked by `rtype`. |
|
1103 |
|
1104 `lt_infos` are supplementary informations, usually coming from __linkto |
|
1105 parameter, that can help further restricting the results in case current |
|
1106 entity is not yet created. It is a dict describing entities the current |
|
1107 entity will be linked to, which keys are (rtype, role) tuples and values |
|
1108 are a list of eids. |
|
1109 """ |
|
1110 return self._cw_compute_linkable_rql(rtype, targettype, role, ordermethod=None, |
|
1111 vocabconstraints=vocabconstraints, |
|
1112 lt_infos=lt_infos, limit=limit, |
|
1113 unrelated_only=True) |
|
1114 |
|
1115 def _cw_compute_linkable_rql(self, rtype, targettype, role, ordermethod=None, |
|
1116 vocabconstraints=True, lt_infos={}, limit=None, |
|
1117 unrelated_only=False): |
|
1118 """build a rql to fetch `targettype` entities that may be related to |
|
1119 this entity using the (rtype, role) relation. |
|
1120 |
|
1121 By default (unrelated_only=False), this includes the already linked |
|
1122 entities as well as the unrelated ones. If `unrelated_only` is True, the |
|
1123 rql filters out the already related entities. |
1090 """ |
1124 """ |
1091 ordermethod = ordermethod or 'fetch_unrelated_order' |
1125 ordermethod = ordermethod or 'fetch_unrelated_order' |
1092 rschema = self._cw.vreg.schema.rschema(rtype) |
1126 rschema = self._cw.vreg.schema.rschema(rtype) |
1093 rdef = rschema.role_rdef(self.e_schema, targettype, role) |
1127 rdef = rschema.role_rdef(self.e_schema, targettype, role) |
1094 rewriter = RQLRewriter(self._cw) |
1128 rewriter = RQLRewriter(self._cw) |
1114 if role == 'subject': |
1148 if role == 'subject': |
1115 rel = make_relation(variable, rtype, (searchedvar,), VariableRef) |
1149 rel = make_relation(variable, rtype, (searchedvar,), VariableRef) |
1116 else: |
1150 else: |
1117 rel = make_relation(searchedvar, rtype, (variable,), VariableRef) |
1151 rel = make_relation(searchedvar, rtype, (variable,), VariableRef) |
1118 select.add_restriction(Not(rel)) |
1152 select.add_restriction(Not(rel)) |
1119 elif self.has_eid(): |
1153 elif self.has_eid() and unrelated_only: |
1120 # elif we have an eid, we don't want a target entity which is |
1154 # elif we have an eid, we don't want a target entity which is |
1121 # already linked to ourself through this relation |
1155 # already linked to ourself through this relation |
1122 rel = make_relation(subjvar, rtype, (objvar,), VariableRef) |
1156 rel = make_relation(subjvar, rtype, (objvar,), VariableRef) |
1123 select.add_restriction(Not(rel)) |
1157 select.add_restriction(Not(rel)) |
1124 if self.has_eid(): |
1158 if self.has_eid(): |