def create_project(request): form = ProjectCreationForm(request.POST) # TODO: Form validation project = Project.objects.create( project_creator=get_request_contributor(request), project_name=form.data.get('project_name'), project_short_description=form.data.get( 'project_short_description'), project_date_created=timezone.now(), project_url='', project_description='', project_location='', is_created=False) project = Project.objects.get(id=project.id) # Tag fields operate like ManyToMany fields, and so cannot # be added until after the object is created. Tag.merge_tags_field(project.project_issue_area, form.data.get('project_issue_area')) merge_single_file(project, form, FileCategory.THUMBNAIL, 'project_thumbnail_location') project.save() return project
def tags(request): url_parts = request.GET.urlencode() query_terms = urlparse.parse_qs( url_parts, keep_blank_values=0, strict_parsing=0) if 'category' in query_terms: category = query_terms.get('category')[0] queryset = get_tags_by_category(category) countoption = bool(strtobool(query_terms.get('getCounts')[0])) if countoption == True: activetagdict = projects_tag_counts() querydict = {tag.tag_name:tag for tag in queryset} resultdict = {} for slug in querydict.keys(): resultdict[slug] = Tag.hydrate_tag_model(querydict[slug]) resultdict[slug]['num_times'] = activetagdict[slug] if slug in activetagdict else 0 tags = list(resultdict.values()) else: tags = list(queryset.values()) else: countoption = bool(strtobool(query_terms.get('getCounts')[0])) if countoption == True: queryset = Tag.objects.all() activetagdict = projects_tag_counts() querydict = {tag.tag_name:tag for tag in queryset} resultdict = {} for slug in querydict.keys(): resultdict[slug] = Tag.hydrate_tag_model(querydict[slug]) resultdict[slug]['num_times'] = activetagdict[slug] if slug in activetagdict else 0 tags = list(resultdict.values()) else: queryset = Tag.objects.all() tags = list(queryset.values()) return JsonResponse(tags, safe=False)
def hydrate_to_json(self): files = ProjectFile.objects.filter(file_project=self.id) thumbnail_files = list( files.filter(file_category=FileCategory.THUMBNAIL.value)) other_files = list(files.filter(file_category=FileCategory.ETC.value)) links = ProjectLink.objects.filter(link_project=self.id) positions = ProjectPosition.objects.filter(position_project=self.id) volunteers = VolunteerRelation.objects.filter(project=self.id) project = { 'project_id': self.id, 'project_name': self.project_name, 'project_creator': self.project_creator.id, 'project_claimed': not self.project_creator.is_admin_contributor(), 'project_description': self.project_description, 'project_short_description': self.project_short_description, 'project_url': self.project_url, 'project_location': self.project_location, 'project_organization': Tag.hydrate_to_json(self.id, list( self.project_organization.all().values())), 'project_issue_area': Tag.hydrate_to_json(self.id, list(self.project_issue_area.all().values())), 'project_stage': Tag.hydrate_to_json(self.id, list(self.project_stage.all().values())), 'project_technologies': Tag.hydrate_to_json(self.id, list( self.project_technologies.all().values())), 'project_positions': list(map(lambda position: position.to_json(), positions)), 'project_files': list(map(lambda file: file.to_json(), other_files)), 'project_links': list(map(lambda link: link.to_json(), links)), 'project_owners': [self.project_creator.hydrate_to_tile_json()], 'project_volunteers': list(map(lambda volunteer: volunteer.to_json(), volunteers)), 'project_date_modified': self.project_date_modified.__str__() } if len(thumbnail_files) > 0: project['project_thumbnail'] = thumbnail_files[0].to_json() return project
def edit_user(request, user_id): user = Contributor.objects.get(id=user_id) if not (request.user.username == user.username or request.user.is_staff): raise PermissionDenied() project_fields_changed = False form = DemocracyLabUserCreationForm(request.POST) project_fields_changed |= read_form_field_string( user, form, 'first_name') project_fields_changed |= read_form_field_string( user, form, 'last_name') read_form_field_string(user, form, 'about_me') read_form_field_string(user, form, 'postal_code') read_form_field_string(user, form, 'country') Tag.merge_tags_field(user.user_technologies, form.data.get('user_technologies')) user.save() links_json_text = form.data.get('user_links') if len(links_json_text) > 0: links_json = json.loads(links_json_text) ProjectLink.merge_changes(user, links_json) files_json_text = form.data.get('user_files') if len(files_json_text) > 0: files_json = json.loads(files_json_text) ProjectFile.merge_changes(user, files_json) user_thumbnail_location = form.data.get('user_thumbnail_location') if len(user_thumbnail_location) > 0: thumbnail_file_json = json.loads(user_thumbnail_location) project_fields_changed |= ProjectFile.replace_single_file( user, FileCategory.THUMBNAIL, thumbnail_file_json) user_resume_file = form.data.get('user_resume_file') if len(user_resume_file) > 0: user_resume_file_json = json.loads(user_resume_file) ProjectFile.replace_single_file(user, FileCategory.RESUME, user_resume_file_json) if project_fields_changed: user.update_linked_items() SubscribeUserToQiqoChat(user)
def to_json(self): return { 'id': self.id, 'description': self.position_description, 'descriptionUrl': self.description_url, 'roleTag': Tag.hydrate_to_json(self.id, self.position_role.all().values())[0] }
def send_volunteer_application_email(volunteer_relation, is_reminder=False): project = volunteer_relation.project user = volunteer_relation.volunteer role_details = Tag.from_field(volunteer_relation.role) role_text = "{subcategory}: {name}".format( subcategory=role_details.subcategory, name=role_details.display_name) project_profile_url = settings.PROTOCOL_DOMAIN + '/index/?section=AboutProject&id=' + str( project.id) approve_url = settings.PROTOCOL_DOMAIN + '/volunteer/approve/' + str( volunteer_relation.id) + '/' email_subject = '{is_reminder}{firstname} {lastname} would like to volunteer with {project} as {role}'.format( is_reminder='REMINDER: ' if is_reminder else '', firstname=user.first_name, lastname=user.last_name, project=project.project_name, role=role_text) email_template = HtmlEmailTemplate()\ .header("You Have a New Volunteer!")\ .paragraph('\"{message}\" -{firstname} {lastname}'.format( message=volunteer_relation.application_text, firstname=user.first_name, lastname=user.last_name))\ .paragraph('Please click below to review this volunteer')\ .button(url=project_profile_url, text='REVIEW VOLUNTEER')\ .button(url=approve_url, text='APPROVE VOLUNTEER') send_to_project_owners(project=project, sender=user, subject=email_subject, template=email_template)
def send_volunteer_application_email(volunteer_relation, is_reminder=False): project = volunteer_relation.project user = volunteer_relation.volunteer role_details = Tag.from_field(volunteer_relation.role) role_text = "{subcategory}: {name}".format(subcategory=role_details.subcategory, name=role_details.display_name) project_profile_url = settings.PROTOCOL_DOMAIN + '/index/?section=AboutProject&id=' + str(project.id) approve_url = settings.PROTOCOL_DOMAIN + '/volunteer/approve/' + str(volunteer_relation.id) + '/' email_subject = '{is_reminder}{firstname} {lastname} would like to volunteer with {project} as {role}'.format( is_reminder='REMINDER: ' if is_reminder else '', firstname=user.first_name, lastname=user.last_name, project=project.project_name, role=role_text) email_template = HtmlEmailTemplate()\ .subheader("Opportunity Information:")\ .text_line("Title: {role}".format(role=role_details.display_name))\ .text_line("Organization: {projectname}".format(projectname=project.project_name))\ .text_line("Date: {currentdate}".format(currentdate=datetime_to_string(timezone.now(), DateTimeFormats.MONTH_DD_YYYY)))\ .subheader("Volunteer Information:")\ .text_line("Name: {firstname} {lastname}".format( firstname=user.first_name, lastname=user.last_name))\ .text_line("Email: " + Html.a(href='mailto:' + user.email, text=user.email)) if user.postal_code: email_template = email_template.text_line("Zip: {zip}".format(zip=user.postal_code)) email_template = email_template.header_left("You Have a New Volunteer!")\ .paragraph('\"{message}\" -{firstname} {lastname}'.format( message=volunteer_relation.application_text, firstname=user.first_name, lastname=user.last_name))\ .paragraph('To contact this volunteer directly, you can reply to this email. To review their profile or approve their application, use the buttons below.')\ .button(url=project_profile_url, text='REVIEW VOLUNTEER')\ .button(url=approve_url, text='APPROVE VOLUNTEER') send_to_project_owners(project=project, sender=user, subject=email_subject, template=email_template)
def remove_tags_not_in_list(): for project in Project.objects.all(): for issue in project.project_issue_area.all(): issue_tag = Tag.get_by_name(issue) if not issue_tag: print('Removing invalid tag', issue, 'from project:', project.project_name) project.project_issue_area.remove(issue)
def to_json(self): volunteer = self.volunteer volunteer_json = { 'application_id': self.id, 'user': volunteer.hydrate_to_tile_json(), 'application_text': self.application_text, 'application_date': self.application_date.__str__(), 'platform_date_joined': volunteer.date_joined.__str__(), 'roleTag': Tag.hydrate_to_json(volunteer.id, self.role.all().values())[0], 'isApproved': self.is_approved, 'isCoOwner': self.is_co_owner, 'isUpForRenewal': self.is_up_for_renewal(), 'projectedEndDate': self.projected_end_date.__str__() } return volunteer_json
def hydrate_to_json(self): files = ProjectFile.objects.filter(file_project=self.id) thumbnail_files = list( files.filter(file_category=FileCategory.THUMBNAIL.value)) other_files = list(files.filter(file_category=FileCategory.ETC.value)) links = ProjectLink.objects.filter(link_project=self.id) project = { 'project_id': self.id, 'project_name': self.project_name, 'project_creator': self.project_creator.id, 'project_description': self.project_description, 'project_url': self.project_url, 'project_location': self.project_location, 'project_issue_area': Tag.hydrate_to_json(list(self.project_issue_area.all().values())), 'project_files': list(map(lambda file: file.to_json(), other_files)), 'project_links': list(map(lambda link: link.to_json(), links)) } if len(thumbnail_files) > 0: project['project_thumbnail'] = thumbnail_files[0].to_json() return project
def hydrate_to_json(self): projects = ProjectRelationship.objects.filter(relationship_event=self.id) files = ProjectFile.objects.filter(file_event=self.id) thumbnail_files = list(files.filter(file_category=FileCategory.THUMBNAIL.value)) other_files = list(files.filter(file_category=FileCategory.ETC.value)) event = { 'event_agenda': self.event_agenda, 'event_creator': self.event_creator.id, 'event_date_end': self.event_date_end.__str__(), 'event_date_modified': self.event_date_modified.__str__(), 'event_date_start': self.event_date_start.__str__(), 'event_description': self.event_description, 'event_files': list(map(lambda file: file.to_json(), other_files)), 'event_id': self.id, 'event_location': self.event_location, 'event_rsvp_url': self.event_rsvp_url, 'event_live_id': self.event_live_id, 'event_name': self.event_name, 'event_owners': [self.event_creator.hydrate_to_tile_json()], 'event_short_description': self.event_short_description, 'event_legacy_organization': Tag.hydrate_to_json(self.id, list(self.event_legacy_organization.all().values())), } if len(projects) > 0: event['event_projects'] = list(map(lambda project: project.relationship_project.hydrate_to_tile_json(), projects)) if len(thumbnail_files) > 0: event['event_thumbnail'] = thumbnail_files[0].to_json() return event
def edit_project(request, project_id): project = Project.objects.get(id=project_id) if not request.user.username == project.project_creator.username: raise PermissionDenied() form = ProjectCreationForm(request.POST) project.project_description = form.data.get('project_description') project.project_location = form.data.get('project_location') project.project_name = form.data.get('project_name') project.project_url = form.data.get('project_url') issue_areas = form.data.get('project_issue_area') if issue_areas and len(issue_areas) != 0: Tag.merge_tags_field(project.project_issue_area, issue_areas) project.save() links_json_text = form.data.get('project_links') if len(links_json_text) > 0: links_json = json.loads(links_json_text) ProjectLink.merge_changes(project, links_json) files_json_text = form.data.get('project_files') if len(files_json_text) > 0: files_json = json.loads(files_json_text) ProjectFile.merge_changes(project, files_json) project_thumbnail_location = form.data.get( 'project_thumbnail_location') if len(project_thumbnail_location) > 0: thumbnail_json = json.loads(project_thumbnail_location) existing_thumbnail = ProjectFile.objects.filter( file_project=project.id, file_category=FileCategory.THUMBNAIL.value).first() if not existing_thumbnail: thumbnail = ProjectFile.from_json(project, FileCategory.THUMBNAIL, thumbnail_json) thumbnail.save() elif not thumbnail_json['key'] == existing_thumbnail.file_key: thumbnail = ProjectFile.from_json(project, FileCategory.THUMBNAIL, thumbnail_json) thumbnail.save() existing_thumbnail.delete()
def hydrate_to_tile_json(self): files = ProjectFile.objects.filter(file_project=self.id) thumbnail_files = list( files.filter(file_category=FileCategory.THUMBNAIL.value)) positions = ProjectPosition.objects.filter(position_project=self.id) project = { 'project_id': self.id, 'project_name': self.project_name, 'project_creator': self.project_creator.id, 'project_description': self.project_short_description if self.project_short_description else self.project_description, 'project_url': self.project_url, 'project_location': self.project_location, 'project_country': self.project_country, 'project_state': self.project_state, 'project_city': self.project_city, 'project_issue_area': Tag.hydrate_to_json(self.id, list(self.project_issue_area.all().values())), 'project_stage': Tag.hydrate_to_json(self.id, list(self.project_stage.all().values())), 'project_positions': list(map(lambda position: position.to_json(), positions)), 'project_date_modified': self.project_date_modified.__str__() } if len(thumbnail_files) > 0: project['project_thumbnail'] = thumbnail_files[0].to_json() return project
def edit_user(request, user_id): user = Contributor.objects.get(id=user_id) if not request.user.username == user.username: raise PermissionDenied() form = DemocracyLabUserCreationForm(request.POST) user.about_me = form.data.get('about_me') user.postal_code = form.data.get('postal_code') user.country = form.data.get('country') user.first_name = form.data.get('first_name') user.last_name = form.data.get('last_name') Tag.merge_tags_field(user.user_technologies, form.data.get('user_technologies')) user.save() links_json_text = form.data.get('user_links') if len(links_json_text) > 0: links_json = json.loads(links_json_text) ProjectLink.merge_changes(user, links_json) files_json_text = form.data.get('user_files') if len(files_json_text) > 0: files_json = json.loads(files_json_text) ProjectFile.merge_changes(user, files_json) user_thumbnail_location = form.data.get('user_thumbnail_location') if len(user_thumbnail_location) > 0: thumbnail_file_json = json.loads(user_thumbnail_location) ProjectFile.replace_single_file(user, FileCategory.THUMBNAIL, thumbnail_file_json) user_resume_file = form.data.get('user_resume_file') if len(user_resume_file) > 0: user_resume_file_json = json.loads(user_resume_file) ProjectFile.replace_single_file(user, FileCategory.RESUME, user_resume_file_json) SubscribeUserToQiqoChat(user)
def read_form_field_tags(model, form, field_name): """ Read tags form field into model field :param model: Model containing tag field :param form: Form data from front-end :param field_name: Name of field shared by model and form :return: True if changes to model tag field were made """ if field_name in form.data: return Tag.merge_tags_field(getattr(model, field_name), form.data.get(field_name)) return False
def get_issue_areas(self): project_relationships = ProjectRelationship.objects.filter( relationship_event=self.id) project_ids = list( map(lambda relationship: relationship.relationship_project.id, project_relationships)) project_list = Project.objects.filter(id__in=project_ids) return [ Tag.hydrate_to_json( project.id, list(project.project_issue_area.all().values())) for project in project_list ]
def edit_project(request, project_id): project = Project.objects.get(id=project_id) if not is_co_owner_or_staff(request.user, project): raise PermissionDenied() form = ProjectCreationForm(request.POST) project.project_description = form.data.get('project_description') project.project_short_description = form.data.get( 'project_short_description') project.project_location = form.data.get('project_location') project.project_name = form.data.get('project_name') project.project_url = form.data.get('project_url') Tag.merge_tags_field(project.project_issue_area, form.data.get('project_issue_area')) Tag.merge_tags_field(project.project_stage, form.data.get('project_stage')) Tag.merge_tags_field(project.project_technologies, form.data.get('project_technologies')) Tag.merge_tags_field(project.project_organization, form.data.get('project_organization')) project.save() positions_json_text = form.data.get('project_positions') if len(positions_json_text) > 0: positions_json = json.loads(positions_json_text) ProjectPosition.merge_changes(project, positions_json) links_json_text = form.data.get('project_links') if len(links_json_text) > 0: links_json = json.loads(links_json_text) ProjectLink.merge_changes(project, links_json) files_json_text = form.data.get('project_files') if len(files_json_text) > 0: files_json = json.loads(files_json_text) ProjectFile.merge_changes(project, files_json) project_thumbnail_location = form.data.get( 'project_thumbnail_location') if len(project_thumbnail_location) > 0: thumbnail_location_json = json.loads(project_thumbnail_location) ProjectFile.replace_single_file(project, FileCategory.THUMBNAIL, thumbnail_location_json)
def send_volunteer_application_email(volunteer_relation, is_reminder=False): project = volunteer_relation.project user = volunteer_relation.volunteer role_details = Tag.from_field(volunteer_relation.role) role_text = "{subcategory}: {name}".format(subcategory=role_details.subcategory, name=role_details.display_name) project_profile_url = settings.PROTOCOL_DOMAIN + '/index/?section=AboutProject&id=' + str(project.id) email_subject = '{is_reminder}{firstname} {lastname} would like to volunteer with {project} as {role}'.format( is_reminder='REMINDER: ' if is_reminder else '', firstname=user.first_name, lastname=user.last_name, project=project.project_name, role=role_text) email_body = '{message} \n -- \n To review this volunteer, see {url}'.format( message=volunteer_relation.application_text, user=user.email, url=project_profile_url) send_to_project_owners(project=project, sender=user, subject=email_subject, body=email_body)
def hydrate_to_json(self): links = civictechprojects.models.ProjectLink.objects.filter( link_user=self.id) files = civictechprojects.models.ProjectFile.objects.filter( file_user=self.id) other_files = list( filter( lambda file: file.file_category != civictechprojects.models. FileCategory.THUMBNAIL.value, files)) user = { 'id': self.id, 'email': self.email, 'first_name': self.first_name, 'last_name': self.last_name, 'about_me': self.about_me, 'country': self.country, 'postal_code': self.postal_code, 'user_technologies': Tag.hydrate_to_json(self.id, list(self.user_technologies.all().values())), 'user_links': list(map(lambda link: link.to_json(), links)), 'user_files': list(map(lambda file: file.to_json(), other_files)), } thumbnail_files = list( files.filter(file_category=civictechprojects.models.FileCategory. THUMBNAIL.value)) if len(thumbnail_files) > 0: user['user_thumbnail'] = thumbnail_files[0].to_json() return user
def import_tags_from_csv(): dir = os.path.dirname(__file__) filename = os.path.join(dir, '../models/Tag_definitions.csv') tags = [] with open(filename, 'r', encoding='utf-8') as f: reader = csv.reader(f) try: #skip header row next(reader) for row in reader: tag = Tag(tag_name=row[0], display_name=row[1], caption=row[2], category=row[3], subcategory=row[4], parent=row[5] ) tags.append(tag) except csv.Error as e: sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e)) merge_tags_with_existing(tags)
def to_json(self): volunteer = self.volunteer # TODO: Add end date and application date volunteer_json = { 'application_id': self.id, 'user': volunteer.hydrate_to_tile_json(), 'application_text': self.application_text, 'roleTag': Tag.hydrate_to_json(volunteer.id, self.role.all().values())[0], 'isApproved': self.is_approved, 'isCoOwner': self.is_co_owner } return volunteer_json
def read_form_field_tags(model, form, field_name): if field_name in form.data: Tag.merge_tags_field(getattr(model, field_name), form.data.get(field_name))
def update_from_json(position, position_json): position.position_description = position_json['description'] position.description_url = position_json['descriptionUrl'] new_role = position_json['roleTag']['tag_name'] Tag.merge_tags_field(position.position_role, new_role) position.save()
def remove_tags_not_in_list(): for project in Project.objects.all(): for issue in project.project_issue_area.all(): issue_tag = Tag.get_by_name(issue) if not issue_tag: project.project_issue_area.remove(issue)
def hydrate_to_json(self): files = ProjectFile.objects.filter(file_project=self.id) thumbnail_files = list( files.filter(file_category=FileCategory.THUMBNAIL.value)) other_files = list(files.filter(file_category=FileCategory.ETC.value)) links = ProjectLink.objects.filter(link_project=self.id) positions = ProjectPosition.objects.filter(position_project=self.id) volunteers = VolunteerRelation.objects.filter(project=self.id) commits = ProjectCommit.objects.filter( commit_project=self.id).order_by('-commit_date')[:20] # TODO: Don't return location id # TODO: Reduce country down to 2-char code project = { 'project_id': self.id, 'project_name': self.project_name, 'project_creator': self.project_creator.id, 'project_claimed': not self.project_creator.is_admin_contributor(), 'project_approved': self.is_searchable, 'project_description': self.project_description, 'project_description_solution': self.project_description_solution, 'project_description_actions': self.project_description_actions, 'project_short_description': self.project_short_description, 'project_url': self.project_url, 'project_location': self.project_location, 'project_country': self.project_country, 'project_state': self.project_state, 'project_city': self.project_city, 'project_organization': Tag.hydrate_to_json(self.id, list( self.project_organization.all().values())), 'project_organization_type': Tag.hydrate_to_json( self.id, list(self.project_organization_type.all().values())), 'project_issue_area': Tag.hydrate_to_json(self.id, list(self.project_issue_area.all().values())), 'project_stage': Tag.hydrate_to_json(self.id, list(self.project_stage.all().values())), 'project_technologies': Tag.hydrate_to_json(self.id, list( self.project_technologies.all().values())), 'project_positions': list(map(lambda position: position.to_json(), positions)), 'project_files': list(map(lambda file: file.to_json(), other_files)), 'project_links': list(map(lambda link: link.to_json(), links)), 'project_commits': list(map(lambda commit: commit.to_json(), commits)), 'project_owners': [self.project_creator.hydrate_to_tile_json()], 'project_volunteers': list(map(lambda volunteer: volunteer.to_json(), volunteers)), 'project_date_modified': self.project_date_modified.__str__() } if self.project_location_coords is not None: project['project_latitude'] = self.project_location_coords.x project['project_longitude'] = self.project_location_coords.y if len(thumbnail_files) > 0: project['project_thumbnail'] = thumbnail_files[0].to_json() return project