def edit_invite(request, pk, operation): """ Accepting or declining the event invitation. """ user_profile = get_users_profile(request.user.id) if user_profile.premium: if operation == 'accept': Event.participant_accepted(pk, request.user) return redirect('event', pk) if operation == 'decline': return redirect('profile') else: return redirect('premium_info')
def view_invite(request, pk): """ View the invite page where the user can accept or decline the request. """ user_profile = get_users_profile(request.user.id) if user_profile.premium: context = { 'event': get_object_or_404(Event, pk=pk), 'user_profile': user_profile, } return render(request, 'event/view_invite.html', context) else: return redirect('premium_info')
def news_feed(request): """ Will show users all their friends posts """ all_friends = get_all_friends(request) news_feed = get_news_feed(request) user_profile = get_users_profile(request.user.id) context = { 'news_feed': news_feed, 'user_profile': user_profile, 'status_form': StatusForm, } return render(request, 'status/news_feed.html', context)
def test_like_notification_method_returns_username(self): user_one = create_test_user('1') user_two = create_test_user('2') user_profile = get_users_profile(user_one.id) status = Status( user=user_one, user_profile=user_profile, title='Test title', content='Test content', ) like_notification = LikeNotification( user=user_one, status=status, liker=user_two, ) self.assertEqual(str(like_notification), like_notification.user.username)
def test_comment_notification_method_returns_status_title(self): user_one = create_test_user('1') user_two = create_test_user('2') user_profile = get_users_profile(user_one.id) status = Status( user=user_one, user_profile=user_profile, title='Test title', content='Test content', ) comment_notification = CommentNotification( user=user_one, status=status, commenter=user_two, ) self.assertEqual(str(comment_notification), comment_notification.status.title)
def event(request, pk): """ View the selected event and create messages on the page for only attendees to see. """ user_profile = get_users_profile(request.user.id) if user_profile.premium: event = Event.objects.get(pk=pk) event = add_count_down_to_events([event]) context = { 'event': event[0], 'user_profile': user_profile, } return render(request, 'event/event.html', context) else: return redirect('premium_info')
def update_status(request, operation, pk, redirect_user): """ Add the users status to the database """ # Add the status to the database, else remove the status from the database if operation == 'add': form = StatusForm(request.POST, request.FILES) if form.is_valid(): post = form.save(commit=False) post.user = request.user post.user_profile = get_users_profile(request.user.id) post.save() return redirect('news_feed') elif operation == 'remove': status = get_object_or_404(Status, pk=pk) status.delete() if redirect_user == 'profile': return redirect('profile') if redirect_user == 'news_feed': return redirect('news_feed')
def get_current_conversation_dict(request): conversations_from = Message.objects.filter(sent_from=request.user) conversations_to = Message.objects.filter(sent_to=request.user) current_conversations_with_users = [] for conversation in conversations_from: current_conversations_with_users.append(conversation.sent_to) for conversation in conversations_to: current_conversations_with_users.append(conversation.sent_from) current_conversations = [] for index, conversation in enumerate(current_conversations_with_users): if index == 0: current_conversations.append(conversation) else: new_conversation = True for list_item in current_conversations: if list_item == conversation: new_conversation = False if new_conversation: current_conversations.append(conversation) current_conversation_dicts = [] for conversation in current_conversations: user_profile = get_users_profile(conversation.id) conversation_dict = { 'first_name': conversation.first_name, 'last_name': conversation.last_name, 'id': conversation.id, 'profile_image': user_profile.profile_image, } current_conversation_dicts.append(conversation_dict) return current_conversation_dicts
def menu(request): """ View all the users events, created or invited. """ user_profile = get_users_profile(request.user.id) if user_profile.premium: # Get all events the users has been invited to. all_events = Event.objects.all() events = [] for event in all_events: if request.user in event.participants.all(): events.append(event) events = add_count_down_to_events(events) context = { 'events': events, 'user_profile': user_profile, } return render(request, 'event/menu.html', context) else: return redirect('premium_info')
def quick_item(request, item, category): """ Add or remove an item from the database returning json """ item_name = item.capitalize() quantity = 1 category_selected = Category.objects.get(category=category, user=request.user) # Get users items from the database. users_items = Item.objects.filter(user=request.user) user_profile = get_users_profile(request.user.id) try: item_in_database = Item.objects.get(item=item_name, user=request.user) item_in_database.quantity += quantity item_in_database.save() new_item = False except: new_item = True # Add item if new if new_item: new_item = Item( user=request.user, item=item_name, quantity=quantity, category=category_selected, ) new_item.save() purchase_item = PurchasedItems( user=request.user, item=item_name, quantity=quantity, category=category_selected, ) purchase_item.save() try: favorite = Favorite.objects.get(item=item_name, user=request.user) favorite.quantity += quantity favorite.save() except: favorite_item = Favorite( user=request.user, item=item_name, quantity=quantity, category=category, ) favorite_item.save() shopping_partners_list = get_shopping_partners(request) all_items = all_shopping_items(request) categories = Category.objects.filter(user=request.user) categories_used = find_categories_used_dict(request) all_items_no_duplicates = add_items_quantity_not_duplicates(request) all_items_no_duplicates = sorted(all_items_no_duplicates, key=lambda x: x['item'], reverse=True) return JsonResponse({ 'items': all_items_no_duplicates, 'categories_used': categories_used, })
def insight(request, filter): """ Display instight page with graphs and table of the users favorite items and shopping habbits. """ user_profile = get_users_profile(request.user.id) if user_profile.premium: if filter == 'personal': # Get all users favorite items and order them by quantity favorites = Favorite.objects.filter( user=request.user).order_by('-quantity') # get purchsed items form the oldest first purchased_items = PurchasedItems.objects.filter( user=request.user).order_by('created_date') # Get all purchased items all_purchased_items = PurchasedItems.objects.filter( user=request.user).order_by('-created_date') if filter == 'group': # Get users shopping partners partners = get_shopping_partners(request) # Append the current users and their shopping partners favorite into a list favorites = get_favorites_from_user_and_partners(request) favorites_list = favorites favorites = [] for index, favorite in enumerate(favorites_list): if index == 0: favorites.append(favorite) else: new_item = True for list_item in favorites: if list_item.item == favorite.item: list_item.quantity += favorite.quantity new_item = False if new_item: favorites.append(favorite) favorites = sorted(favorites, key=lambda x: x.quantity, reverse=True) # Append the current users purchased items and their shopping partners items together in a list purchased_items = [] current_users_purchased_items = PurchasedItems.objects.filter( user=request.user).order_by('-created_date') for purchased in current_users_purchased_items: purchased_items.append(purchased) for partner in partners: partners_purchased_items = PurchasedItems.objects.filter( user=partner).order_by('-quantity') for item in partners_purchased_items: purchased_items.append(item) all_purchased_items = sorted(purchased_items, key=lambda x: x.created_date, reverse=True) # top five chart data all_favorites = favorites chart_data_top_five = [] chart_labels_top_five = [] for fav_index, favorite in enumerate(all_favorites): if fav_index < 5: chart_labels_top_five.append(favorite.item) chart_data_top_five.append(favorite.quantity) # all favorites for chart data all_favorites = favorites chart_data = [] chart_labels = [] for favorite in all_favorites: chart_labels.append(favorite.item) chart_data.append(favorite.quantity) # dataset for the line chart monthly_report_dates = [] months = [ 'January', 'Febuary', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ] users_start_date = date(request.user.date_joined.year, request.user.date_joined.month, request.user.date_joined.day) todays_date = date.today() time_between = todays_date - users_start_date months_between_start_date_and_now = [] for index_day in range(time_between.days + 1): day = users_start_date + timedelta(days=index_day) months_between_start_date_and_now.append(str(day)[0:7]) months_between_start_date_and_now = list( dict.fromkeys(months_between_start_date_and_now)) months_between_start_date_and_now_as_dates = [] monthly_report_dates_number = [] monthly_report_dates = [] for month in months_between_start_date_and_now: months_between_start_date_and_now_as_dates.append(month) monthly_report_dates_number.append(str(month)[5:7]) for month in monthly_report_dates_number: monthly_report_dates.append(months[int(month) - 1]) monthly_report_data = [] # find the name of each item used and sort in in used_items used_items = [] for item in purchased_items: used_items.append(item.item) used_items = list(dict.fromkeys(used_items)) # get the users account user_profile = UserProfile.objects.get(user=request.user) for index, item in enumerate(used_items): quantity_of_item_purchased_each_month = [] for month in months_between_start_date_and_now_as_dates: month_quantity = 0 for purchased_item in purchased_items: if purchased_item.item == item and str( purchased_item.created_date)[0:7] == month: month_quantity += 1 quantity_of_item_purchased_each_month.append(month_quantity) item_dict = { 'item': item, 'quantity': quantity_of_item_purchased_each_month, } monthly_report_data.append(item_dict) line_chart_dataset = format_report_data_for_line_graph( monthly_report_data) context = { 'purchased_items': all_purchased_items, 'favorites': favorites, 'chart_data_top_five': chart_data_top_five, 'chart_labels_top_five': chart_labels_top_five, 'chart_data': chart_data, 'chart_labels': chart_labels, 'line_chart_dataset': line_chart_dataset, 'monthly_report_dates': monthly_report_dates, 'monthly_report_data': monthly_report_data, 'user_profile': user_profile, } return render(request, 'shopping/insight.html', context) else: return redirect('premium_info')
def oauth_2_call_back(request): user_profile = get_users_profile(request.user.id) if user_profile.premium: SCOPES = ['https://www.googleapis.com/auth/calendar'] state = request.session['state'] config = json.loads(os.environ['CRED']) flow = google_auth_oauthlib.flow.Flow.from_client_config(config, SCOPES, state=state) flow.redirect_uri = 'https://family-organiser.herokuapp.com/event/oauth_2_call_back/' authorization_response = request.get_full_path() flow.fetch_token(authorization_response=authorization_response) credentials = flow.credentials service = build('calendar', 'v3', credentials=credentials) event_date = request.session['event']['date'] event_start_time = request.session['event']['start_time'] event_end_time = request.session['event']['end_time'] event = { 'summary': request.session['event']['title'], 'location': request.session['event']['location'], 'description': request.session['event']['description'], 'start': { 'dateTime': '{}T00:00:00-{}'.format(event_date, event_start_time), 'timeZone': "Europe/London", }, 'end': { 'dateTime': '{}T00:00:00-{}'.format(event_date, event_end_time), 'timeZone': "Europe/London", }, 'attendees': [ { 'email': '*****@*****.**' }, { 'email': '*****@*****.**' }, ], 'reminders': { 'useDefault': False, 'overrides': [ { 'method': 'email', 'minutes': 24 * 60 }, { 'method': 'popup', 'minutes': 10 }, ], }, } event = service.events().insert(calendarId='primary', body=event).execute() calendar_link = event.get('htmlLink') return redirect('calendar_confirmed') else: return redirect('premium_info')