def clear_notification(request, notification_pk): try: notification = Notification.objects.get( user=request.user, pk=notification_pk) notification.cleared = True notification.save() return json_response() except Notification.DoesNotExist: return json_response(status=400)
def clear_notification(request, notification_pk): try: notification = Notification.objects.get(user=request.user, pk=notification_pk) notification.cleared = True notification.save() return json_response() except Notification.DoesNotExist: return json_response(status=400)
def attendance_search(request, max_results=20): """Return a JSON response of members based on search for name. The search uses the "searchTerm" post parameter. Return up to max_results number of results. The results only include people who have not attended the event specified by the post parameter eventPK. """ search_query = request.GET['searchTerm'] event_pk = request.GET['eventPK'] event = Event.objects.get(pk=event_pk) # Get all users who did not attend this event: # TODO(sjdemartini): Properly filter for members, instead of just getting # all users who are not officers or candidates (as these other users may # include company users, etc.) members = user_model.objects.exclude( eventattendance__event=event).select_related('userprofile') # A list of entries for each member that matches the search query: member_matches = [] # Parse the search query into separate pieces if the query includes # whitespace search_terms = search_query.lower().split() for member in members: name = member.userprofile.get_verbose_full_name() name_lower = name.lower() if all(search_term in name_lower for search_term in search_terms): pic_html = render_to_string('_user_thumbnail.html', {'user_profile': member.userprofile}) entry = {'label': name, 'value': member.pk, 'picture': pic_html} member_matches.append(entry) if len(member_matches) >= max_results: break return json_response(data=member_matches)
def assign_house(request): """Assign an officer or candidate to a house for the current semester. The user is specified by a userPK post parameter, the house is specified by a houseName post parameter, and the term is specified by a term post parameter which is the url name of the display term. """ user_pk = request.POST.get('userPK') user = get_user_model().objects.get(pk=user_pk) house_name = request.POST.get('houseName') house = House.objects.get(mailing_list=house_name) term = Term.objects.get_by_url_name(request.POST.get('term')) house_member, created = HouseMember.objects.get_or_create( user=user, term=term, defaults={'house': house}) # Find out if the user is a house leader for setting is_leader correctly user_house_leader = get_object_or_none( Officer, position__short_name='house-leaders', user__id=user_pk, term=term) if user_house_leader is not None: # Find out if there is already a house leader for this house existing_house_leader = get_object_or_none( HouseMember, house=house, is_leader=True, term=term) if existing_house_leader is not None: # If there is already a house leader in that house and we're trying # to add a house leader, delete any newly created HouseMember object # and return a 400 error to be handled by jQuery if created: house_member.delete() return json_response(status=400) else: house_member.is_leader = True # If an object was gotten, the user was in a different house previously if not created: house_member.house = house house_member.save() return json_response()
def event_unsignup(request, event_pk): """Handles the action of un-signing up for events.""" try: event = Event.objects.get(pk=event_pk) except: return json_response(status=400) success_msg = 'Unsignup successful.' if request.user.is_authenticated(): try: # Try to get a signup object for this user signup = EventSignUp.objects.get(event=event, user=request.user) signup.user = request.user # Set the signup as "unsigned up" signup.unsignup = True signup.save() messages.success(request, success_msg) except EventSignUp.DoesNotExist: # If a signup could not be found, ignore this, since the user # is not signed up for the event pass else: email = request.POST.get('email') if email: try: signup = EventSignUp.objects.get(event=event, email=email) signup.unsignup = True signup.save() messages.success(request, success_msg) except: errors = { 'email': ('The email address you entered was not ' 'used to sign up.') } return json_response(status=400, data=errors) else: errors = { 'email': 'Please enter the email address you used to sign up.' } return json_response(status=400, data=errors) return json_response()
def update_candidate_initiation_status(request): """Endpoint for updating a candidate's initiation status. The post parameters "candidate" and "initiated" specify the candidate (by Candidate pk) and their new initiation status, respectively. """ candidate_pk = request.POST.get('candidate') if not candidate_pk: return json_response(status=404) candidate = get_object_or_none(Candidate, pk=candidate_pk) initiated = json.loads(request.POST.get('initiated')) if not candidate or initiated is None: return json_response(status=400) candidate.initiated = initiated candidate.save(update_fields=['initiated']) # TODO(sjdemartini): Update LDAP-related information, like removal from # or addition to relevant groups. # TODO(sjdemartini): Update relevant mailing lists, moving initiated # candidates off of the candidates list and onto the members list. return json_response()
def event_unsignup(request, event_pk): """Handles the action of un-signing up for events.""" try: event = Event.objects.get(pk=event_pk) except: return json_response(status=400) success_msg = 'Unsignup successful.' if request.user.is_authenticated(): try: # Try to get a signup object for this user signup = EventSignUp.objects.get(event=event, user=request.user) signup.user = request.user # Set the signup as "unsigned up" signup.unsignup = True signup.save() messages.success(request, success_msg) except EventSignUp.DoesNotExist: # If a signup could not be found, ignore this, since the user # is not signed up for the event pass else: email = request.POST.get('email') if email: try: signup = EventSignUp.objects.get(event=event, email=email) signup.unsignup = True signup.save() messages.success(request, success_msg) except: errors = {'email': ('The email address you entered was not ' 'used to sign up.')} return json_response(status=400, data=errors) else: errors = { 'email': 'Please enter the email address you used to sign up.' } return json_response(status=400, data=errors) return json_response()
def attendance_submit(request): """Record attendance for a given user at a given event. The user is specified by a userPK post parameter, and the event is specified by an eventPK post parameter. """ event_pk = request.POST['eventPK'] event = Event.objects.get(pk=event_pk) user_pk = request.POST['userPK'] user = user_model.objects.get(pk=user_pk) # Record attendance for this user at this event EventAttendance.objects.get_or_create(user=user, event=event) return json_response()
def attendance_delete(request): """Remove attendance for a given user at a given event. The user is specified by a userPK post parameter, and the event is specified by an eventPK post parameter. """ event_pk = request.POST['eventPK'] event = Event.objects.get(pk=event_pk) user_pk = request.POST['userPK'] # Delete this user's attendance for the event if it exists: try: EventAttendance.objects.get(user__pk=user_pk, event=event).delete() except EventAttendance.DoesNotExist: # Fine if the attendance does not exist, since we wanted to remove it pass return json_response()
def unassign_house(request): """Remove an officer or candidate from his or her house this semester. The user is specified by a userPK post parameter, and the house doesn't matter as a user can only be in one house in a given semester. The term is specified by a term post parameter which is the url name of the display term. """ user_pk = request.POST.get('userPK') term = Term.objects.get_by_url_name(request.POST.get('term')) # Delete the HouseMember object for this user/term if it exists try: HouseMember.objects.get(user__pk=user_pk, term=term).delete() except HouseMember.DoesNotExist: # Fine if the HouseMember does not exist since we wanted to remove it pass return json_response()
def news_reorder(request): """Endpoint for saving the ordering (ranks) of newsreel News items.""" # The "news" post parameter is a list of PKs of News items in the order that # they should appear (i.e., ordered from highest to lowest ranking items): news_order = request.POST.getlist('news') # We will reset the "rank" fields of each of the News items based on the # news_order given above. If we walk in reverse order of the news_order # list, we can set the rank of the current News item equal to the current # loop iteration number (since lower rank value means later in the # ordering): for rank, news_pk in enumerate(reversed(news_order)): news = News.objects.get(pk=news_pk) if news.rank != rank: news.rank = rank news.save(update_fields=['rank']) return json_response()
def attendance_search(request, max_results=20): """Return a JSON response of members based on search for name. The search uses the "searchTerm" post parameter. Return up to max_results number of results. The results only include people who have not attended the event specified by the post parameter eventPK. """ search_query = request.GET['searchTerm'] event_pk = request.GET['eventPK'] event = Event.objects.get(pk=event_pk) # Get all users who did not attend this event: # TODO(sjdemartini): Properly filter for members, instead of just getting # all users who are not officers or candidates (as these other users may # include company users, etc.) members = user_model.objects.exclude( eventattendance__event=event).select_related( 'userprofile') # A list of entries for each member that matches the search query: member_matches = [] # Parse the search query into separate pieces if the query includes # whitespace search_terms = search_query.lower().split() for member in members: name = member.userprofile.get_verbose_full_name() name_lower = name.lower() if all(search_term in name_lower for search_term in search_terms): pic_html = render_to_string( '_user_thumbnail.html', {'user_profile': member.userprofile}) entry = { 'label': name, 'value': member.pk, 'picture': pic_html } member_matches.append(entry) if len(member_matches) >= max_results: break return json_response(data=member_matches)
def event_revive(request, event_pk): """Revive a previously cancelled event.""" try: event = Event.objects.get(pk=event_pk) except: return json_response(status=404) event.cancelled = False event.save() if event.project_report is None: project_report = ProjectReport() else: project_report = event.project_report project_report.term = event.term project_report.date = event.end_datetime.date() project_report.title = event.name project_report.author = event.contact project_report.committee = event.committee project_report.save() event.project_report = project_report event.save(update_fields=['project_report']) return redirect('events:detail', event_pk=event.pk)
def dispatch(self, *args, **kwargs): self.event = get_object_or_404(Event, pk=self.kwargs['event_pk']) # A user cannot sign up unless they have permission to do so if not self.event.can_user_sign_up(self.request.user): return json_response(status=403) return super(EventSignUpView, self).dispatch(*args, **kwargs)