def text_view_comments(request, key, version_key=None, adminkey=None): text = get_text_by_keys_or_404(key) read_only = False if version_key : text_version = get_textversion_by_keys_or_404(version_key, adminkey, key) if settings.ALLOW_CLIENT_MODIF_ON_LAST_VERSION_ONLY : read_only = (text.last_text_version_id != text_version.id) else : text_version = text.get_latest_version() comments = get_viewable_comments(request, text_version.comment_set.filter(reply_to__isnull=True),text) filter_datas = get_filter_datas(request, text_version, text) get_params = simplejson.dumps(request.GET) wrapped_text_version, _ , _ = spannify(text_version.get_content()) from cm.models import ApplicationConfiguration categories = {} for i in range(1, 6): if text_version.__dict__['category_' + str(i)] == None or text_version.__dict__['category_' + str(i)].lower() != 'none': if text_version.__dict__['category_' + str(i)] != None and text_version.__dict__['category_' + str(i)] != '': categories[i] = text_version.__dict__['category_' + str(i)] else: if ApplicationConfiguration.get_key('workspace_category_' + str(i)) != None and ApplicationConfiguration.get_key('workspace_category_' + str(i)) != '': categories[i] = ApplicationConfiguration.get_key('workspace_category_' + str(i)) template_dict = { 'text' : text, 'text_version' : text_version, 'title' : text_version.title, # TODO use it ... 'get_params' : get_params, 'content' : wrapped_text_version, 'client_date_fmt' : settings.CLIENT_DATE_FMT, 'read_only' : read_only, } template_dict['json_comments'] = jsonize(comments, request) template_dict['json_filter_datas'] = jsonize(filter_datas, request) if categories: categories[0] = 'none' template_dict['categories'] = jsonize(categories, request) custom_css_str = ApplicationConfiguration.get_key('custom_css') if custom_css_str: custom_css = cssutils.parseString(custom_css_str) for css_rule in custom_css: if css_rule.type == css_rule.STYLE_RULE and css_rule.wellformed: css_rule.selectorText = "#textcontainer %s" %css_rule.selectorText template_dict['custom_css'] = custom_css.cssText template_dict['custom_font'] = ApplicationConfiguration.get_key('custom_font') template_dict['custom_titles_font'] = ApplicationConfiguration.get_key('custom_titles_font') return render_to_response('site/text_view_comments.html', template_dict, context_instance=RequestContext(request))
def register(request): if request.method == 'POST': userform = UserForm(request.POST) userprofileaddform = UserProfileRegisterForm(request.POST) if userform.is_valid() and userprofileaddform.is_valid(): data = userform.cleaned_data data.update(userprofileaddform.cleaned_data) user = UserProfile.objects.create_inactive_user( userform.cleaned_data['email'], False, **userprofileaddform.cleaned_data) profile = user.get_profile() if ApplicationConfiguration.get_key( 'workspace_registration_moderation', False): # need moderation profile.is_suspended = True profile.save() display_message( request, _(u"You've been registered, you will receive a confirmation mail once a moderator has approved your membership." )) else: profile.send_activation_email() display_message( request, _(u"You've been registered, please check your email for the confirm message." )) return HttpResponseRedirect(reverse('index')) else: userform = UserForm() userprofileaddform = UserProfileRegisterForm() return render_to_response('site/register.html', {'forms': [userform, userprofileaddform]}, context_instance=RequestContext(request))
def settings_design(request): if request.method == 'POST': if 'delete_logo' in request.POST: Configuration.objects.del_key('workspace_logo_file_key') display_message(request, _(u'Settings saved')) return HttpResponseRedirect(reverse('settings-design')) else: form = SettingsDesignForm(data=request.POST) if form.is_valid(): form.save() logo_file = request.FILES.get('workspace_logo_file', None) if logo_file: form.save_file(logo_file) display_message(request, _(u'Settings saved')) return HttpResponseRedirect(reverse('settings-design')) else: from cm.models import ApplicationConfiguration custom_css = ApplicationConfiguration.get_key('custom_css') if custom_css: default_css = custom_css else: default_css = ''' .voted { color: #008000; } .rejected, .fallen, .withdrawn { color: #ff0000; } div.frame { border: 1px solid #000; padding: 5px; } div.frame .title { font-weight: bold; text-align: center; }''' form = SettingsDesignForm(initial={'custom_css': default_css}) return render_to_response('site/settings_design.html', {'form': form}, context_instance=RequestContext(request))
def settings_design(request): if request.method == 'POST': if 'delete_logo' in request.POST: Configuration.objects.del_key('workspace_logo_file_key') display_message(request, _(u'Settings saved')) return HttpResponseRedirect(reverse('settings-design')) else: form = SettingsDesignForm(data=request.POST) if form.is_valid() : form.save() logo_file = request.FILES.get('workspace_logo_file',None) if logo_file: form.save_file(logo_file) display_message(request, _(u'Settings saved')) return HttpResponseRedirect(reverse('settings-design')) else: from cm.models import ApplicationConfiguration custom_css = ApplicationConfiguration.get_key('custom_css') if custom_css: default_css = custom_css else: default_css = ''' .voted { color: #008000; } .rejected, .fallen, .withdrawn { color: #ff0000; } div.frame { border: 1px solid #000; padding: 5px; } div.frame .title { font-weight: bold; text-align: center; }''' form = SettingsDesignForm(initial={'custom_css': default_css}) return render_to_response('site/settings_design.html', {'form' : form}, context_instance=RequestContext(request))
def register(request): if request.method == 'POST': userform = UserForm(request.POST) userprofileaddform = UserProfileRegisterForm(request.POST) if userform.is_valid() and userprofileaddform.is_valid(): data = userform.cleaned_data data.update(userprofileaddform.cleaned_data) user = UserProfile.objects.create_inactive_user(userform.cleaned_data['email'], False, **userprofileaddform.cleaned_data) profile = user.get_profile() if ApplicationConfiguration.get_key('workspace_registration_moderation', False): # need moderation profile.is_suspended = True profile.save() display_message(request, _(u"You've been registered, you will receive a confirmation mail once a moderator has approved your membership.")) else: profile.send_activation_email() display_message(request, _(u"You've been registered, please check your email for the confirm message.")) return HttpResponseRedirect(reverse('index')) else: userform = UserForm() userprofileaddform = UserProfileRegisterForm() return render_to_response('site/register.html', {'forms':[userform, userprofileaddform]}, context_instance=RequestContext(request))
def get_viewable_comments(request, comments, text, order_by=('created', )): """ Get comments visibles by user comments: queryset """ user = get_request_user(request) try: myself = request.GET.get('name', None) except AttributeError: myself = None key = sha1( str((settings.SITE_URL, 'get_viewable_comments', (user, myself, text, comments)))).hexdigest() val = cache.get(key) if val != None: return val if user and has_perm(request, 'can_view_unapproved_comment', text=text): ret = list(comments.order_by(*order_by)) cache.set(key, ret) return ret else: # Fetch role_model to process specific behaviour for role_teacher model from cm.models import ApplicationConfiguration role_model = ApplicationConfiguration.get_key('workspace_role_model') if has_perm(request, 'can_view_approved_comment', text=text): visible_comments = comments.filter(state='approved').order_by( *order_by) # filter comments with a non visible (i.e. moderated) comment in the above thread comments_thread_viewable = [ c for c in visible_comments if c.is_thread_full_visible() ] # for role_teacher role model, do not show 'individual student' comments if (role_model == 'teacher'): unfiltered_comments = list(comments_thread_viewable) for c in unfiltered_comments: if c.user_id and c.user_id != 1: try: userrole = UserRole.objects.get(user=c.user, text=text) except: userrole = UserRole.objects.get(user=None, text=None) if userrole.role_id == None: role = c.user.get_profile().global_userrole().role else: role = userrole.role if role.name == 'Individual student': comments_thread_viewable.remove(c) cache.set(key, comments_thread_viewable) return comments_thread_viewable elif user and has_perm(request, 'can_view_comment_own', text=text): if DECORATED_CREATORS: visible_comments = comments.filter(name=myself).order_by( *order_by) else: visible_comments = comments.filter(user=user).order_by( *order_by) # for role_teacher role model, add 'teacher' comments if (role_model == 'teacher'): with_teachers = [] for u in list( User.objects.filter(userrole__role__name='Teacher')): with_teachers.append(u.id) # add admin and current user admin = User.objects.get(id=1) with_teachers.append(admin.id) if DECORATED_CREATORS: visible_comments = comments.filter( Q(user__id__in=with_teachers) | Q(name=myself)).order_by(*order_by) else: with_teachers.append(user.id) visible_comments = comments.filter( user__id__in=with_teachers).order_by(*order_by) # filter comments with a non visible (i.e. moderated) comment in the above thread comments_thread_viewable = [ c for c in visible_comments if c.is_thread_full_visible(own_user=user) ] cache.set(key, comments_thread_viewable) return comments_thread_viewable else: cache.set(key, []) return []
def get_viewable_comments(request, comments, text, order_by=('created',)): """ Get comments visibles by user comments: queryset """ user = get_request_user(request) try: myself = request.GET.get('name', None) except AttributeError: myself = None key = sha1(str((settings.SITE_URL, 'get_viewable_comments', (user, myself, text, comments)))).hexdigest() val = cache.get(key) if val != None: return val if user and has_perm(request, 'can_view_unapproved_comment', text=text): ret = list(comments.order_by(*order_by)) cache.set(key, ret) return ret else: # Fetch role_model to process specific behaviour for role_teacher model from cm.models import ApplicationConfiguration role_model = ApplicationConfiguration.get_key('workspace_role_model') if has_perm(request, 'can_view_approved_comment', text=text): visible_comments = comments.filter(state = 'approved').order_by(*order_by) # filter comments with a non visible (i.e. moderated) comment in the above thread comments_thread_viewable = [c for c in visible_comments if c.is_thread_full_visible()] # for role_teacher role model, do not show 'individual student' comments if (role_model == 'teacher'): unfiltered_comments = list(comments_thread_viewable) for c in unfiltered_comments: if c.user_id and c.user_id != 1: try: userrole = UserRole.objects.get(user=c.user, text=text) except: userrole = UserRole.objects.get(user=None, text=None) if userrole.role_id == None: role = c.user.get_profile().global_userrole().role else: role = userrole.role if role.name == 'Individual student': comments_thread_viewable.remove(c) cache.set(key, comments_thread_viewable) return comments_thread_viewable elif user and has_perm(request, 'can_view_comment_own', text=text): if DECORATED_CREATORS: visible_comments = comments.filter(name=myself).order_by(*order_by) else: visible_comments = comments.filter(user=user).order_by(*order_by) # for role_teacher role model, add 'teacher' comments if (role_model == 'teacher'): with_teachers = [] for u in list(User.objects.filter(userrole__role__name = 'Teacher')): with_teachers.append(u.id) # add admin and current user admin = User.objects.get(id=1) with_teachers.append(admin.id) if DECORATED_CREATORS: visible_comments = comments.filter(Q(user__id__in=with_teachers) | Q(name=myself)).order_by(*order_by) else: with_teachers.append(user.id) visible_comments = comments.filter(user__id__in=with_teachers).order_by(*order_by) # filter comments with a non visible (i.e. moderated) comment in the above thread comments_thread_viewable = [c for c in visible_comments if c.is_thread_full_visible(own_user=user)] cache.set(key, comments_thread_viewable) return comments_thread_viewable else: cache.set(key, []) return []