示例#1
0
文件: migrate.py 项目: MSylvia/snipt
def snipts():

    print "Deleting existing snipts"
    snipts = Snipt.objects.all()
    for s in snipts:
        s.delete()

    print "Deleting existing tags"
    existing_tags = Tag.objects.all()
    existing_tagged_items = Tag.objects.all()

    for t in existing_tags:
        t.delete()

    for t in existing_tagged_items:
        t.delete()

    cursor.execute("SELECT * FROM snippet_snippet")
    rows = cursor.fetchall()

    print "Adding snipts"

    for row in rows:
        snipt_id = row[0]
        code     = row[1]
        title    = row[2]
        created  = row[3]
        user_id  = row[4]
        tags     = row[5]
        lexer    = row[6]
        public   = row[7]
        key      = row[8]
        slug     = row[9]

        title = title[:255]

        snipt = Snipt(
            id=snipt_id,
            code=code,
            title=title,
            slug=slug,
            lexer=lexer,
            key=key,
            user=User.objects.get(id=user_id),
            public=public,
            created=created,
            modified=created,
        )
        for t in parse_tag_input(tags):
            snipt.tags.add(t)

        print 'Saving snipt ' + snipt.title
        snipt.save()

    print 'Done with snipts'
示例#2
0
    def setUp(self):
        super(SniptResourceTest, self).setUp()

        # Johnny
        self.johnny = User.objects.create_user('johnny', '*****@*****.**',
                                               'password')
        ApiKey.objects.get_or_create(user=self.johnny)
        self.johnny_auth = self.create_apikey(self.johnny,
                                              self.johnny.api_key.key)
        self.johnny_private = Snipt(title='Private snipt for Johnny',
                                    lexer='text',
                                    public=False,
                                    user=self.johnny)
        self.johnny_public = Snipt(title='Public snipt for Johnny',
                                   lexer='text',
                                   public=True,
                                   user=self.johnny)
        self.johnny_private.save()
        self.johnny_public.save()

        # Bob
        self.bob = User.objects.create_user('bob', '*****@*****.**', 'password')
        ApiKey.objects.get_or_create(user=self.bob)
        self.bob_auth = self.create_apikey(self.bob, self.bob.api_key.key)
        self.bob_private = Snipt(title='Private snipt for Bob',
                                 lexer='text',
                                 public=False,
                                 user=self.bob)
        self.bob_public = Snipt(title='Public snipt for Bob',
                                lexer='text',
                                public=True,
                                user=self.bob)
        self.bob_private.save()
        self.bob_public.save()
示例#3
0
    def handle(self, *args, **options):
        api_key = options["api_key"][0]
        from_username = options["from_username"][0]
        to_username = options["to_username"][0]
        to_user = User.objects.get(username=to_username)

        print(u"Fetching snipts...")

        items = get_snipts(api_key, from_username)
        items.reverse()

        for snipt in items:
            s = Snipt(
                blog_post=snipt["blog_post"],
                code=snipt["code"],
                description=snipt["description"],
                id=snipt["id"],
                key=snipt["key"],
                lexer=snipt["lexer"],
                line_count=snipt["line_count"],
                meta=snipt["meta"],
                public=snipt["public"],
                publish_date=snipt["publish_datetime"],
                secure=snipt["secure"],
                slug=snipt["slug"],
                stylized=snipt["stylized"],
                title=snipt["title"],
                user=to_user,
                views=snipt["views"],
            )

            s.created = snipt["created"]
            s.modified = snipt["modified"]

            for tag in snipt["tags"]:
                s.tags.add(tag["name"])

            s.save()

            self.stdout.write(snipt["title"])
示例#4
0
class SniptResourceTest(ResourceTestCase):
    fixtures = ['test_entries.json']

    def setUp(self):
        super(SniptResourceTest, self).setUp()

        # Johnny
        self.johnny = User.objects.create_user('johnny', '*****@*****.**', 'password')
        ApiKey.objects.get_or_create(user=self.johnny)
        self.johnny_auth = self.create_apikey(self.johnny, self.johnny.api_key.key)
        self.johnny_private = Snipt(title='Private snipt for Johnny', lexer='text', public=False, user=self.johnny)
        self.johnny_public = Snipt(title='Public snipt for Johnny', lexer='text', public=True, user=self.johnny)
        self.johnny_private.save()
        self.johnny_public.save()

        # Bob
        self.bob = User.objects.create_user('bob', '*****@*****.**', 'password')
        ApiKey.objects.get_or_create(user=self.bob)
        self.bob_auth = self.create_apikey(self.bob, self.bob.api_key.key)
        self.bob_private = Snipt(title='Private snipt for Bob', lexer='text', public=False, user=self.bob)
        self.bob_public = Snipt(title='Public snipt for Bob', lexer='text', public=True, user=self.bob)
        self.bob_private.save()
        self.bob_public.save()


    # Private
    def test_get_private_list(self):

        resp = self.api_client.get('/api/private/snipt/', format='json', authentication=self.johnny_auth)

        self.assertHttpOK(resp)
        self.assertValidJSONResponse(resp)
        self.assertEqual(len(self.deserialize(resp)['objects']), 2)

    def test_get_private_detail(self):

        resp = self.api_client.get('/api/private/snipt/{}/'.format(self.johnny_private.pk), format='json', authentication=self.johnny_auth)

        self.assertHttpOK(resp)
        self.assertValidJSONResponse(resp)
        self.assertEqual(self.deserialize(resp)['key'], self.johnny_private.key)

        # Unauthenticated request.
        resp = self.api_client.get('/api/private/snipt/{}/'.format(self.johnny_private.pk), format='json')
        self.assertHttpUnauthorized(resp)

        # Unauthorized request.
        resp = self.api_client.get('/api/private/snipt/{}/'.format(self.johnny_private.pk), format='json', authentication=self.bob_auth)
        self.assertHttpUnauthorized(resp)

    def test_post_private_list(self):

        new_snipt = {
            'title': 'A new private snipt.',
            'lexer': 'text',
            'public': False,
        }

        resp = self.api_client.post('/api/private/snipt/',
                data=new_snipt,
                format='json',
                authentication=self.johnny_auth)
        
        self.assertHttpCreated(resp)
        self.assertEqual(Snipt.objects.count(), 5)

        resp = self.api_client.get('/api/private/snipt/', format='json', authentication=self.johnny_auth)
        self.assertEqual(len(self.deserialize(resp)['objects']), 3)

        resp = self.api_client.get('/api/public/snipt/', format='json')
        self.assertEqual(len(self.deserialize(resp)['objects']), 2)


    # Public
    def test_get_public_list(self):

        self.assertEqual(Snipt.objects.count(), 4)

        resp = self.api_client.get('/api/public/snipt/', format='json')

        self.assertHttpOK(resp)
        self.assertValidJSONResponse(resp)
        self.assertEqual(len(self.deserialize(resp)['objects']), 2)
示例#5
0
class SniptResourceTest(ResourceTestCase):
    fixtures = ["test_entries.json"]

    def setUp(self):
        super(SniptResourceTest, self).setUp()

        # Johnny
        self.johnny = User.objects.create_user('johnny', '*****@*****.**',
                                               'password')
        ApiKey.objects.get_or_create(user=self.johnny)
        self.johnny_auth = self.create_apikey(self.johnny,
                                              self.johnny.api_key.key)
        self.johnny_private = Snipt(title='Private snipt for Johnny',
                                    lexer='text',
                                    public=False,
                                    user=self.johnny)
        self.johnny_public = Snipt(title='Public snipt for Johnny',
                                   lexer='text',
                                   public=True,
                                   user=self.johnny)
        self.johnny_private.save()
        self.johnny_public.save()

        # Bob
        self.bob = User.objects.create_user('bob', '*****@*****.**', 'password')
        ApiKey.objects.get_or_create(user=self.bob)
        self.bob_auth = self.create_apikey(self.bob, self.bob.api_key.key)
        self.bob_private = Snipt(title='Private snipt for Bob',
                                 lexer='text',
                                 public=False,
                                 user=self.bob)
        self.bob_public = Snipt(title='Public snipt for Bob',
                                lexer='text',
                                public=True,
                                user=self.bob)
        self.bob_private.save()
        self.bob_public.save()

    def test_get_private_list(self):

        resp = self.api_client.get(
            "/api/private/snipt/", format="json", authentication=self.johnny_auth
        )

        self.assertHttpOK(resp)
        self.assertValidJSONResponse(resp)
        self.assertEqual(len(self.deserialize(resp)["objects"]), 2)

    def test_get_private_detail(self):

        resp = self.api_client.get(
            "/api/private/snipt/{}/".format(self.johnny_private.pk),
            format="json",
            authentication=self.johnny_auth,
        )

        self.assertHttpOK(resp)
        self.assertValidJSONResponse(resp)
        self.assertEqual(self.deserialize(resp)["key"], self.johnny_private.key)

        # Unauthenticated request.
        resp = self.api_client.get(
            "/api/private/snipt/{}/".format(self.johnny_private.pk), format="json"
        )
        self.assertHttpUnauthorized(resp)

        # Unauthorized request.
        resp = self.api_client.get(
            "/api/private/snipt/{}/".format(self.johnny_private.pk),
            format="json",
            authentication=self.bob_auth,
        )
        self.assertHttpUnauthorized(resp)

    def test_post_private_list(self):

        new_snipt = {
            'title': 'A new private snipt.',
            'lexer': 'text',
            'public': False,
        }

        resp = self.api_client.post(
            "/api/private/snipt/",
            data=new_snipt,
            format="json",
            authentication=self.johnny_auth,
        )

        self.assertHttpCreated(resp)
        self.assertEqual(Snipt.objects.count(), 5)

        resp = self.api_client.get(
            "/api/private/snipt/", format="json", authentication=self.johnny_auth
        )
        self.assertEqual(len(self.deserialize(resp)["objects"]), 3)

        resp = self.api_client.get("/api/public/snipt/", format="json")
        self.assertEqual(len(self.deserialize(resp)["objects"]), 2)

    def test_get_public_list(self):

        self.assertEqual(Snipt.objects.count(), 4)

        resp = self.api_client.get("/api/public/snipt/", format="json")

        self.assertHttpOK(resp)
        self.assertValidJSONResponse(resp)
        self.assertEqual(len(self.deserialize(resp)["objects"]), 2)