975 |
975 |
def get_repeat_template(self, context):
|
976 |
976 |
return len(context[self.global_context_key])
|
977 |
977 |
|
|
978 |
def get_card_data(self, card_slug, card_id, context, synchronous=False):
|
|
979 |
api_url = '/api/cards/%s/%s/?include-files-content=off' % (card_slug, card_id)
|
|
980 |
if not synchronous:
|
|
981 |
synchronous = bool(context.get('synchronous'))
|
|
982 |
wcs_site = get_wcs_services().get(self.wcs_site)
|
|
983 |
try:
|
|
984 |
response = requests.get(
|
|
985 |
api_url,
|
|
986 |
remote_service=wcs_site,
|
|
987 |
user=None if self.without_user else getattr(context.get('request'), 'user', None),
|
|
988 |
without_user=self.without_user,
|
|
989 |
cache_duration=5,
|
|
990 |
raise_if_not_cached=not synchronous,
|
|
991 |
log_errors=False,
|
|
992 |
)
|
|
993 |
response.raise_for_status()
|
|
994 |
except RequestException:
|
|
995 |
return {}
|
|
996 |
|
|
997 |
if response.status_code == 200:
|
|
998 |
return response.json()
|
|
999 |
return {}
|
|
1000 |
|
978 |
1001 |
def get_card_schema(self, card_slug):
|
979 |
1002 |
wcs_site = get_wcs_services().get(self.wcs_site)
|
980 |
1003 |
card_schema = get_wcs_json(wcs_site, 'api/cards/%s/@schema' % card_slug, log_errors='warn')
|
... | ... | |
1040 |
1063 |
)
|
1041 |
1064 |
return results
|
1042 |
1065 |
|
|
1066 |
def get_card_ids_from_template(self, template, original_context, request):
|
|
1067 |
try:
|
|
1068 |
context = RequestContext(request)
|
|
1069 |
context.push(original_context)
|
|
1070 |
ids = Template(template).render(context).split(',')
|
|
1071 |
return [i.strip() for i in ids if i.strip()]
|
|
1072 |
except (VariableDoesNotExist, TemplateSyntaxError):
|
|
1073 |
return []
|
|
1074 |
|
1043 |
1075 |
def get_card_ids(self, original_context, request):
|
1044 |
1076 |
if self.card_ids:
|
1045 |
|
try:
|
1046 |
|
context = RequestContext(request)
|
1047 |
|
context.push(original_context)
|
1048 |
|
ids = Template(self.card_ids).render(context).split(',')
|
1049 |
|
return [i.strip() for i in ids if i.strip()]
|
1050 |
|
except (VariableDoesNotExist, TemplateSyntaxError):
|
1051 |
|
return []
|
|
1077 |
# card ids template is defined
|
|
1078 |
return self.get_card_ids_from_template(self.card_ids, original_context, request)
|
1052 |
1079 |
|
1053 |
|
card_slug = self.carddef_reference.split(':')[1]
|
1054 |
|
card_id = '%s_id' % card_slug
|
|
1080 |
# get card id from page's sub slug (check in context)
|
|
1081 |
card_id = '%s_id' % self.card_slug
|
1055 |
1082 |
if original_context.get(card_id):
|
1056 |
1083 |
return [original_context[card_id]]
|
|
1084 |
|
|
1085 |
# nothing found in context
|
1057 |
1086 |
return []
|
1058 |
1087 |
|
1059 |
1088 |
def get_card_id(self, context):
|
... | ... | |
1085 |
1114 |
if self.card_ids: # template defined, but result is empty or None
|
1086 |
1115 |
extra_context['card_not_found'] = True
|
1087 |
1116 |
return extra_context
|
1088 |
|
card_slug = self.carddef_reference.split(':')[1]
|
1089 |
|
api_url = '/api/cards/%s/%s/?include-files-content=off' % (card_slug, card_id)
|
1090 |
|
|
1091 |
|
wcs_site = get_wcs_services().get(self.wcs_site)
|
1092 |
1117 |
|
1093 |
|
try:
|
1094 |
|
response = requests.get(
|
1095 |
|
api_url,
|
1096 |
|
remote_service=wcs_site,
|
1097 |
|
user=None if self.without_user else getattr(context.get('request'), 'user', None),
|
1098 |
|
without_user=self.without_user,
|
1099 |
|
cache_duration=5,
|
1100 |
|
raise_if_not_cached=not (context.get('synchronous')),
|
1101 |
|
log_errors=False,
|
1102 |
|
)
|
1103 |
|
response.raise_for_status()
|
1104 |
|
except RequestException:
|
|
1118 |
card_data = self.get_card_data(self.card_slug, card_id, context)
|
|
1119 |
if not card_data:
|
1105 |
1120 |
return extra_context
|
1106 |
1121 |
|
1107 |
|
if response.status_code == 200:
|
1108 |
|
extra_context['card'] = response.json()
|
1109 |
|
context = Context(extra_context)
|
1110 |
|
if self.title_type == 'manual':
|
|
1122 |
extra_context['card'] = card_data
|
|
1123 |
context = Context(extra_context)
|
|
1124 |
if self.title_type == 'manual':
|
|
1125 |
try:
|
|
1126 |
extra_context['title'] = Template(self.custom_title).render(context)
|
|
1127 |
except (VariableDoesNotExist, TemplateSyntaxError):
|
|
1128 |
extra_context['title'] = ''
|
|
1129 |
# auto title or custom_title gives an empty title, use default value + card text
|
|
1130 |
if self.title_type == 'auto' or self.title_type == 'manual' and not extra_context['title']:
|
|
1131 |
extra_context['title'] = '%s - %s' % (self.cached_title, extra_context['card'].get('text'))
|
|
1132 |
|
|
1133 |
extra_context['card']['custom_fields'] = {}
|
|
1134 |
if self.custom_schema:
|
|
1135 |
for item in self.custom_schema.get('cells') or []:
|
|
1136 |
if item.get('varname') != '@custom@':
|
|
1137 |
continue
|
|
1138 |
if not item.get('template'):
|
|
1139 |
continue
|
1111 |
1140 |
try:
|
1112 |
|
extra_context['title'] = Template(self.custom_title).render(context)
|
|
1141 |
extra_context['card']['custom_fields'][item['template']] = Template(
|
|
1142 |
item['template']
|
|
1143 |
).render(context)
|
1113 |
1144 |
except (VariableDoesNotExist, TemplateSyntaxError):
|
1114 |
|
extra_context['title'] = ''
|
1115 |
|
# auto title or custom_title gives an empty title, use default value + card text
|
1116 |
|
if self.title_type == 'auto' or self.title_type == 'manual' and not extra_context['title']:
|
1117 |
|
extra_context['title'] = '%s - %s' % (self.cached_title, extra_context['card'].get('text'))
|
1118 |
|
|
1119 |
|
extra_context['card']['custom_fields'] = {}
|
1120 |
|
if self.custom_schema:
|
1121 |
|
for item in self.custom_schema.get('cells') or []:
|
1122 |
|
if item.get('varname') != '@custom@':
|
1123 |
|
continue
|
1124 |
|
if not item.get('template'):
|
1125 |
|
continue
|
1126 |
|
try:
|
1127 |
|
extra_context['card']['custom_fields'][item['template']] = Template(
|
1128 |
|
item['template']
|
1129 |
|
).render(context)
|
1130 |
|
except (VariableDoesNotExist, TemplateSyntaxError):
|
1131 |
|
pass
|
1132 |
|
|
|
1145 |
pass
|
1133 |
1146 |
return extra_context
|
1134 |
1147 |
|
1135 |
1148 |
def get_default_form_class(self):
|
1136 |
|
-
|