Python models.User.get() Examples

The following are 30 code examples of models.User.get(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module models.User , or try the search function .
Example #1
Source File: tools.py    From mltshp with Mozilla Public License 2.0 6 votes vote down vote up
def post(self):
        url = self.get_argument('url', None)
        if not url:
            self.render("tools/save-video.html", url = url, title = None, description=None)
        url = Sourcefile.make_oembed_url(url.strip())
        if url:
            current_user = self.get_current_user_object();
            shake_id = self.get_argument('shake_id', None)
            if not shake_id:
                self.destination_shake = Shake.get('user_id=%s and type=%s', current_user.id, 'user')
            else:
                self.destination_shake = Shake.get('id=%s', shake_id)
                if not self.destination_shake:
                    return self.render("tools/save-video-error.html", message="We couldn't save the video to specified shake. Please contact support.")
                if not self.destination_shake.can_update(current_user.id):
                    return self.render("tools/save-video-error.html", message="We couldn't save the video to specified shake. Please contact support.")
                if current_user.email_confirmed != 1:
                    return self.render("tools/save-video-error.html", message="You must confirm your email address before you can post.")
            self.handle_oembed_url(url)
        else:
            self.render("tools/save-video-error.html", message="We could not load the embed code. The video server may be down. Please contact support.") 
Example #2
Source File: misc.py    From BotListBot with MIT License 6 votes vote down vote up
def set_notifications(bot, update, value: bool):
    cid = update.effective_chat.id
    try:
        notifications = Notifications.get(Notifications.chat_id == cid)
    except Notifications.DoesNotExist:
        notifications = Notifications(chat_id=cid)
    notifications.enabled = value
    notifications.save()

    Statistic.of(update, ('enabled' if value else 'disabled') + ' notifications for their group {}'.format(
        cid))

    msg = util.success("Nice! Notifications enabled.") if value else "Ok, notifications disabled."
    msg += '\nYou can always adjust this setting with the /subscribe command.'
    bot.formatter.send_or_edit(cid, msg, to_edit=util.mid_from_update(update))
    return ConversationHandler.END 
Example #3
Source File: urls.py    From python-webapp-blog with GNU General Public License v3.0 6 votes vote down vote up
def api_update_blog(blog_id):
    check_admin()
    i = ctx.request.input(name='', summary='', content='')
    name = i.name.strip()
    summary = i.summary.strip()
    content = i.content.strip()
    if not name:
        raise APIValueError('name', 'name cannot be empty.')
    if not summary:
        raise APIValueError('summary', 'summary cannot be empty.')
    if not content:
        raise APIValueError('content', 'content cannot be empty.')
    blog = Blog.get(blog_id)
    if blog is None:
        raise APIResourceNotFoundError('Blog')
    blog.name = name
    blog.summary = summary
    blog.content = content
    blog.update()
    return blog 
Example #4
Source File: admin.py    From mltshp with Mozilla Public License 2.0 6 votes vote down vote up
def get(self):
        where = "deleted=0 ORDER BY id DESC LIMIT 21"
        before_id = self.get_argument('before', None)
        if before_id is not None:
            where = "id < %d AND %s" % (int(before_id), where)
        users = User.where(where)

        prev_link = None
        next_link = None
        if len(users) == 21:
            next_link = "?before=%d" % users[-1].id

        for user in users[:20]:
            files = user.sharedfiles(per_page=1)
            user.last_sharedfile = len(files) == 1 and files[0] or None

        return self.render("admin/new-users.html", users=users[:20],
            previous_link=prev_link, next_link=next_link) 
Example #5
Source File: shake.py    From mltshp with Mozilla Public License 2.0 6 votes vote down vote up
def post(self, shake_name=None):
        shake = Shake.get('name=%s and user_id = %s and deleted=0', shake_name, self.current_user['id'])

        if not shake:
            raise tornado.web.HTTPError(404)

        sm = ShakeManager.get('user_id = %s and shake_id = %s and deleted = 0', self.get_argument('user_id', 0), shake.id)

        if sm:
            sm.delete()
            former_member = User.get('id=%s', self.get_argument('user_id'))
            Notification.send_shake_member_removal(shake, former_member)

            return self.write({'status':'ok'})

        raise tornado.web.HTTPError(403) 
Example #6
Source File: shake.py    From mltshp with Mozilla Public License 2.0 6 votes vote down vote up
def get(self, shake_name):
        shake = Shake.get("name=%s and deleted=0", shake_name)
        if not shake:
            raise tornado.web.HTTPError(404)

        current_user = self.get_current_user_object()
        invitation, invitation_requests = _invitations(shake, current_user)

        #is this user a shake manager?
        managers = shake.managers()
        is_shake_manager = False
        if managers and current_user:
            for manager in managers:
                if manager.id == current_user.id:
                    is_shake_manager = True
                    break

        followers = shake.subscribers()
        follower_count = shake.subscriber_count()

        return self.render("shakes/members.html", shake=shake, invitation=invitation,
            managers=shake.managers(), current_user_obj=current_user,
            invitation_requests=invitation_requests, shake_editor=shake.owner(),
            is_shake_manager=is_shake_manager, followers=followers,
            follower_count=follower_count) 
Example #7
Source File: shake.py    From mltshp with Mozilla Public License 2.0 6 votes vote down vote up
def post(self, shake_name=None):
        shake = Shake.get('name=%s and deleted=0', shake_name)
        current_user_object = self.get_current_user_object()

        if not shake:
            raise tornado.web.HTTPError(404)

        if current_user_object.can_request_invitation_to_shake(shake.id):
            current_user_object.request_invitation_to_shake(shake.id)
            if self.get_argument('json', None):
                return self.write({'status':'ok'})
            else:
                return self.redirect('/%s' % (shake.name))
        else:
            if self.get_argument('json', None):
                return self.write({'status':'error', 'message':'not allowed'})
            else:
                return self.redirect('/') 
Example #8
Source File: urls.py    From awesome-python3-webapp with GNU General Public License v2.0 6 votes vote down vote up
def parse_signed_cookie(cookie_str):
    try:
        L = cookie_str.split('-')
        if len(L) != 3:
            return None
        id, expires, md5 = L
        if int(expires) < time.time():
            return None
        user = User.get(id)
        if user is None:
            return None
        if md5 != hashlib.md5(('%s-%s-%s-%s' % (id, user.password, expires, _COOKIE_KEY)).encode('utf-8')).hexdigest():
            return None
        return user
    except:
        return None 
Example #9
Source File: shake.py    From mltshp with Mozilla Public License 2.0 6 votes vote down vote up
def post(self, shake_id):
        is_json = self.get_argument('json', None)
        user = self.get_current_user_object()

        shake = Shake.get('id=%s and deleted=0', shake_id)
        if not shake:
            if is_json:
                return self.write({'error':'Shake not found.'})
            else:
                return self.redirect(shake.path())

        if not user.unsubscribe(shake):
            if is_json:
                return self.write({'error':'error'})
            else:
                return self.redirect(shake.path())
        else:
            if is_json:
                return self.write(json_encode({'subscription_status': False}))
            else:
                return self.redirect(shake.path()) 
Example #10
Source File: shake.py    From mltshp with Mozilla Public License 2.0 6 votes vote down vote up
def post(self, shake_name):
        current_user = self.get_current_user_object()
        shake_to_update = Shake.get('name=%s and user_id=%s and type=%s and deleted=0', shake_name, current_user.id, 'group')
        new_title = self.get_argument('title', None)
        new_description = self.get_argument('description', None)

        if not shake_to_update:
            return self.write({'error':'No permission to update shake.'})

        if new_title:
            shake_to_update.title = new_title
        if new_description:
            shake_to_update.description = new_description
        shake_to_update.save()

        return self.redirect('/shake/' + shake_to_update.name + '/quick-details') 
Example #11
Source File: shake.py    From mltshp with Mozilla Public License 2.0 6 votes vote down vote up
def get(self, shake_name):
        shake = Shake.get("name=%s and deleted=0", shake_name)
        if not shake:
            raise tornado.web.HTTPError(404)

        value = {
            'title' : escape.xhtml_escape(shake.title) if shake.title else '',
            'title_raw' : shake.title if shake.title else '',
            'description' : escape.xhtml_escape(shake.description) if shake.description else '',
            'description_raw' : shake.description if shake.description else ''
        }
        # prevents IE from caching ajax requests.
        self.set_header("Cache-Control","no-store, no-cache, must-revalidate");
        self.set_header("Pragma","no-cache");
        self.set_header("Expires", 0);
        return self.write(escape.json_encode(value)) 
Example #12
Source File: image.py    From mltshp with Mozilla Public License 2.0 6 votes vote down vote up
def post(self, share_key, comment_id):
        shared_file = models.Sharedfile.get_by_share_key(share_key)
        user = self.get_current_user_object()
        comment = Comment.get("id=%s", comment_id)

        if not shared_file or not comment:
            raise tornado.web.HTTPError(404)

        existing_comment_like = models.CommentLike.get("comment_id = %s and user_id = %s",
                comment.id, user.id)

        if existing_comment_like:
            existing_comment_like.deleted = 1
            existing_comment_like.save()

        json = self.get_argument("json", False)
        if json:
            self.set_header("Cache-Control","no-store, no-cache, must-revalidate");
            self.set_header("Pragma","no-cache");
            self.set_header("Expires", 0);
            count = models.CommentLike.where_count("comment_id = %s", comment.id)
            return self.write(json_encode({'response':'ok', 'count': count, 'like' : True }))
        else:
            return self.redirect("/p/%s?salty" % (share_key,)) 
Example #13
Source File: test_transactions.py    From aiopeewee with MIT License 6 votes vote down vote up
def test_atomic_with_delete(flushdb):
    for i in range(3):
        await User.create(username=f'u{i}')

    async with db.atomic():
        user = await User.get(User.username == 'u1')
        await user.delete_instance()

    usernames = [u.username async for u in User.select()]
    assert sorted(usernames) == ['u0', 'u2']

    async with db.atomic():
        async with db.atomic():
            user = await User.get(User.username == 'u2')
            await user.delete_instance()

    usernames = [u.username async for u in User.select()]
    assert usernames == ['u0'] 
Example #14
Source File: shake.py    From mltshp with Mozilla Public License 2.0 6 votes vote down vote up
def post(self, shake_id):
        is_json = self.get_argument('json', None)
        user = self.get_current_user_object()

        shake = Shake.get('id=%s and deleted=0', shake_id)
        if not shake:
            if is_json:
                return self.write(json_encode({'error':'Shake not found.'}))
            else:
                return self.redirect(shake.path())

        if not user.subscribe(shake):
            if is_json:
                return self.write(json_encode({'error':'error'}))
            else:
                return self.redirect(shake.path())
        else:
            if is_json:
                return self.write(json_encode({'subscription_status': True}))
            else:
                return self.redirect(shake.path()) 
Example #15
Source File: image.py    From mltshp with Mozilla Public License 2.0 6 votes vote down vote up
def post(self, share_key, comment_id):
        shared_file = Sharedfile.get_by_share_key(share_key)
        if not shared_file:
            raise tornado.web.HTTPError(404)

        ajax = self.get_argument('ajax', False)
        redirect_suffix = ""
        if ajax:
            # Prevent IE cache.
            self.set_header("Cache-Control","no-store, no-cache, must-revalidate");
            self.set_header("Pragma","no-cache");
            self.set_header("Expires", 0);
            redirect_suffix = "/quick-comments?expanded=1"

        user = self.get_current_user_object()
        comment = Comment.get("id = %s", comment_id)
        if comment.can_user_delete(user):
            comment.delete()
        return self.redirect("/p/%s%s" % (share_key, redirect_suffix)) 
Example #16
Source File: image.py    From mltshp with Mozilla Public License 2.0 6 votes vote down vote up
def post(self, share_key, shake_id):
        current_user = self.get_current_user_object()
        sharedfile = Sharedfile.get_by_share_key(share_key)
        shake = Shake.get("id = %s", shake_id)
        if not sharedfile:
            raise tornado.web.HTTPError(404)
        if not shake:
            raise tornado.web.HTTPError(404)
        if not sharedfile.can_user_delete_from_shake(current_user, shake):
            raise tornado.web.HTTPError(403)
        sharedfile.delete_from_shake(shake)
        redirect_to = self.get_argument("redirect_to", None)
        if redirect_to:
            return self.redirect(redirect_to)
        else:
            return self.redirect("/p/%s" % sharedfile.share_key) 
Example #17
Source File: image.py    From mltshp with Mozilla Public License 2.0 6 votes vote down vote up
def get(self, share_key):
        sharedfile = Sharedfile.get_by_share_key(share_key)
        if not sharedfile:
            raise tornado.web.HTTPError(404)

        expanded = self.get_argument("expanded", False)
        if expanded:
            expanded = True

        # Prevent IE from caching AJAX requests
        self.set_header("Cache-Control","no-store, no-cache, must-revalidate");
        self.set_header("Pragma","no-cache");
        self.set_header("Expires", 0);

        user = self.get_current_user_object()
        can_comment = user and user.email_confirmed == 1 and not options.readonly

        comments = sharedfile.comments()
        html_response = self.render_string("image/quick-comments.html", sharedfile=sharedfile,
            comments=comments, current_user=user,
            can_comment=can_comment,
            expanded=expanded)
        return self.write({'result' : 'ok', 'count' : len(comments), 'html' : html_response }) 
Example #18
Source File: urls.py    From python-webapp-blog with GNU General Public License v3.0 6 votes vote down vote up
def parse_signed_cookie(cookie_str):
    try:
        L = cookie_str.split('-')
        if len(L) != 3:
            return None
        id, expires, md5 = L
        if int(expires) < time.time():
            return None
        user = User.get(id)
        if user is None:
            return None
        if md5 != hashlib.md5('%s-%s-%s-%s' % (id, user.password, expires, _COOKIE_KEY)).hexdigest():
            return None
        return user
    except:
        return None 
Example #19
Source File: admin.py    From mltshp with Mozilla Public License 2.0 5 votes vote down vote up
def get(self, type=None):
        group_shakes = Shake.where('type=%s ORDER BY recommended', 'group')
        return self.render("admin/group-shake-recommended.html", group_shakes=group_shakes) 
Example #20
Source File: image.py    From mltshp with Mozilla Public License 2.0 5 votes vote down vote up
def post(self, share_key):
        current_user = self.get_current_user_object()
        sharedfile = Sharedfile.get_by_share_key(share_key)
        if not sharedfile:
            raise tornado.web.HTTPError(404)
        if current_user.id != sharedfile.user_id:
            raise tornado.web.HTTPError(403)
        shakes = self.get_arguments('shakes', [])
        for shake_id in shakes:
            shake = Shake.get("id = %s", shake_id)
            if shake.can_update(current_user.id):
                sharedfile.add_to_shake(shake)
        return self.redirect("/p/%s" % sharedfile.share_key) 
Example #21
Source File: image.py    From mltshp with Mozilla Public License 2.0 5 votes vote down vote up
def post(self, share_key):
        user = self.get_current_user()
        sf = Sharedfile.get("share_key=%s and user_id=%s", share_key, user['id'])
        if sf:
            sf.delete()
        return self.redirect("/") 
Example #22
Source File: image.py    From mltshp with Mozilla Public License 2.0 5 votes vote down vote up
def get(self, share_key):
        sharedfile = Sharedfile.get_by_share_key(share_key)
        if not sharedfile:
            return {'error': 'Invalid file key.'}

        response_data = []
        for sharedfile in sharedfile.favorites():
            user = sharedfile.user()
            response_data.append({
                'user_name' : user.name,
                'user_profile_image_url' :  user.profile_image_url(),
                'posted_at_friendly' : sharedfile.pretty_created_at()
            })

        return self.write({'result' : response_data, 'count' : len(response_data)}) 
Example #23
Source File: admin.py    From mltshp with Mozilla Public License 2.0 5 votes vote down vote up
def post(self, type=None):
        shake_to_update = Shake.get("id = %s and type=%s", self.get_argument('shake_id', None), 'group')
        if shake_to_update:
            if type=="recommend":
                shake_to_update.recommended = 1
                shake_to_update.save()
            elif type=='unrecommend':
                shake_to_update.recommended = 0
                shake_to_update.save()
        return self.redirect("/admin/recommend-group-shake") 
Example #24
Source File: urls.py    From python-webapp-blog with GNU General Public License v3.0 5 votes vote down vote up
def _get_page_index():
    page_index = 1
    try:
        page_index = int(ctx.request.get('page', '1'))
    except ValueError:
        pass
    return page_index 
Example #25
Source File: image.py    From mltshp with Mozilla Public License 2.0 5 votes vote down vote up
def get(self, share_key):
        sharedfile = Sharedfile.get_by_share_key(share_key)
        value = {
            'title' : escape.xhtml_escape(sharedfile.get_title()),
            'title_raw' : sharedfile.get_title()
        }
        # prevents IE from caching ajax requests.
        self.set_header("Cache-Control","no-store, no-cache, must-revalidate");
        self.set_header("Pragma","no-cache");
        self.set_header("Expires", 0);
        return self.write(escape.json_encode(value)) 
Example #26
Source File: image.py    From mltshp with Mozilla Public License 2.0 5 votes vote down vote up
def get(self, share_key):
        sharedfile = Sharedfile.get_by_share_key(share_key)
        source_url = ''
        if sharedfile.source_url:
            source_url = sharedfile.source_url
        value = {
            'source_url' : escape.xhtml_escape(source_url),
            'source_url_raw' : source_url
        }
        return self.write(escape.json_encode(value)) 
Example #27
Source File: image.py    From mltshp with Mozilla Public License 2.0 5 votes vote down vote up
def get(self):
        parsed_url = None
        try:
            parsed_url = urlparse(self.get_argument('url', None))
        except:
            raise tornado.web.HTTPError(404)
        if parsed_url.netloc not in [options.app_host, 'mltshp.localhost:8000', 'localhost:8000', 'localhost:81', 'www.' + options.app_host]:
            raise tornado.web.HTTPError(404)
        share_key = re.search("/p/([A-Za-z0-9]+)", parsed_url.path)
        if not share_key:
            raise tornado.web.HTTPError(404)

        include_embed = self.get_argument('include_embed', False)

        sharedfile = Sharedfile.get_by_share_key(share_key.group(1))
        sourcefile = sharedfile.sourcefile()
        sharedfile_owner = User.get("id=%s", sharedfile.user_id)

        jsonp = None
        if self.get_argument('jsoncallback', None):
            jsonp=self.get_argument('jsoncallback')
            match = re.search('^[A-Za-z0-9]+$', jsonp)
            if not match:
                jsonp = None

        if jsonp:
            self.set_header("Content-Type", "application/json")
        else:
            self.set_header("Content-Type", "application/javascript")
        self.render("services/oembed.json", sharedfile=sharedfile, sourcefile=sourcefile,
                    sharedfile_owner=sharedfile_owner, jsonp=jsonp,
                    include_embed=include_embed, app_host=options.app_host,
                    cdn_host=options.cdn_host) 
Example #28
Source File: admin.py    From mltshp with Mozilla Public License 2.0 5 votes vote down vote up
def post(self, user_name):
        user = User.get('name = %s', user_name)
        if not user:
            return self.redirect('/')
        
        nsfw = int(self.get_argument("nsfw", 0))
        if nsfw == 1:
            user.flag_nsfw()
        else:
            user.unflag_nsfw()
        user.save()
        send_slack_notification("%s flagged user '%s' as %s" % (self.admin_user.name, user.name, nsfw == 1 and "NSFW" or "SFW"),
            channel="#moderation", icon_emoji=":ghost:", username="modbot")
        return self.redirect("/user/%s" % user.name) 
Example #29
Source File: image.py    From mltshp with Mozilla Public License 2.0 5 votes vote down vote up
def post(self, share_key):
        sharedfile = Sharedfile.get_by_share_key(share_key)
        if not sharedfile:
            raise tornado.web.HTTPError(404)

        current_user = self.get_current_user_object()
        if not current_user:
            raise tornado.web.HTTPError(403)

        json = self.get_arguments('json', False)
        if not sharedfile.can_save(current_user):
            if json:
                return self.write({'error' : "Can't save that file."})
            else:
                return self.redirect("/p/%s" % sharedfile.share_key)

        count = sharedfile.save_count

        shake_id = self.get_argument('shake_id', None)
        if shake_id:
            shake = Shake.get("id = %s", shake_id)
            if shake and shake.can_update(current_user.id):
                new_sharedfile = sharedfile.save_to_shake(current_user, shake)
            else:
                return self.write({'error' : "Can't save that file."})
        else:
            new_sharedfile = sharedfile.save_to_shake(current_user)
        if json:
            return self.write({
                'new_share_key' : new_sharedfile.share_key,
                'share_key' : sharedfile.share_key,
                'count' : count + 1
            })
        else:
            return self.redirect("/p/%s" % new_sharedfile.share_key) 
Example #30
Source File: admin.py    From mltshp with Mozilla Public License 2.0 5 votes vote down vote up
def get(self):
        if not self.admin_user.is_superuser():
            return self.redirect('/admin')
        return self.render('admin/delete-user.html')