def cached_idol(self): if not self._cache_idol_last_update or self._cache_idol_last_update < timezone.now( ) - datetime.timedelta(days=self._cache_idol_days): self.force_cache_idol() return AttrDict({ 'pk': self.idol_id, 'id': self.idol_id, 'name': self._cache_idol_name, 'japanese_name': self._cache_idol_japanese_name, 'i_type': self._cache_idol_i_type, 'type': TYPE_DICT[self._cache_idol_i_type] if self._cache_idol_i_type is not None else None, 'english_type': ENGLISH_TYPE_DICT[self._cache_idol_i_type] if self._cache_idol_i_type is not None else None, 'image': self._cache_idol_image, 'image_url': get_image_url(self._cache_idol_image), 'http_image_url': get_http_image_url(self._cache_idol_image), 'item_url': u'/idol/{}/{}/'.format(self.idol_id, tourldash(self._cache_idol_name)), 'ajax_item_url': u'/ajax/idol/{}/'.format(self.idol_id), })
def cached_event(self): if not self.event_id: return None if not self._cache_event_last_update or self._cache_event_last_update < timezone.now( ) - datetime.timedelta(days=self._cache_event_days): self.force_cache_event() return AttrDict({ 'id': self.event_id, 'pk': self.event_id, 'name': self._cache_event_name, 'translated_name': self._cache_event_translated_name, 'image': self._cache_event_image, 'image_url': get_image_url(self._cache_event_image), 'item': u'/event/{}/{}/'.format( self.event_id, tourldash( self._cache_event_translated_name if self. _cache_event_translated_name else self._cache_event_name)), 'ajax_item_url': u'/ajax/event/{}/'.format(self.event_id), })
def cached_json_extra(self, field_name, d): # Get original model class for cached thing try: original_cls = self._meta.get_field(field_name).rel.to except FieldDoesNotExist: original_cls = None original_cls = getattr(self, u'_cache_{}_fk_class'.format(field_name), original_cls) if callable(original_cls): original_cls = original_cls() # Call pre if provided if hasattr(self, u'cached_{}_pre'.format(field_name)): getattr(self, u'cached_{}_pre'.format(field_name))(d) # Add default unicode if missing if 'unicode' not in d: d['unicode'] = d['name'] if 'name' in d else (d['id'] if 'id' in d else '?') # TODO try to add a way to call __unicode__ smooth if 'id' in d: if 'pk' not in d: d['pk'] = d['id'] # Set collection item URLs collection_name = getattr(self, u'_cached_{}_collection_name'.format(field_name), field_name) if 'item_url' not in d: d['item_url'] = u'/{}/{}/{}/'.format(collection_name, d['id'], tourldash(d['unicode'])) if 'ajax_item_url' not in d: d['ajax_item_url'] = u'/ajax/{}/{}/'.format(collection_name, d['id']) if 'full_item_url' not in d: d['full_item_url'] = u'{}{}/{}/{}/'.format(django_settings.SITE_URL, collection_name, d['id'], tourldash(d['unicode'])) if 'http_item_url' not in d: d['http_item_url'] = u'https:{}'.format(d['full_item_url']) if 'http' not in d['full_item_url'] else d['full_item_url'] # Set image url helpers if 'image' in d: if 'image_url' not in d: d['image_url'] = get_image_url_from_path(d['image']) if 'http_image_url' not in d: d['http_image_url'] = get_http_image_url_from_path(d['image']) if original_cls: for k in d.keys(): # i_ fields if k.startswith('i_'): d[k[2:]] = original_cls.get_reverse_i(k[2:], d[k]) d['t_{}'.format(k[2:])] = original_cls.get_verbose_i(k[2:], d[k]) # Translated fields language = get_language() for k, v in d.items(): if isinstance(d.get(u'{}s'.format(k), None), dict): if language == 'en': d['t_{}'.format(k)] = v else: d['t_{}'.format(k)] = d[u'{}s'.format(k)].get(language, v) # Call extra if provided if hasattr(self, u'cached_{}_extra'.format(field_name)): getattr(self, u'cached_{}_extra'.format(field_name))(d) return d
def cached_json_extra(self, field_name, d): # Get original model class for cached thing try: original_cls = self._meta.get_field(field_name).rel.to except FieldDoesNotExist: original_cls = None original_cls = getattr(self, u'_cache_{}_fk_class'.format(field_name), original_cls) if callable(original_cls): original_cls = original_cls() # Call pre if provided if hasattr(self, u'cached_{}_pre'.format(field_name)): getattr(self, u'cached_{}_pre'.format(field_name))(d) # Add default unicode if missing if 'unicode' not in d: d['unicode'] = d['name'] if 'name' in d else (unicode(d['id']) if 'id' in d else '?') if 'id' in d: if 'pk' not in d: d['pk'] = d['id'] # Set collection item URLs collection_name = getattr(self, u'_cached_{}_collection_name'.format(field_name), field_name) if 'item_url' not in d: d['item_url'] = u'/{}/{}/{}/'.format(collection_name, d['id'], tourldash(d['unicode'])) if 'ajax_item_url' not in d: d['ajax_item_url'] = u'/ajax/{}/{}/'.format(collection_name, d['id']) if 'full_item_url' not in d: d['full_item_url'] = u'{}{}/{}/{}/'.format(django_settings.SITE_URL, collection_name, d['id'], tourldash(d['unicode'])) if 'http_item_url' not in d: d['http_item_url'] = u'https:{}'.format(d['full_item_url']) if 'http' not in d['full_item_url'] else d['full_item_url'] # Set image url helpers for image_field in getattr(self, u'_cache_{}_images'.format(field_name), []) + ['image']: if image_field in d: if u'{}_url'.format(image_field) not in d: d[u'{}_url'.format(image_field)] = get_image_url_from_path(d[image_field]) if u'http_{}_url'.format(image_field) not in d: d[u'http_{}_url'.format(image_field)] = get_http_image_url_from_path(d[image_field]) if original_cls: for k in d.keys(): # i_ fields if k.startswith('i_'): d[k[2:]] = original_cls.get_reverse_i(k[2:], d[k]) d['t_{}'.format(k[2:])] = original_cls.get_verbose_i(k[2:], d[k]) # Translated fields language = get_language() for k, v in d.items(): if isinstance(d.get(u'{}s'.format(k), None), dict): if language == 'en': d['t_{}'.format(k)] = v else: d['t_{}'.format(k)] = d[u'{}s'.format(k)].get(language, v) # Call extra if provided if hasattr(self, u'cached_{}_extra'.format(field_name)): getattr(self, u'cached_{}_extra'.format(field_name))(d) return d
def __call__(self, instance, filename): _, extension = os.path.splitext(filename) if not extension: extension = '.png' return u'{static_uploaded_files_prefix}{prefix}/{id}{string}{extension}'.format( static_uploaded_files_prefix=django_settings. STATIC_UPLOADED_FILES_PREFIX, prefix=self.prefix, id=instance.id if instance.id else randomString(6), string=tourldash(unicode(instance)), extension=extension, )
def cached_center(self): if not self.center_id: return None if not self._cache_center_last_update or self._cache_center_last_update < timezone.now( ) - datetime.timedelta(days=self._cache_center_days): self.force_cache_center() return AttrDict({ 'pk': self.center_id, 'id': self.center_id, 'card_id': self._cache_center_card_id, 'awakened': self._cache_center_awakened, 'card': AttrDict({ 'id': self._cache_center_card_id, 'pk': self._cache_center_card_id, 'i_type': self._cache_center_card_i_type, 'type': TYPE_DICT[self._cache_center_card_i_type] if self._cache_center_card_i_type is not None else None, 'english_type': ENGLISH_TYPE_DICT[self._cache_center_card_i_type] if self._cache_center_card_i_type is not None else None, 'icon': self._cache_center_card_icon, 'icon_url': get_image_url(self._cache_center_card_icon), 'art': self._cache_center_card_art, 'art_url': get_image_url(self._cache_center_card_art), 'transparent': self._cache_center_card_transparent, 'transparent_url': get_image_url(self._cache_center_card_transparent), 'string': self._cache_center_card_string, 'item_url': u'/card/{}/{}/'.format( self._cache_center_card_id, tourldash(self._cache_center_card_string)), 'ajax_item_url': u'/ajax/card/{}/'.format(self._cache_center_card_id), }), })
def cached_json_extra(self, field_name, d): # Get original model class for cached thing try: original_cls = self._meta.get_field(field_name).rel.to except FieldDoesNotExist: original_cls = None original_cls = getattr(self, u'_cache_{}_fk_class'.format(field_name), original_cls) if callable(original_cls): original_cls = original_cls() # Call pre if provided if hasattr(self, u'cached_{}_pre'.format(field_name)): getattr(self, u'cached_{}_pre'.format(field_name))(d) # Add default unicode if missing if 'unicode' not in d: d['unicode'] = d['name'] if 'name' in d else (d['id'] if 'id' in d else '?') # TODO try to add a way to call __unicode__ smooth if 'id' in d: if 'pk' not in d: d['pk'] = d['id'] # Set collection item URLs collection_name = getattr(self, u'_cached_{}_collection_name'.format(field_name), field_name) if 'item_url' not in d: d['item_url'] = u'/{}/{}/{}/'.format(collection_name, d['id'], tourldash(d['unicode'])) if 'ajax_item_url' not in d: d['ajax_item_url'] = u'/ajax/{}/{}/'.format(collection_name, d['id']) if 'full_item_url' not in d: d['full_item_url'] = u'{}{}/{}/{}/'.format(django_settings.SITE_URL, collection_name, d['id'], tourldash(d['unicode'])) if 'http_item_url' not in d: d['http_item_url'] = u'https:{}'.format(d['full_item_url']) if 'http' not in d['full_item_url'] else d['full_item_url'] # Set image url helpers if 'image' in d: if 'image_url' not in d: d['image_url'] = get_image_url_from_path(d['image']) if 'http_image_url' not in d: d['http_image_url'] = get_http_image_url_from_path(d['image']) if original_cls: for k in d.keys(): # i_ fields if k.startswith('i_'): d[k[2:]] = original_cls.get_reverse_i(k[2:], d[k]) d['t_{}'.format(k[2:])] = original_cls.get_verbose_i(k[2:], d[k]) # Call extra if provided if hasattr(self, u'cached_{}_extra'.format(field_name)): getattr(self, u'cached_{}_extra'.format(field_name))(d) return d
def likeactivity(request, pk): context = ajaxContext(request) if not request.user.is_authenticated() or request.method != 'POST': raise PermissionDenied() activity = get_object_or_404(models.Activity.objects.extra( select={ 'liked': 'SELECT COUNT(*) FROM magi_activity_likes WHERE activity_id = magi_activity.id AND user_id={}' .format(request.user.id), }).annotate(total_likes=Count('likes')).select_related( 'owner', 'owner__preferences'), pk=pk) # If the owner of the liked activity blocked the authenticated user if activity.cached_owner.id in request.user.preferences.cached_blocked_by_ids: raise PermissionDenied() if activity.cached_owner.username == request.user.username: raise PermissionDenied() if 'like' in request.POST and not activity.liked: activity.likes.add(request.user) activity.update_cache('total_likes') activity.save() pushNotification( activity.owner, 'like', [unicode(request.user), unicode(activity)], url_values=[str(activity.id), tourldash(unicode(activity))], image=activity.image) return JsonResponse({ 'total_likes': activity.total_likes + 2, 'result': 'liked', }) if 'unlike' in request.POST and activity.liked: activity.likes.remove(request.user) activity.update_cache('total_likes') activity.save() return JsonResponse({ 'total_likes': activity.total_likes, 'result': 'unliked', }) return JsonResponse({ 'total_likes': activity.total_likes + 1, })
def get_full_item_url(instance): return u'{}{}/{}/{}/'.format(django_settings.SITE_URL, instance.collection_name, instance.pk, tourldash(unicode(instance)))
def get_item_url(instance): return u'/{}/{}/{}/'.format(instance.collection_name, instance.pk, tourldash(unicode(instance)))
if not r.user.preferences.age else u'') if r.user.is_authenticated() and r.user.preferences.age < 18 else True, }), ] ############################################################ # User preferences and profiles CUSTOM_PREFERENCES_FORM = True EXTRA_PREFERENCES = DEFAULT_EXTRA_PREFERENCES + [ ('i_favorite_band', lambda: _('Favorite {thing}').format(thing=_('Band'))), ] FAVORITE_CHARACTER_TO_URL = lambda link: '/member/{pk}/{name}/'.format( pk=link.raw_value, name=tourldash(link.value)) FAVORITE_CHARACTER_NAME = _('Member') USER_COLORS = [ ('power', _('Power'), 'Power', '#FF2D54'), ('cool', _('Cool'), 'Cool', '#4057E3'), ('pure', _('Pure'), 'Pure', '#44C527'), ('happy', _('Happy'), 'Happy', '#FF8400'), ] ACCOUNT_TAB_ORDERING = [ 'about', 'collectiblecard', 'eventparticipation', 'playedsong', 'item', 'areaitem' ] ############################################################
if hasattr(django_settings, 'FAVORITE_CHARACTERS'): FAVORITE_CHARACTERS = getattr(django_settings, 'FAVORITE_CHARACTERS') else: FAVORITE_CHARACTERS = None if hasattr(settings_module, 'FAVORITE_CHARACTERS_MODEL'): # Used by generated settings FAVORITE_CHARACTERS_MODEL = getattr(settings_module, 'FAVORITE_CHARACTERS_MODEL') FAVORITE_CHARACTERS_FILTER = getattr(settings_module, 'FAVORITE_CHARACTERS_FILTER', lambda _q: _q) FAVORITE_CHARACTER_TO_URL = lambda _link: u'/{}/{}/{}/'.format( FAVORITE_CHARACTERS_MODEL.collection_name, _link.raw_value, tourldash(_link.value), ) FAVORITE_CHARACTER_NAME = lambda: getMagiCollection( FAVORITE_CHARACTERS_MODEL.collection_name).title else: FAVORITE_CHARACTERS_MODEL = None FAVORITE_CHARACTERS_FILTER = lambda _q: _q FAVORITE_CHARACTER_TO_URL = lambda _: '#' FAVORITE_CHARACTER_NAME = None if hasattr(settings_module, 'FAVORITE_CHARACTER_TO_URL'): FAVORITE_CHARACTER_TO_URL = getattr(settings_module, 'FAVORITE_CHARACTER_TO_URL') if hasattr(settings_module, 'FAVORITE_CHARACTER_NAME'): FAVORITE_CHARACTER_NAME = getattr(settings_module, 'FAVORITE_CHARACTER_NAME')
'auth': page.get('navbar_link_auth', (True, True)), 'get_url': None if not page.get('url_variables', None) else (getURLLambda(name, lambdas)), 'show_link_callback': getPageShowLinkLambda(page), 'divider_before': page.get('divider_before', False), 'divider_after': page.get('divider_after', False), } navbarAddLink(name, link, page.get('navbar_link_list', None)) ############################################################ # Add staff links to navbar for permission, details in GLOBAL_OUTSIDE_PERMISSIONS.items(): if not isinstance(details, dict): details = { 'url': url } if details.get('url', None): url_name = u'staff-global-{}'.format(tourldash(permission).lower()) navbarAddLink(url_name, { 'url_name': url_name, 'url': details['url'], 'image': details.get('image', None), 'icon': details.get('icon', None), 'title': permission, 'show_link_callback': lambda context: context['request'].user.is_staff, 'new_tab': True, }, 'staff') def _getPageShowLinkForGroupsLambda(group): def _show_link_callback(context): return context['request'].user.is_authenticated() and context['request'].user.hasGroup(group) return _show_link_callback
def get_item_url(instance): return u'/{}/{}/{}/'.format(instance.collection_name, instance.pk, tourldash(unicode(instance)))
def getSchoolURLFromPk(pk, ajax=False): school = django_settings.SCHOOLS[int(pk)] return u'{}/school/{}/{}'.format( u'/ajax' if ajax else u'', pk, u'' if ajax else u'{}/'.format(tourldash(getSchoolNameFromPk(pk))))
def get_full_item_url(instance): return u'{}{}/{}/{}/'.format(django_settings.SITE_URL, instance.collection_name, instance.pk, tourldash(unicode(instance)))
'image': page.get('image', None), 'new_tab': page.get('new_tab', False), 'auth': page.get('navbar_link_auth', (True, True)), 'get_url': None if not page.get('url_variables', None) else (getURLLambda(name, lambdas)), 'show_link_callback': getPageShowLinkLambda(page), 'divider_before': page.get('divider_before', False), 'divider_after': page.get('divider_after', False), } navbarAddLink(name, link, page.get('navbar_link_list', None)) ############################################################ # Add staff links to navbar for permission, url in GLOBAL_OUTSIDE_PERMISSIONS.items(): if url: url_name = u'staff-global-{}'.format(tourldash(permission).lower()) navbarAddLink(url_name, { 'url_name': url_name, 'url': url, 'title': permission, 'show_link_callback': lambda context: context['request'].user.is_staff, 'new_tab': True, }, 'staff') def _getPageShowLinkForGroupsLambda(group): def _show_link_callback(context): return context['request'].user.is_authenticated() and context['request'].user.hasGroup(group) return _show_link_callback _groups_dict = dict(GROUPS) _links_for_groups = {}