Esempio n. 1
0
    def post(self, request):
        form = self.form_class(request.POST)
        if form.is_valid():
            try:
                request.FILES['myfile']
            except:
                messages.error(request, 'Please select a file.')
                return HttpResponseRedirect("/diary/create_page")

            username = self.request.user.username
            diary = Diary.objects.filter(username=username)

            error = self.__check_title(form, diary)

            if (error is not None):
                messages.error(request, error)
                return HttpResponseRedirect("/diary/create_page")

            page = form.save()
            imgurUtil = ImgurUtil()
            my_file = request.FILES['myfile']
            page.diary = diary[0]
            page.date = str(datetime.date.today())
            description = str(page.diary) + ':' + \
                str(page.id) + ':' + page.date
            response = imgurUtil.upload_image_locally(description, my_file)
            if (response.status_code == requests.codes.ok):
                uploader_url = response.json()["data"]["link"]
                page.picture = uploader_url
                page.save()

            else:
                messages.error(request, 'Please select a valid file.')
                return HttpResponseRedirect("/diary/create_page")
        return HttpResponseRedirect("/diary/")
Esempio n. 2
0
    def test_05_get_all_albums(self):
        """
        Test get all album hashes for further use in the test.
        """

        imgurUtil = ImgurUtil()
        response = imgurUtil.get_all_albums_info()
        self.assertEqual(response['status'], 200)
Esempio n. 3
0
    def test_01_create_album(self):
        """
        Create a temporary album for testing.
        """

        imgurUtil = ImgurUtil()
        response = imgurUtil.create_album('test_only')
        self.assertEqual(response.status_code, 200)
Esempio n. 4
0
    def test_10_delete_album(self):
        """
        Test delete an album.
        """

        imgurUtil = ImgurUtil()
        album_title = 'test_only'
        response = imgurUtil.delete_album(album_title)
        self.assertEqual(response.status_code, 200)
Esempio n. 5
0
 def delete(self, request, *args, **kwargs):
     """
     Function to delete picture from database and imgur.
     """
     imgurUtil = ImgurUtil()
     page = self.get_object()
     description = str(page.diary) + ':' + str(page.id) + ':' + page.date
     image_hash = imgurUtil.get_image_hash(description)
     imgurUtil.delete_image(image_hash)
     page.delete()
     return HttpResponseRedirect('/diary/')
Esempio n. 6
0
    def test_04_create_many_albums(self):
        """
        Create many albums for testing.
        """

        imgurUtil = ImgurUtil()
        response1 = imgurUtil.create_album('album1')
        response2 = imgurUtil.create_album('album2')
        response3 = imgurUtil.create_album('album3')
        self.assertEqual(response1.status_code, 200)
        self.assertEqual(response2.status_code, 200)
        self.assertEqual(response3.status_code, 200)
Esempio n. 7
0
    def test_11_delete_multiple_albums(self):
        """
        Test delete many albums.
        """

        imgurUtil = ImgurUtil()
        albumTitle_1 = 'album1'
        albumTitle_2 = 'album2'
        albumTitle_3 = 'album3'
        response1 = imgurUtil.delete_album(albumTitle_1)
        response2 = imgurUtil.delete_album(albumTitle_2)
        response3 = imgurUtil.delete_album(albumTitle_3)
        self.assertEqual(response1.status_code, 200)
        self.assertEqual(response2.status_code, 200)
        self.assertEqual(response3.status_code, 200)
Esempio n. 8
0
    def test_12_simple_upload_process(self):
        """
        Test whole simple upload process
        """

        imgurUtil = ImgurUtil()
        album_title = 'test_2'
        create_album_response = imgurUtil.create_album(album_title)
        album_hash = imgurUtil.get_album_hash(album_title)
        imgurUtil.set_album_hash(album_hash)
        image_link = 'https://instagram.fbkk10-1.fna.fbcdn.net/vp/32fca421863cca50988741b338de6165/5CAFF2AE/t51.2885-19/s320x320/45476686_1153987844754206_6755756503161896960_n.jpg'
        upload_response = imgurUtil.upload_image('test', image_link)
        hashes = imgurUtil.get_image_hash('test')
        delete_image_response = imgurUtil.delete_image(hashes)
        delete_album_response = imgurUtil.delete_album(album_title)
        self.assertEqual(create_album_response.status_code, 200)
        self.assertEqual(upload_response.status_code, 200)
        self.assertEqual(delete_image_response.status_code, 200)
        self.assertEqual(delete_album_response.status_code, 200)
Esempio n. 9
0
    def test_09_delete_multiple_image(self):
        """
        Test deleting many pictures.
        """

        imgurUtil = ImgurUtil()
        album_hash = imgurUtil.get_album_hash('test_only')
        imgurUtil.set_album_hash(album_hash)
        hashes2 = imgurUtil.get_image_hash('temp2')
        hashes3 = imgurUtil.get_image_hash('temp3')
        hashes4 = imgurUtil.get_image_hash('temp4')
        response1 = imgurUtil.delete_image(hashes2)
        response2 = imgurUtil.delete_image(hashes3)
        response3 = imgurUtil.delete_image(hashes4)
        self.assertEqual(response1.status_code, 200)
        self.assertEqual(response2.status_code, 200)
        self.assertEqual(response3.status_code, 200)
Esempio n. 10
0
    def test_08_delete_single_image(self):
        """
        Test deleting a picture.
        """

        imgurUtil = ImgurUtil()
        album_hash = imgurUtil.get_album_hash('test_only')
        imgurUtil.set_album_hash(album_hash)
        hashes = imgurUtil.get_image_hash('temp')
        response = imgurUtil.delete_image(hashes)
        self.assertEqual(response.status_code, 200)
Esempio n. 11
0
    def test_02_single_upload(self):
        """
        Test if single upload success.
        """

        imgurUtil = ImgurUtil()
        album_hash = imgurUtil.get_album_hash('test_only')
        imgurUtil.set_album_hash(album_hash)
        image_link = 'https://instagram.fbkk10-1.fna.fbcdn.net/vp/32fca421863cca50988741b338de6165/5CAFF2AE/t51.2885-19/s320x320/45476686_1153987844754206_6755756503161896960_n.jpg'
        response = imgurUtil.upload_image('temp', image_link)
        self.assertEqual(response.status_code, 200)
Esempio n. 12
0
    def test_06_get_single_image(self):
        """
        Test getting an single image from test_album.
        """

        imgurUtil = ImgurUtil()
        album_title = 'test_album'
        album_hash = imgurUtil.get_album_hash(album_title)
        imgurUtil.set_album_hash(album_hash)
        response = imgurUtil.get_image_description('Catterpillar')
        description = 'Catterpillar'
        self.assertEqual(response, description)
Esempio n. 13
0
    def get_queryset(self):
        """
        Get the queryset to look an object up against.
        """
        username = self.request.user.username
        diaries = Diary.objects.filter(username=username)
        page = Page.objects.all()
        imgurUtil = ImgurUtil()

        query = self.request.GET.get("q")
        if diaries == page[0].diary:
            if query:
                return page.filter(Q(title__icontains=query)).distinct()

        if len(diaries) == 0:
            Diary.objects.create(username=username)

        return Page.objects.filter(diary__username=username)
Esempio n. 14
0
    def test_03_multiple_upload(self):
        """
        Test if multiple upload success.
        """

        imgurUtil = ImgurUtil()
        album_hash = imgurUtil.get_album_hash('test_only')
        imgurUtil.set_album_hash(album_hash)
        image1_link = 'https://instagram.fbkk2-2.fna.fbcdn.net/vp/a7af227dd8e1cf5b352e6bf4b87cefba/5C7A4F55/t51.2885-19/s320x320/42437542_2213514032250422_4515480103911686144_n.jpg'
        image2_link = 'https://instagram.fbkk22-1.fna.fbcdn.net/vp/e8379b291ba024e9d8872ffc8de465d5/5C9493DE/t51.2885-19/s320x320/22794246_352255698554015_5072593132353224704_n.jpg'
        image3_link = 'https://instagram.fbkk2-2.fna.fbcdn.net/vp/1f1cf2cea82f1d792ca848f7b5a4597c/5C7562C1/t51.2885-19/s320x320/44392027_1870453886342960_1282978848111067136_n.jpg'
        response1 = imgurUtil.upload_image('temp2', image1_link)
        response2 = imgurUtil.upload_image('temp3', image2_link)
        response3 = imgurUtil.upload_image('temp4', image3_link)
        self.assertEqual(response1.status_code, 200)
        self.assertEqual(response2.status_code, 200)
        self.assertEqual(response3.status_code, 200)
Esempio n. 15
0
    def test_07_get_multiple_image(self):
        """
        Test getting multiple image from test_album.
        """

        imgurUtil = ImgurUtil()
        album_title = 'test_album'
        album_hash = imgurUtil.get_album_hash(album_title)
        imgurUtil.set_album_hash(album_hash)

        des1 = 'Catterpillar'
        image1_des = imgurUtil.get_image_description(des1)

        des2 = 'Sea and mountain'
        image2_des = imgurUtil.get_image_description(des2)

        des3 = 'Catty'
        image3_des = imgurUtil.get_image_description(des3)

        self.assertEqual(image1_des, des1)
        self.assertEqual(image2_des, des2)
        self.assertEqual(image3_des, des3)
Esempio n. 16
0
    def test_13_multiple_upload_process(self):
        """
        Test multiple simple upload process
        """

        imgurUtil = ImgurUtil()
        album_title = 'test_3'
        create_album_response = imgurUtil.create_album(album_title)
        album_hash = imgurUtil.get_album_hash(album_title)
        imgurUtil.set_album_hash(album_hash)

        image_link1 = 'https://instagram.fbkk1-1.fna.fbcdn.net/vp/2e312db571e6a0b2eaaba201f54010b5/5C84466C/t51.2885-19/s320x320/26071653_402875793494059_4400556558711259136_n.jpg'
        upload_response1 = imgurUtil.upload_image('test1', image_link1)
        hashes1 = imgurUtil.get_image_hash('test1')

        image_link2 = 'https://instagram.fbkk1-1.fna.fbcdn.net/vp/dca8a4e0ef1508ef6fcc0b9046d6261a/5C896EAC/t51.2885-19/s320x320/43700894_2233080543604526_24320505716670464_n.jpg'
        upload_response2 = imgurUtil.upload_image('test2', image_link2)
        hashes2 = imgurUtil.get_image_hash('test2')

        image_link3 = 'https://instagram.fbkk1-1.fna.fbcdn.net/vp/dde581ed788603a79d4d3f73a40bc132/5C8846B6/t51.2885-19/s320x320/43661432_2011354002276624_8575559070172315648_n.jpg'
        upload_response3 = imgurUtil.upload_image('test3', image_link3)
        hashes3 = imgurUtil.get_image_hash('test3')

        delete_image_response1 = imgurUtil.delete_image(hashes1)
        delete_image_response2 = imgurUtil.delete_image(hashes2)
        delete_image_response3 = imgurUtil.delete_image(hashes3)

        delete_album_response = imgurUtil.delete_album(album_title)
        self.assertEqual(create_album_response.status_code, 200)

        self.assertEqual(upload_response1.status_code, 200)
        self.assertEqual(upload_response2.status_code, 200)
        self.assertEqual(upload_response3.status_code, 200)

        self.assertEqual(delete_image_response1.status_code, 200)
        self.assertEqual(delete_image_response2.status_code, 200)
        self.assertEqual(delete_image_response3.status_code, 200)

        self.assertEqual(delete_album_response.status_code, 200)