def get_notifications(self): notifications = models.Notification.objects.filter(target=self.request.user).order_by('-created_at') out = [] for notification in notifications: me = {'created_at': notification.created_at.strftime("%s"), 'created_by': notification.created_by.username, 'target': notification.target.username, 'text': notification.text, 'type': notification.type} if notification.type == models.Notification.TYPE_MENTION: me['status_text'] = "mentioned you in a post" elif notification.type == models.Notification.TYPE_FOLLOWER: me['status_text'] = "started following you" elif notification.type == models.Notification.TYPE_COMMENT: me['status_text'] = "commented on your post" created_by = models.User.objects.filter(ref=notification.created_by)[0] if created_by.avatar: file = File.open_unknown_type(created_by.avatar) me['mugshot'] = file.get_url() me['mugshot_id'] = file.model.id if notification.post: file = File.open_unknown_type(notification.post.file) me['post_image'] = file.get_url() me['post_image_id'] = file.model.id me['post_id'] = notification.post.id out.append(me) return {'notifications': out}
def mugshot(self): if 'file' in self.request.FILES: upload = self.request.FILES['file'] file = File.create_unknown_type(upload.temporary_file_path(), self.request.user, mime_type=upload.content_type) elif 'file' in self.request.POST: temp = tempfile.NamedTemporaryFile() temp.write(self.request.POST['file'].encode("UTF-8")) temp.flush() file = File.create_unknown_type(temp.name, self.request.user, mime_type='image/jpeg') user = models.User.objects.filter(ref=self.request.user)[0] user.avatar = file.model user.save() return True
def following(self): users = models.UserRelation.objects.filter(user__ref=self.model.ref) out = [] for user in users: ref = models.User.objects.filter(ref=user.following)[0] me = {'username': user.following.username, 'id': user.following.id} if ref.avatar: file = File.open_unknown_type(ref.avatar) me['mugshot'] = file.get_url() me['mugshot_id'] = file.model.id out.append(me) return {'users': out}
def followers(self): users = models.UserRelation.objects.filter(following__ref=self.model.ref) out = [] for user in users: ref = models.User.objects.filter(ref=user.user)[0] me = {'username': user.user.username, 'id': user.user.id, 'me_following': models.UserRelation.objects.filter(user=self.request.user, following=user.user).count() > 0} if ref.avatar: file = File.open_unknown_type(ref.avatar) me['mugshot'] = file.get_url() me['mugshot_id'] = file.model.id out.append(me) return {'users': out}
def search(self): offset = self.args.get('offset', 0) limit = self.args.get('limit', 10) users = AuthUser.objects.filter(username__icontains=self.args['username'].strip())[offset:limit] out = [] for user in users: me = {'username': user.username} ref = models.User.objects.filter(ref=user)[0] if ref.avatar: file = File.open_unknown_type(ref.avatar) me['mugshot'] = file.get_url() me['mugshot_id'] = file.model.id out.append(me) return {'users': out}
def facebook_friends(self): ids = json.loads(self.args['ids']) out = [] for id in ids: users = models.User.objects.filter(fbid=id) if users.count(): for user in users: me = {'username': user.ref.username, 'id': user.ref.id, 'me_following': models.UserRelation.objects.filter(user=self.request.user, following=user.ref).count() > 0} if user.avatar: file = File.open_unknown_type(user.avatar) me['mugshot'] = file.get_url() me['mugshot_id'] = file.model.id out.append(me) return {'users': out}
def get_root(self): user = models.User.objects.filter(ref__username=self.args['username'])[0] out = {} out['posts_count'] = models.Post.objects.filter(created_by=user.ref).count() out['followers_count'] = models.UserRelation.objects.filter(following=user.ref).count() out['following_count'] = models.UserRelation.objects.filter(user=user.ref).count() out['username'] = user.ref.username posts = models.Post.objects.filter(created_by=user).values_list('id') out['looks_count'] = models.Look.objects.filter(post__in=posts).count() out['me_following'] = models.UserRelation.objects.filter(following=user.ref, user=self.request.user).count() > 0 out['id'] = user.ref.id if user.avatar: file = File.open_unknown_type(user.avatar) out['mugshot'] = file.get_url() out['mugshot_id'] = file.model.id return {'user': out}