def save(self, user, story): logging.debug("Adding Post to Experience") if self.cleaned_data['id']: post = StoryLineItem.objects.get(pk=self.cleaned_data['id']) post.text = self.cleaned_data['text'] else: post = StoryLineItem(text=self.cleaned_data['text'], creator=user, story=story) post.save() if self.cleaned_data['lng'] and self.cleaned_data['lat']: location = Point(float(self.cleaned_data['lng']), float(self.cleaned_data['lat'])) pointTag = GeoPointTag(content_object=post, location=location, creator=user) pointTag.save() # add medias if self.cleaned_data["image"]: logging.debug("Adding Post With Image") title = "IPhone - %s"%self.cleaned_data["timestamp"] image = Image(title=title) image.image.save(os.path.basename(story.title+"-"+title), ContentFile(self.cleaned_data["image"].read())) image.save() storylineAttachment = StoryLineItemMedia(storylineitem=post, content_object=image) storylineAttachment.save() logging.debug("Finished Saving Image") return post
def setUp(self): self.user_1 = User.objects.create_user('Chevy Chase', '*****@*****.**', 'chevyspassword') self.profile = Profile(profile_photo='imageurl.png', bio='Lorem ipsum', user=self.user_1) self.image = Image(image='newimageurl.png', name='Test Image', caption='Lorem ipsum', profile=self.profile)
def test_update_caption(self): self.user_1.save() self.profile.save() self.image.save() update_details = { 'image': 'newlink.png', 'name': 'image_name', 'caption': 'some image description', 'profile': self.user_1.profile, } Image.update_caption(update_details, self.image.id) self.updated_image = Image.objects.get( pk=self.image.id) #get new updated image cloudinary_url_prefix = 'http://res.cloudinary.com/dtw9t2dom/image/upload/' self.assertEqual(self.updated_image.image.url, cloudinary_url_prefix + 'newlink.png')
def get(self, request, format=None): group_id = request.query_params.get('group_id') url = 'https://api.flickr.com/services/rest/?method=' \ 'flickr.groups.pools.getPhotos&api_key=' \ '4fb0946eb8e41c0121381f40e772c414&group_id=' \ '{0}&format=json&nojsoncallback=1'.format(group_id) r = requests.get(url) if r.status_code == 200: res = r.json() else: raise Exception('An error has occurred') res_list = [] group, created = Group.objects.get_or_create(user=request.user, group_id=group_id) for image in res['photos']['photo']: image_id = image['id'] title = image['title'] date = arrow.get(image['dateadded']).datetime is_public = True if image['ispublic'] == 1 else False is_friend = True if image['isfriend'] == 1 else False is_family = True if image['isfamily'] == 1 else False # Get URL of the image by image_id image_url = 'https://api.flickr.com/services/rest/?method=' \ 'flickr.photos.getInfo&api_key=' \ '4fb0946eb8e41c0121381f40e772c414&' \ 'photo_id={0}&format=' \ 'json&nojsoncallback=1'.format(image_id) r = requests.get(image_url) if r.status_code == 200: result = r.json() else: raise Exception('An error has occurred') image_url = result['photo']['urls']['url'][0]['_content'] description = result['photo']['description']['_content'] image = Image(group=group, image_id=image_id, title=title, added_date=date, is_public=is_public, is_friend=is_friend, is_family=is_family, image_url=image_url, description=description) res_list.append(image) Image.objects.bulk_create(res_list, batch_size=50) return Response(status=status.HTTP_200_OK)
def iphone_post(request): import base64 data = base64.b64decode(request.POST.get("imageData")) image = Image() image.member = User.objects.get(username="******") image.image.save(os.path.basename("IphoneUpload.jpg"), ContentFile(data)) image.save() # now rotate it according to exif data try: image.rotate_to_exif() except: pass return HttpResponse(status=200, content="sdf")
def store_user(data): # # User # firstname = data['firstname'] if not firstname: firstname = " " lastname = data['lastname'] if not lastname: lastname = " " user = User(username=data['name'], first_name=firstname, last_name=lastname, email=data['email'], password="******"+data["pass"]) user.save(force_insert=True) print "User: "******" " #profile = Profile(user=user, # first_name=firstname, # last_name=lastname, # city=city, # about=data['description']) #profile.save() #account = Account(user=user) #account.save() # avatar # # stories - do them first, since they are containers that we want to add items to # stories = get_stories(data["user_id"]) for story in stories: print "-Story: "+str(story['story_id']) title = story['title'] if not title: title = " " new_story = Story(title=title, slug=slugify(title), creator=user, description=story['text'], mapmode=convert_mapmode(story['mapmode']), creation_date=convert_time(story['timestamp']), update_date=convert_time(story['timestamp']), privacy=get_privacy_of_old_privacy(story['right_view']), status=get_status_of_old_privacy(story['right_view'])) new_story.slug = make_unique(new_story, lambda x: Story.objects.filter(slug__exact=x.slug).count() == 0) new_story.save() migrated_post = MigratedItem(type="story", old_id=story["story_id"], new_id=new_story.pk) migrated_post.save() store_tags_of_item(new_story, story['story_id']) set_hit_count(new_story, story['view_count']) # lines lines = get_lines(data["user_id"]) if lines: for line in lines: print "-Line: "+str(line['line_id']) linestring = make_linestring_of_string(line["controlpoints"], line["start_marker_id"], line["end_marker_id"]) if linestring: line_post = get_post_of_line(line['line_id']) if not line_post: title = " " description = " " else: if line_post['title']: title = line_post['title'] else: title = " " if line_post['text']: description = line_post['text'] else: description = " " new_line = GeoLineTag(creator=user, title=title, description=description, line=linestring, ) # add to story line_w_story = get_story_of_line(line['line_id']) if line_w_story and line_w_story['story_id']: try: new_story = Story.objects.get(pk=get_new_id("story",line_w_story['story_id'])) print "Adding line %s (%s) to story %s (%s)"%(line['line_id'], new_line.id, line_w_story['story_id'], new_story.id) new_line.content_object = new_story except: print "Could not add Story" new_line.save() migrated_line = MigratedItem(type="line", old_id=line["line_id"], new_id=new_line.pk) migrated_line.save() # video videos = get_videos(data["user_id"]) for video in videos: print "-Video: "+str(video['video_id']) if video['medialocation']: import re video_id = re.findall(r"/v/(.+)", video['medialocation'])[0] import_url = "http://www.youtube.com/watch?v="+video_id comment = video['text'] if not comment: comment = " " new_video = Video(creator=user, title=video['title'], comment=comment, was_uploaded=False, import_url=import_url, thumbnail_url=video['thumb1'], safetylevel=get_privacy_of_old_privacy(video['right_view']), last_modified=convert_time(video['timestamp']), date_added=convert_time(video['timestamp']), ) new_video.save() migrated_video = MigratedItem(type="video", old_id=video["video_id"], new_id=new_video.pk) migrated_video.save() # add to story if video["story_id"]: story = Story.objects.get(pk=get_new_id("story", video["story_id"])) count = StoryLineItem.objects.filter(creator=user, story=story, timestamp_start__lte=convert_time(video['timestamp'])).count() print "Adding video %s (%s) to story %s (%s) at position: %s "%(video['video_id'], new_video.id, video["story_id"], story.id, count) text = video['text'] if not text: text = " " story_line_item = StoryLineItem(creator=user, story=story, text=text, timestamp_start=convert_time(video['timestamp']), timestamp_end=convert_time(video['timestamp'])) story_line_item.save() story_line_item_media = StoryLineItemMedia(storylineitem=story_line_item, content_object=new_video) story_line_item_media.save() # geotag if video['mapobject_id']: geotag_item(video["video_id"], story_line_item, user) if video['mapobject_id']: geotag_item(video["video_id"], new_video, user) store_tags_of_item(new_video, video["video_id"]) set_hit_count(new_video, video['view_count']) # posts posts = get_posts(data["user_id"]) for post in posts: print "-Post: "+str(post['post_id']) if post['story_id']: post_text = post['text'] if not post_text: post_text = " " try: post_story = Story.objects.get(pk=get_new_id("story", post['story_id'])) count = StoryLineItem.objects.filter(creator=user, story=story, timestamp_start__lte=convert_time(post['timestamp'])).count() print "Adding post %s to story %s (%s) at position: %s "%(post['post_id'], post["story_id"], post_story.id, count) new_post = StoryLineItem(creator=user, story=post_story, text=post_text, timestamp_start=convert_time(post['timestamp']), timestamp_end=convert_time(post['timestamp'])) new_post.save() migrated_post = MigratedItem(type="post", old_id=post["post_id"], new_id=new_post.pk) migrated_post.save() if post['mapobject_id']: geotag_item(post["post_id"], new_post, user) store_tags_of_item(new_post, post["post_id"]) set_hit_count(new_post, post['view_count']) except: print "Could not add to Story" # images images = get_images(data["user_id"]) if images: for image in images: print "-Image: "+str(image['image_id']) caption = image['text'] if not caption: caption = " " title = image['title'] if not title: title = " " new_image = Image(member=user, title=title, caption=caption, date_added=convert_time(image['timestamp']), ) try: new_image.image.save(os.path.basename(image["filename"]+".jpg"), ContentFile(open(path_to_images+image["filename"]+".jpg", "r").read())) migrated_image = MigratedItem(type="image", old_id=image["image_id"], new_id=new_image.pk) new_image.save() migrated_image.save() if image["story_id"]: story = Story.objects.get(pk=get_new_id("story", image["story_id"])) count = StoryLineItem.objects.filter(creator=user, story=story, timestamp_start__lte=convert_time(image['timestamp'])).count() print "Adding image %s (%s) to story %s (%s) at position: %s "%(image['image_id'], new_image.id, image["story_id"], story.id, count) story_line_item = StoryLineItem(creator=user, story=story, text=caption, position=count, timestamp_start=convert_time(image['timestamp']), timestamp_end=convert_time(image['timestamp'])) story_line_item.save() story_line_item_media = StoryLineItemMedia(storylineitem=story_line_item, content_object=new_image) story_line_item_media.save() if image['mapobject_id']: geotag_item(image['image_id'], story_line_item, user) if image['mapobject_id']: geotag_item(image['image_id'], new_image, user) store_tags_of_item(new_image, image["image_id"]) set_hit_count(new_image, image['view_count']) except: print "Could not store image: %s filename: %s"%(image['image_id'], image['filename']) # story image # store user migrated_user = MigratedItem(type="user", old_id=data["user_id"], new_id=user.pk) migrated_user.save() make_avatar(data) # flickr images # TODO:
class ImageTestClass(TestCase): '''Test methods of Image model''' def setUp(self): self.user_1 = User.objects.create_user('Chevy Chase', '*****@*****.**', 'chevyspassword') self.profile = Profile(profile_photo='imageurl.png', bio='Lorem ipsum', user=self.user_1) self.image = Image(image='newimageurl.png', name='Test Image', caption='Lorem ipsum', profile=self.profile) def tearDown(self): self.image.delete() self.profile.delete() self.user_1.delete() def test_save_image(self): self.user_1.save() self.profile.save() self.image.save() images = Image.objects.all() self.assertTrue(len(images) > 0) def test_update_caption(self): self.user_1.save() self.profile.save() self.image.save() update_details = { 'image': 'newlink.png', 'name': 'image_name', 'caption': 'some image description', 'profile': self.user_1.profile, } Image.update_caption(update_details, self.image.id) self.updated_image = Image.objects.get( pk=self.image.id) #get new updated image cloudinary_url_prefix = 'http://res.cloudinary.com/dtw9t2dom/image/upload/' self.assertEqual(self.updated_image.image.url, cloudinary_url_prefix + 'newlink.png') def test_delete_image(self): self.user_1.save() self.profile.save() self.image.save() self.image2 = Image.objects.get(pk=self.image.id) self.image2.delete_image() images = Image.objects.all() self.assertTrue(images.count() == 0)
def module_create(request): if request.method == 'POST': module_form = ModuleCreateForm(request.POST) if module_form.is_valid(): module_type = module_form.cleaned_data['module_type'] module = module_form.save(commit=False) user = Profile.objects.get(user=request.user) print(f'We have a module! {module}') print(f'Type is: \'{module_type}\'') #extended_form = None # NOTE: this is also temporary, use actual module ids extended_form = None extended_module = None if str(module_type) == 'Datetime': extended_form = DateForm(request.POST) elif str(module_type) == 'Forecast': extended_form = ForecastForm(request.POST) elif str(module_type) == 'Photos': extended_form = PhotosForm(request.POST) formset = ImageFormSet(request.POST, request.FILES, queryset=Image.objects.none()) if extended_form.is_valid() and formset.is_valid(): extended_module = extended_form.save(commit=False) for form in formset.cleaned_data: # prevent crashing if the user doesn't upload all the photos if form: image_form = form['image'] public = form['public'] image = Image(owner=user, photos_module=extended_module, image=image_form, public=public) image.save() elif str(module_type) == 'Weather': extended_form = WeatherForm(request.POST) else: pass # Temporary save the module if it wasn't saved above if extended_form.is_valid() and extended_module is None: extended_module = extended_form.save(commit=False) # Save all data from the form if extended_form.is_valid(): module.owner = user module.save() extended_module.module = module extended_module.save() # TODO: complete this module_style = get_template('dashboard/includes/module_style.html') module_content = get_template('dashboard/includes/module_content.html') page_content = get_content(request, module) update_method = get_update_method(module) module_script = get_template('dashboard/includes/module_script.html') context = { 'id': module.id, 'type': module.module_type.module_type, # NOTE: adding a style tag to head may not work, may need to put this as a 'style' within the div # it seems to work, however 'style': module_style.render({ 'id': module.id, 'type': module.module_type.module_type, 'values': { 'z-index': module.z_index, 'top': module.y, 'left': module.x, 'color': module.text_color, }, }), # Module's style 'content': module_content.render({ 'id': module.id, 'type': module.module_type.module_type, 'content': page_content['content'], 'moveable': page_content['moveable'], 'ajax': True, 'values': { 'zindex': module.z_index, 'top': module.y, 'left': module.x, 'color': module.text_color, } }), # Module's div 'values': { 'zindex': module.z_index, 'top': module.y, 'left': module.x, 'color': module.text_color, 'moveable': page_content['moveable'], }, 'script': module_script.render({ }), # Module's script 'method': update_method, } return JsonResponse(context) ''' if dt_form.is_valid(): dt = dt_form.save(commit=False) module.owner = user module.save() dt.module = module dt.save() print(f'We have DT module! {dt}') else: print('Form was not valid') elif str(module_type) == 'Forecast': forecast_form = ForecastForm(request.POST) if forecast_form.is_valid(): forecast = forecast_form.save(commit=False) module.owner = user module.save() forecast.module = module forecast.save() print(f'We have Forecast module! {forecast}') else: print('Form was not valid') elif str(module_type) == 'Photos': photos_form = PhotosForm(request.POST)#, module=module)#, instance=module) formset = ImageFormSet(request.POST, request.FILES, queryset=Image.objects.none()) if photos_form.is_valid() and formset.is_valid(): photos = photos_form.save(commit=False) module.owner = user module.save() photos.module = module photos.save() print(f'We have Photos module! {photos}') for form in formset.cleaned_data: # prevent crashing if the user doesn't upload all the photos if form: image_form = form['image'] public = form['public'] image = Image(owner=user, photos_module=photos, image=image_form, public=public) image.save() elif str(module_type) == 'Weather': weather_form = WeatherForm(request.POST)#, module=module)#, instance=module) if weather_form.is_valid(): weather = weather_form.save(commit=False) module.owner = user module.save() weather.module = module weather.save() print(f'We have Weather module! {weather}') else: # TODO: what do? pass ''' #return redirect('user-modules') # Can also redirect to an object's get_absolute_url() else: module_form = ModuleCreateForm() context = { 'module_form': module_form } template = get_template('dashboard/add_module.html') return JsonResponse({'content': template.render(context, request=request)})