Exemplo n.º 1
0
    def get(self, request, *args, **kwargs):
        forms = {
            'venue_form':
            VenueForm(),
            'event_form':
            EventForm(),
            'venue_bookmark_form':
            VenueBookmarkForm(choices=[(o.id, str(o.name))
                                       for o in Venue.objects.all()]),
            'event_bookmark_form':
            EventBookmarkForm(choices=[(o.id, str(o.name))
                                       for o in Event.objects.all()]),
        }
        context = {}
        current_user = request.user
        if current_user.is_authenticated:  # retrieve the user's info to display
            current_user_profile = UserProfile.objects.get(
                user=current_user.id)
            context['profile'] = current_user_profile

            venue_bookmarks = current_user_profile.bookmarked_venues.all()
            event_bookmarks = current_user_profile.bookmarked_event.all()
            owned_venues = current_user_profile.owned_venues.all()
            info = {
                'event_bookmarks': event_bookmarks,
                'venue_bookmarks': venue_bookmarks,
                'owned_venues': owned_venues
            }

            context = {**context, **forms, **info}
            return render(request, 'profile.html', context)
        else:
            return redirect('login')
Exemplo n.º 2
0
    def post(self, request, *args, **kwargs):
        context = {}
        current_user = request.user
        current_user_profile = UserProfile.objects.get(user=current_user.id)
        if 'addVenueBookmark' in request.POST:
            chosen_venue = request.POST['venue']
            #this is horrible but I couldn't figure out how to reinstate the form
            #with the post data as I've added a constructor argument to the form
            #see addVenueForm/addEventForm to see how it should be done
            venue_obj = Venue.objects.get(id=chosen_venue)
            try:
                current_user_profile.bookmarked_venues.add(venue_obj)
            except IntegrityError:
                pass  # in case it's already a bookmark: do nothing
            current_user_profile.save()
            return redirect('profile')
        if 'addEventBookmark' in request.POST:
            chosen_event = request.POST['event']
            event_obj = Event.objects.get(id=chosen_event)
            #see addVenueBookmark
            try:
                current_user_profile.bookmarked_event.add(chosen_event)
            except IntegrityError:
                pass  # in case it's already a bookmark: do nothing
            current_user_profile.save()
            return redirect('profile')
        if 'addVenueForm' in request.POST:
            a_v_form = VenueForm(request.POST, request.FILES)
            if a_v_form.is_valid():
                address = a_v_form.cleaned_data['address']
                address_fr, address_nl, point = Geocoder().geocode(address)

                venue = Venue(
                    name=a_v_form.cleaned_data['name'],
                    point=point,
                    address_fr=address_fr,
                    address_nl=address_nl,
                    description=a_v_form.cleaned_data['description'],
                    image=a_v_form.cleaned_data['image'],
                )
                venue.save()
                current_user_profile.owned_venues.add(venue)

                return redirect('profile')
            else:
                return redirect('profile')
        if 'addEventForm' in request.POST:
            a_e_form = EventForm(request.POST, request.FILES)
            if a_e_form.is_valid():
                price_input = a_e_form.cleaned_data['price']
                venue_object = a_e_form.cleaned_data['venue']
                artists = a_e_form.cleaned_data['artists']
                genres_text = a_e_form.cleaned_data['genres']
                previews = a_e_form.cleaned_data['previews']

                event = Event(
                    name=a_e_form.cleaned_data['name'],
                    venue=venue_object,
                    description=a_e_form.cleaned_data['description'],
                    price=a_e_form.cleaned_data['price'],
                    official_page=a_e_form.cleaned_data['official_page'],
                    datetime=a_e_form.cleaned_data['date'],
                    image=a_e_form.cleaned_data['image'],
                )
                # todo: solve it not saving the time

                # order of saving objects is important here
                # id needs to be created (is done on save) etc..
                event.save()
                for artist in artists.split(','):
                    last_fm_exists = is_artist_on_lastfm(artist)
                    artist_instance = Artist(
                        name=artist, last_fm_entry_exists=last_fm_exists)
                    artist_instance.save()
                    artist_instance.events.add(event)
                for genre in genres_text.split(','):
                    genre_instance = Genre(name=genre[:19])
                    genre_instance.save()
                    event.genres.add(genre_instance)
                for preview in previews.split(','):
                    yt_check = check_preview_for_youtube(preview)
                    if yt_check is None:
                        # preview_instance = Preview(youtube_video_id=preview,type="")
                        pass
                    else:
                        preview_instance = Preview(youtube_video_id=yt_check,
                                                   type="youtube")
                    preview_instance.save()
                    event.previews.add(preview_instance)

                return redirect('profile')

            else:
                return redirect('profile')
        else:
            return redirect('profile')