def add_sample_album(album_sample_data, user_sample_data): album = Album() album.name = album_sample_data['name'] album.start_date = album_sample_data['start_date'] album.end_date = album_sample_data['end_date'] user_profile = UserProfile.objects.get(user__username=user_sample_data['username']) album.user_profile = user_profile album.save()
def add_sample_album(album_sample_data, user_sample_data): album = Album() album.name = album_sample_data['name'] album.start_date = album_sample_data['start_date'] album.end_date = album_sample_data['end_date'] user_profile = UserProfile.objects.get( user__username=user_sample_data['username']) album.user_profile = user_profile album.save()
def create_or_update_album(request, operation, album_id): """ Creates or updates an album based on the provided operation. :param request: HttpRequest instance received by the view :param operation: Takes two values: 'create' or 'update' :param album_id: Id of the album that needs to be updated. Id will be ignored if operation = 'create' :return: Dict containing an error message or a redirection url. If no errors occur, then this dict is empty """ user = request.user return_data = {} if user.is_authenticated(): post_data = request.POST name = post_data.get('name', "") url_name = re.sub(r'\s', '_', name) start_date = post_data.get('start_date', "") end_date = post_data.get('end_date', "") cover_picture = request.FILES.get('cover_picture', None) # validate album data album_to_update = None user_profile = UserProfile.objects.get(user=user) if operation == 'create': error = validate_album_form(operation, user_profile, name, start_date, end_date, None, cover_picture) else: album_to_update = Album.objects.get(id=album_id) error = validate_album_form(operation, user_profile, name, start_date, end_date, album_to_update, cover_picture) if error is not None: return_data['error'] = error else: # convert string dates to datetime.date start_date = convert_string_to_date(start_date) end_date = convert_string_to_date(end_date) # one final check to confirm if end_date comes after start_date if (end_date - start_date).days < 0: return_data['error'] = "End date must come after Start date" else: if operation == 'create': # create new album new_album = Album() new_album.user_profile = user_profile new_album.name = name new_album.url_name = url_name new_album.start_date = start_date new_album.end_date = end_date if cover_picture is not None: new_album.cover_picture = cover_picture new_album.save() else: # update existing album album_to_update.name = name album_to_update.url_name = url_name album_to_update.start_date = start_date album_to_update.end_date = end_date if cover_picture is not None: album_to_update.cover_picture = cover_picture album_to_update.save() else: return_data['redirect_to'] = "/mytravelog/sign_in/" return return_data