def community_edit_process(request): """ Processes the form data from the community add/edit form. :param request: Request object. :return: JSON response. """ try: # Get all of the form data. community_id = request.POST.get("community_id", "") name = request.POST.get("name", "") motto = request.POST.get("motto", "") hangout = request.POST.get("hangout", "") try: district_id = request.POST.get("district-dropdown", False) if district_id: district = District.objects.get(id=int(district_id)) else: district = None except: district = None try: state_id = request.POST.get("state-dropdown", False) if state_id: state = State.objects.get(id=int(state_id)) else: state = None except: state = None # The logo needs special handling. If the path isn't passed in the post, we'll look to see if it's a new file. if request.POST.get("logo", "nothing") == "nothing": # Try to grab the new file, and if it isn't there, just make it blank. try: logo = FileUploads() logo.type = "community_logos" logo.sub_type = community_id logo.upload = request.FILES.get("logo") logo.save() except Exception as e: logo = None log.warning("Error uploading logo: {0}".format(e)) else: # If the path was passed in, just use that. logo = None facilitator = request.POST.get("facilitator", "") private = request.POST.get("private", 0) # These all have multiple values, so we'll use the get_post_array function to grab all the values. courses = get_post_array(request.POST, "course") resource_names = get_post_array(request.POST, "resource_name") resource_links = get_post_array(request.POST, "resource_link") # If this is a new community, create a new entry, otherwise, load from the DB. if community_id == "new": community_object = CommunityCommunities() else: community_object = CommunityCommunities.objects.get(id=community_id) # Set all the community values and save to the DB. community_object.name = name community_object.motto = motto if logo: community_object.logo = logo community_object.hangout = hangout community_object.private = int(private) community_object.district = district community_object.state = state community_object.save() # Load the main user object for the facilitator user. user_object = False try: user_object = User.objects.get(email=facilitator) except Exception as e: log.warning("Invalid email for facilitator: {0}".format(e)) # As long as the object loaded correctly, make sure this user is set as the facilitator. if user_object: # First we need to make sure if there is already a facilitator set, we unset them. try: old_facilitator = CommunityUsers.objects.filter(facilitator=True, community=community_object) for f in old_facilitator: f.facilitator = False f.save() except: pass # Now we try to load the new user in case they are already a member. try: community_user = CommunityUsers.objects.get(user=user_object, community=community_object) # If they aren't a member already, create a new entry. except: community_user = CommunityUsers() community_user.community = community_object community_user.user = user_object # Set the facilitator flag to true. community_user.facilitator = True community_user.save() else: raise Exception("A valid facilitator is required to create a community.") # Drop all of the courses before adding those in the form. Otherwise there is a lot of expensive checking. CommunityCourses.objects.filter(community=community_object).delete() # Go through the courses and add them to the DB. for key, course in courses.iteritems(): # We only want to save an entry if there's something in it. if course: course_object = CommunityCourses() course_object.community = community_object course_object.course = course course_object.save() # Drop all of the resources before adding those in the form. Otherwise there is a lot of expensive checking. CommunityResources.objects.filter(community=community_object).delete() # Go through the resource links, with the index so we can directly access the names and logos. for key, resource_link in resource_links.iteritems(): # We only want to save an entry if there's something in it. if resource_link: resource_object = CommunityResources() resource_object.community = community_object resource_object.link = resource_link resource_object.name = resource_names[key] # The logo needs special handling since we might need to upload the file. First we try the entry in the # FILES and try to upload it. if request.POST.get("resource_logo[{0}]".format(key)): file_id = int(request.POST.get("resource_logo[{0}]".format(key))) logo = FileUploads.objects.get(id=file_id) else: try: logo = FileUploads() logo.type = "community_resource_logos" logo.sub_type = community_id logo.upload = request.FILES.get("resource_logo[{0}]".format(key)) logo.save() except Exception as e: logo = None log.warning("Error uploading logo: {0}".format(e)) if logo: resource_object.logo = logo resource_object.save() return redirect(reverse("community_view", kwargs={"community_id": community_object.id})) except Exception as e: data = { "error_title": "Problem Saving Community", "error_message": "Error: {0}".format(e), "window_title": "Problem Saving Community", } return render_to_response("error.html", data)
def get_remove_user_rows(request, community_id): """ Builds the rows for display in the PepConn Users report. :param request: User request :return: Table rows for the user table """ # Defines the columns in the table. Key is the column #, value is a list made up of the column selector, the type of # selection, and the type of data in the column (or False to ignore this column in filters). columns = { 0: ["user__email", "__icontains", "str"], 1: ["user__username", "__icontains", "str"], 2: ["user__first_name", "__icontains", "str"], 3: ["user__last_name", "__iexact", "str"], 4: ["user__profile__district__state__name", "__iexact", "str"], 5: ["user__profile__district__name", "__iexact", "str"], 6: ["user__profile__school__name", "__icontains", "str"], } # Parse the sort data passed in. sorts = get_post_array(request.GET, "col") # Parse the filter data passed in. filters = get_post_array(request.GET, "fcol", 7) # Get the page number and number of rows per page, and calculate the start and end of the query. page = int(request.GET["page"]) size = int(request.GET["size"]) start = page * size end = start + size - 1 if filters.get("7"): filters["all"] = filters["7"] del filters["7"] # Get the sort arguments if any. order = build_sorts(columns, sorts) # If the were filers passed in, get the arguments to filter by and add them to the query. if len(filters): args, kwargs = build_filters(columns, filters) # If there was a search for all, add the Q arguments. if args: users = CommunityUsers.objects.prefetch_related().filter(args, **kwargs).order_by(*order) else: users = CommunityUsers.objects.prefetch_related().filter(**kwargs).order_by(*order) # If there are no filters, just select all. else: users = CommunityUsers.objects.prefetch_related().all().order_by(*order) users = users.filter(community=community_id) # The number of results is the first value in the return JSON count = users.count() json_out = [count] # Add the row data to the list of rows. rows = list() for item in users[start:end]: row = list() row.append(str(item.user.email)) row.append(str(item.user.username)) row.append(str(item.user.first_name)) row.append(str(item.user.last_name)) try: user_school = item.user.profile.school.name except: user_school = "" try: user_district = str(item.user.profile.district.name) user_district_state = str(item.user.profile.district.state.name) except: user_district = "" user_district_state = "" try: user_cohort = str(item.user.profile.cohort.code) except: user_cohort = "" row.append(str(user_district_state)) row.append(str(user_district)) # row.append(str(user_cohort)) row.append(str(user_school)) # row.append(str(item.user.profile.subscription_status)) # try: # activation_key = str(Registration.objects.get(user_id=item.user_id).activation_key) # except: # activation_key = '' # row.append(str(item.user.date_joined)) row.append('<input class="select_box" type="checkbox" name="id" value="' + str(item.user.id) + '"/>') rows.append(row) # The list of rows is the second value in the return JSON. json_out.append(rows) return HttpResponse(json.dumps(json_out), content_type="application/json")