def tag_timeline(tagname): endpoint = get_endpoint_or_none("#" + tagname) appearances = PostAppearance.objects.filter(endpoint=endpoint) tag_posts = (x.post for x in appearances.order_by("-timestamp")) return dict(syntax="#" + tagname, posts=tag_posts, DATA_URL=settings.DATA_URL)
def book_timeline(bookname): endpoint = get_endpoint_or_none(u"\u212c" + bookname) appearances = PostAppearance.objects.filter(endpoint=endpoint) book_posts = (x.post for x in appearances.order_by("-timestamp")) return dict(syntax=u"\u212c" + bookname, posts=book_posts, DATA_URL=settings.DATA_URL)
def form_valid(self, form): user = form.save() profile = user.profile profile.description = form.data.get('aboutyourself', '') profile.save() if 'profile_pic' in form.files: misc.set_profile_image(user, form.files['profile_pic']) else: if form.data.get('profile_pic_remove', False): profile.remove_image() try: ep_config = get_endpoint_or_none( '@%s' % user.username).get_config() ep_config.notification_filter = form.data.get('notification', '') ep_config.save() except: pass # send a success message to user messages.success( self.request, _('User settings have been saved successfully!')) return redirect(self.get_success_url())
def form_valid(self, form): user = form.save() profile = user.profile profile.description = form.data.get('aboutyourself', '') profile.save() if 'profile_pic' in form.files: misc.set_profile_image(user, form.files['profile_pic']) else: if form.data.get('profile_pic_remove', False): profile.remove_image() try: ep_config = get_endpoint_or_none('@%s' % user.username).get_config() ep_config.notification_filter = form.data.get('notification', '') ep_config.save() except: pass # send a success message to user messages.success(self.request, _('User settings have been saved successfully!')) return redirect(self.get_success_url())
def group_timeline(groupname): endpoint = get_endpoint_or_none("!" + groupname) appearances = PostAppearance.objects.filter(endpoint=endpoint) group_posts = (x.post for x in appearances.order_by("-timestamp")) return dict(syntax="!" + groupname, posts=group_posts, DATA_URL=settings.DATA_URL)
def user_followingbox(username, template_name="messaging/followingbox.html"): user = get_endpoint_or_none(syntax="@" + username) followings = Following.objects.filter(follower=user) target_users = (following.target.syntax[1:] for following in followings if following.target.syntax.startswith("@")) t = template.loader.get_template(template_name) return t.render(Context(dict(target_users=target_users)))
def user_tagbox(username): user = get_endpoint_or_none(syntax="@" + username) followings = Following.objects.filter(follower=user) tags = (following.target.syntax[1:] for following in followings if following.target.syntax.startswith("#")) books = (following.target.syntax[1:] for following in followings if following.target.syntax.startswith(u"\u212c")) return dict(tags=tags, books=books)
def save_settings(request, username): from django.contrib.auth.models import User from booki.editor import models try: user = User.objects.get(username=username) except User.DoesNotExist: return pages.ErrorPage(request, "errors/user_does_not_exist.html", {"username": username}) if request.user.username != username: return HttpResponse("No, can't do!", "text/plain") profile = user.get_profile() user.email = request.POST.get('email', '') user.first_name = request.POST.get('fullname', '') user.save() profile.description = request.POST.get('aboutyourself', '') if request.FILES.has_key('profile'): from booki.utils import misc # Check for any kind of error at the moment. # Notify user later with different messages. try: misc.setProfileImage(user, request.FILES['profile']) except: pass try: profile.save() endpoint_config = get_endpoint_or_none("@" + user.username).get_config() endpoint_config.notification_filter = request.POST.get( 'notification', '') endpoint_config.save() except: # Should set some error code here transaction.rollback() else: transaction.commit() try: resp = HttpResponse( '<html><head><script>var j = parent.jQuery; j(function() { j.booki.profile.reloadProfileInfo(); });</script></head></html>', 'text/html') except: transaction.rollback() raise else: transaction.commit() return resp
def save_settings(request, username): from django.contrib.auth.models import User from booki.editor import models try: user = User.objects.get(username=username) except User.DoesNotExist: return pages.ErrorPage(request, "errors/user_does_not_exist.html", {"username": username}) if request.user.username != username: return HttpResponse("No, can't do!", "text/plain") profile = user.get_profile() user.email = request.POST.get('email' ,'') user.first_name = request.POST.get('fullname', '') user.save() profile.description = request.POST.get('aboutyourself', '') if request.FILES.has_key('profile'): from booki.utils import misc # Check for any kind of error at the moment. # Notify user later with different messages. try: misc.setProfileImage(user, request.FILES['profile']) except: pass try: profile.save() endpoint_config = get_endpoint_or_none("@"+user.username).get_config() endpoint_config.notification_filter = request.POST.get('notification', '') endpoint_config.save() except: # Should set some error code here transaction.rollback() else: transaction.commit() try: resp = HttpResponse('<html><head><script>var j = parent.jQuery; j(function() { j.booki.profile.reloadProfileInfo(); });</script></head></html>', 'text/html') except: transaction.rollback() raise else: transaction.commit() return resp
def user_timeline(username): endpoint = get_endpoint_or_none("@"+username) posts_by_user = Post.objects.filter(sender=endpoint).order_by("-timestamp") appearances = PostAppearance.objects.filter(endpoint=endpoint) user_posts = (x.post for x in appearances.order_by("-timestamp")) posts = sorted(chain(posts_by_user, user_posts), key=lambda x:x.timestamp, reverse=True) return dict(syntax="@"+username, posts=posts, DATA_URL=settings.DATA_URL)
def user_timeline(username): endpoint = get_endpoint_or_none("@" + username) posts_by_user = Post.objects.filter(sender=endpoint).order_by("-timestamp") appearances = PostAppearance.objects.filter(endpoint=endpoint) user_posts = (x.post for x in appearances.order_by("-timestamp")) posts = sorted(chain(posts_by_user, user_posts), key=lambda x: x.timestamp, reverse=True) return dict(syntax="@" + username, posts=posts, DATA_URL=settings.DATA_URL)
def get_initial(self): profile = self.object.profile initial = super(UserSettingsPage, self).get_initial() initial['aboutyourself'] = profile.description endpoint = get_endpoint_or_none('@%s' % self.object.username) try: endpoint_config = endpoint.get_config() initial['notification'] = endpoint_config.notification_filter except Exception: initial['notification'] = '' if profile.image: initial['profile_pic'] = profile.image.url return initial
def book_followbutton(bookname, requestuser): return dict(bookname=bookname, alreadyfollowing=bool( Following.objects.filter( follower=get_endpoint_or_none("@" + requestuser), target=get_endpoint_or_none(u"\u212c" + bookname))))
def user_followersbox(username): endpoint = get_endpoint_or_none(syntax="@"+username) followings = Following.objects.filter(target=endpoint) followers = (following.follower.syntax[1:] for following in followings) return dict(followers=followers)
def user_followersbox(username): endpoint = get_endpoint_or_none(syntax="@" + username) followings = Following.objects.filter(target=endpoint) followers = (following.follower.syntax[1:] for following in followings) return dict(followers=followers)
def user_followingbox(username): user = get_endpoint_or_none(syntax="@" + username) followings = Following.objects.filter(follower=user) target_users = (following.target.syntax[1:] for following in followings if following.target.syntax.startswith("@")) return dict(target_users=target_users)
def group_timeline(groupname): endpoint = get_endpoint_or_none("!"+groupname) appearances = PostAppearance.objects.filter(endpoint=endpoint) group_posts = (x.post for x in appearances.order_by("-timestamp")) return dict(syntax="!"+groupname, posts=group_posts, DATA_URL=settings.DATA_URL)
def book_followbutton(bookname, requestuser): return dict(bookname=bookname, alreadyfollowing=bool(Following.objects.filter(follower=get_endpoint_or_none("@"+requestuser), target=get_endpoint_or_none(u"\u212c"+bookname))))
def view_profile(request, username): """ Django View. Shows user profile. Right now, this is just basics. @type request: C{django.http.HttpRequest} @param request: Django Request @type username: C{string} @param username: Username. @todo: Check if user exists. """ from django.contrib.auth.models import User from booki.editor import models from booki.utils.misc import isBookLimitReached try: user = User.objects.get(username=username) except User.DoesNotExist: try: resp = pages.ErrorPage(request, "errors/user_does_not_exist.html", {"username": username}) except: transaction.rollback() raise else: transaction.commit() return resp notification_filter = '' if request.user.username == username: books = models.Book.objects.filter(owner=user) endpoint = get_endpoint_or_none("@" + user.username) if endpoint: endpoint_config = endpoint.get_config() if endpoint_config: notification_filter = endpoint_config.notification_filter else: if request.user.is_authenticated() and request.user.is_superuser: books = models.Book.objects.filter(owner=user) else: books = models.Book.objects.filter(owner=user, hidden=False) groups = user.members.all() from django.utils.html import escape userDescription = escape(user.get_profile().description) admin_create = config.getConfiguration('ADMIN_CREATE_BOOKS') admin_import = config.getConfiguration('ADMIN_IMPORT_BOOKS') if request.user.is_superuser: admin_create = False admin_import = False try: resp = render_to_response( 'account/view_profile.html', { "request": request, "user": user, "admin_create": admin_create, "admin_import": admin_import, "user_description": '<br/>'.join(userDescription.replace('\r', '').split('\n')), "books": books, "limit_reached": isBookLimitReached(), "notification_filter": notification_filter, "groups": groups }) except: transaction.rollback() raise else: transaction.commit() return resp
def user_tagbox(username): user = get_endpoint_or_none(syntax="@"+username) followings = Following.objects.filter(follower=user) tags = (following.target.syntax[1:] for following in followings if following.target.syntax.startswith("#")) books = (following.target.syntax[1:] for following in followings if following.target.syntax.startswith(u"\u212c")) return dict(tags=tags, books=books)
def user_followersbox(username, template_name="messaging/followersbox.html"): endpoint = get_endpoint_or_none(syntax="@" + username) followings = Following.objects.filter(target=endpoint) followers = (following.follower.syntax[1:] for following in followings) t = template.loader.get_template(template_name) return t.render(Context(dict(followers=followers)))
def user_followingbox(username, template_name="messaging/followingbox.html"): user = get_endpoint_or_none(syntax="@"+username) followings = Following.objects.filter(follower=user) target_users = (following.target.syntax[1:] for following in followings if following.target.syntax.startswith("@")) t = template.loader.get_template(template_name) return t.render(template.Context(dict(target_users=target_users)))
def user_followingbox(username): user = get_endpoint_or_none(syntax="@"+username) followings = Following.objects.filter(follower=user) target_users = (following.target.syntax[1:] for following in followings if following.target.syntax.startswith("@")) return dict(target_users=target_users)
def tag_timeline(tagname): endpoint = get_endpoint_or_none("#"+tagname) appearances = PostAppearance.objects.filter(endpoint=endpoint) tag_posts = (x.post for x in appearances.order_by("-timestamp")) return dict(syntax="#"+tagname, posts=tag_posts, DATA_URL=settings.DATA_URL)
def book_timeline(bookname): endpoint = get_endpoint_or_none(u"\u212c"+bookname) appearances = PostAppearance.objects.filter(endpoint=endpoint) book_posts = (x.post for x in appearances.order_by("-timestamp")) return dict(syntax=u"\u212c"+bookname, posts=book_posts, DATA_URL=settings.DATA_URL)
def tag_followbutton(tagname, requestuser): return dict(tagname=tagname, alreadyfollowing=bool( Following.objects.filter( follower=get_endpoint_or_none("@" + requestuser), target=get_endpoint_or_none("#" + tagname))))
def save_settings(request, username): from django.contrib.auth.models import User from booki.editor import models try: user = User.objects.get(username=username) except User.DoesNotExist: return pages.ErrorPage(request, "errors/user_does_not_exist.html", {"username": username}) if request.user.username != username: return HttpResponse("No, can't do!", "text/plain") from django.core.files import File profile = user.get_profile() user.email = request.POST.get('email' ,'') user.first_name = request.POST.get('fullname', '') user.save() profile.description = request.POST.get('aboutyourself', '') if request.FILES.has_key('profile'): import tempfile import os # check this later fh, fname = tempfile.mkstemp(suffix='', prefix='profile') f = open(fname, 'wb') for chunk in request.FILES['profile'].chunks(): f.write(chunk) f.close() import Image try: im = Image.open(fname) THUMB_SIZE = 100, 100 width, height = im.size if width > height: delta = width - height left = int(delta/2) upper = 0 right = height + left lower = height else: delta = height - width left = 0 upper = int(delta/2) right = width lower = width + upper im = im.crop((left, upper, right, lower)) im.thumbnail(THUMB_SIZE, Image.ANTIALIAS) im.save('%s/%s%s.jpg' % (settings.MEDIA_ROOT, settings.PROFILE_IMAGE_UPLOAD_DIR, user.username), 'JPEG') profile.image = '%s%s.jpg' % (settings.PROFILE_IMAGE_UPLOAD_DIR, user.username) except: # handle this in better way pass os.unlink(fname) profile.save() endpoint_config = get_endpoint_or_none("@"+user.username).get_config() endpoint_config.notification_filter = request.POST.get('notification', '') endpoint_config.save() try: return HttpResponse('<html><head><script>var j = parent.jQuery; j(function() { j.booki.profile.reloadProfileInfo(); });</script></head></html>', 'text/html') except: transaction.rollback() finally: transaction.commit()
def user_settings(request, username): """ Django View. Edit user settings. Right now, this is just basics. @type request: C{django.http.HttpRequest} @param request: Django Request @type username: C{string} @param username: Username. @todo: Check if user exists. """ from django.contrib.auth.models import User from booki.editor import models try: user = User.objects.get(username=username) except User.DoesNotExist: return pages.ErrorPage(request, "errors/user_does_not_exist.html", {"username": username}) if request.method == 'POST': settings_form = SettingsForm(request.POST, request.FILES) if settings_form.is_valid(): # this is very stupid and wrong # change the way it works # make utils for # - get image url # - get image path # - seperate storage for from django.core.files import File profile = user.get_profile() user.email = settings_form.cleaned_data['email'] user.first_name = settings_form.cleaned_data['firstname'] #user.last_name = settings_form.cleaned_data['lastname'] user.save() profile.description = settings_form.cleaned_data['description'] # this works for now, but this kind of processing must be done somewhere else! if settings_form.cleaned_data['image']: import tempfile import os # check this later fh, fname = tempfile.mkstemp(suffix='', prefix='profile') f = open(fname, 'wb') for chunk in settings_form.cleaned_data['image'].chunks(): f.write(chunk) f.close() import Image im = Image.open(fname) im.thumbnail((120, 120), Image.NEAREST) imageName = '%s.jpg' % fname im.save(imageName) profile.image.save('%s.jpg' % user.username, File(file(imageName))) os.unlink(fname) profile.save() endpoint_config = get_endpoint_or_none("@"+user.username).get_config() endpoint_config.notification_filter = settings_form.cleaned_data['notification_filter'] endpoint_config.save() transaction.commit() return HttpResponseRedirect(reverse("view_profile", args=[username])) else: settings_form = SettingsForm(initial = {'image': 'aaa', 'firstname': user.first_name, #'lastname': user.last_name, 'description': user.get_profile().description, 'email': user.email, 'notification_filter': get_endpoint_or_none("@"+user.username).notification_filter()}) try: return render_to_response('account/user_settings.html', {"request": request, "user": user, "settings_form": settings_form, }) except: transaction.rollback() finally: transaction.commit()
def tag_followbutton(tagname, requestuser): return dict(tagname=tagname, alreadyfollowing=bool(Following.objects.filter(follower=get_endpoint_or_none("@"+requestuser), target=get_endpoint_or_none("#"+tagname))))
def view_profile(request, username): """ Django View. Shows user profile. Right now, this is just basics. @type request: C{django.http.HttpRequest} @param request: Django Request @type username: C{string} @param username: Username. @todo: Check if user exists. """ from django.contrib.auth.models import User from booki.editor import models from booki.utils.misc import isBookLimitReached try: user = User.objects.get(username=username) except User.DoesNotExist: try: resp = pages.ErrorPage(request, "errors/user_does_not_exist.html", {"username": username}) except: transaction.rollback() raise else: transaction.commit() return resp notification_filter = '' if request.user.username == username: #books = models.Book.objects.filter(owner=user) books = models.Book.objects.raw('select * from editor_book where group_id in' ' (select bookigroup_id from editor_bookigroup_members where user_id=%s)' , (user.id,)) endpoint = get_endpoint_or_none("@"+user.username) if endpoint: endpoint_config = endpoint.get_config() if endpoint_config: notification_filter = endpoint_config.notification_filter else: if request.user.is_authenticated() and request.user.is_superuser: books = models.Book.objects.filter(owner=user) else: books = models.Book.objects.raw('select * from editor_book where group_id in' ' (select bookigroup_id from editor_bookigroup_members where user_id=%s)' , (user.id,)) #books = models.Book.objects.filter(owner=user, hidden=False) groups = user.members.all() from django.utils.html import escape userDescription = escape(user.get_profile().description) admin_create = config.getConfiguration('ADMIN_CREATE_BOOKS') admin_import = config.getConfiguration('ADMIN_IMPORT_BOOKS') if request.user.is_superuser: admin_create = False admin_import = False try: resp = render_to_response('account/view_profile.html', {"request": request, "user": user, "admin_create": admin_create, "admin_import": admin_import, "user_description": '<br/>'.join(userDescription.replace('\r','').split('\n')), "books": books, "limit_reached": isBookLimitReached(), "notification_filter": notification_filter, "groups": groups}) except: transaction.rollback() raise else: transaction.commit() return resp
def user_followersbox(username, template_name="messaging/followersbox.html"): endpoint = get_endpoint_or_none(syntax="@"+username) followings = Following.objects.filter(target=endpoint) followers = (following.follower.syntax[1:] for following in followings) t = template.loader.get_template(template_name) return t.render(template.Context(dict(followers=followers)))