示例#1
0
文件: nicks.py 项目: Bombe/demozoo
def match(request):
	initial_query = request.GET.get('q').lstrip()  # only lstrip, because whitespace on right may be significant for autocompletion
	autocomplete = request.GET.get('autocomplete', False)
	sceners_only = request.GET.get('sceners_only', False)
	groups_only = request.GET.get('groups_only', False)

	# irritating workaround for not being able to pass an "omit this parameter" value to jquery
	if autocomplete == 'false' or autocomplete == 'null' or autocomplete == '0':
		autocomplete = False

	filters = {}  # also doubles up as options to pass to NickSearch
	if sceners_only:
		filters['sceners_only'] = True
	elif groups_only:
		filters['groups_only'] = True

	if autocomplete:
		query = initial_query + NickVariant.autocomplete(initial_query, **filters)
	else:
		query = initial_query

	nick_search = NickSearch(query.rstrip(), **filters)

	data = {
		'query': query,
		'initial_query': initial_query,
		'match': nick_search.match_data,
	}
	# to simulate network lag:
	#import time
	#time.sleep(2)
	return HttpResponse(json.dumps(data), mimetype="text/javascript")
示例#2
0
def match(request):
    initial_query = request.GET.get('q').lstrip(
    )  # only lstrip, because whitespace on right may be significant for autocompletion
    autocomplete = request.GET.get('autocomplete', False)
    sceners_only = request.GET.get('sceners_only', False)
    groups_only = request.GET.get('groups_only', False)

    # irritating workaround for not being able to pass an "omit this parameter" value to jquery
    if autocomplete == 'false' or autocomplete == 'null' or autocomplete == '0':
        autocomplete = False

    filters = {}  # also doubles up as options to pass to NickSearch
    if sceners_only:
        filters['sceners_only'] = True
    elif groups_only:
        filters['groups_only'] = True

    if autocomplete:
        query = initial_query + NickVariant.autocomplete(
            initial_query, **filters)
    else:
        query = initial_query

    nick_search = NickSearch(query.rstrip(), **filters)

    data = {
        'query': query,
        'initial_query': initial_query,
        'match': nick_search.match_data,
    }
    # to simulate network lag:
    #import time
    #time.sleep(2)
    return HttpResponse(json.dumps(data), mimetype="text/javascript")
示例#3
0
    def __init__(self,
                 search_term,
                 selection=None,
                 sceners_only=False,
                 groups_only=False,
                 group_ids=[],
                 group_names=[],
                 member_names=[]):

        self.search_term = search_term

        nick_variants = NickVariant.autocompletion_search(
            search_term,
            exact=True,
            sceners_only=sceners_only,
            groups_only=groups_only,
            group_ids=group_ids,
            group_names=group_names,
            member_names=member_names)

        self.suggestions = []

        for nv in nick_variants:
            suggestion = {
                'className':
                ('group' if nv.nick.releaser.is_group else 'scener'),
                'nameWithAffiliations': nv.nick.name_with_affiliations(),
                'name': nv.nick.name,
                'id': nv.nick_id
            }
            if nv.nick.releaser.country_code:
                suggestion[
                    'countryCode'] = nv.nick.releaser.country_code.lower()

            if nv.nick.differentiator:
                suggestion['differentiator'] = nv.nick.differentiator
                suggestion['nameWithDifferentiator'] = "%s (%s)" % (
                    nv.nick.name, nv.nick.differentiator)
            else:
                suggestion['nameWithDifferentiator'] = nv.nick.name

            if nv.nick.name != nv.name:
                suggestion['alias'] = nv.name

            self.suggestions.append(suggestion)

        if not groups_only:
            self.suggestions.append({
                'className':
                'add_scener',
                'nameWithAffiliations':
                "Add a new scener named '%s'" % search_term,
                'nameWithDifferentiator':
                search_term,
                'name':
                search_term,
                'id':
                'newscener',
            })

        if not sceners_only:
            self.suggestions.append({
                'className':
                'add_group',
                'nameWithAffiliations':
                "Add a new group named '%s'" % search_term,
                'nameWithDifferentiator':
                search_term,
                'name':
                search_term,
                'id':
                'newgroup'
            })

        if selection:
            self.selection = selection
        else:
            # if there is a definite best-scoring nickvariant, select it
            if nick_variants.count() == 0:
                self.selection = None
            elif nick_variants.count(
            ) == 1 or nick_variants[0].score > nick_variants[1].score:
                self.selection = NickSelection(
                    self.suggestions[0]['id'],
                    self.suggestions[0]['nameWithDifferentiator'])
            else:
                self.selection = None
示例#4
0
    def __init__(self,
                 search_term,
                 author_nick_selections=[],
                 affiliation_nick_selections=[],
                 autocomplete=False):

        self.search_term = search_term

        # parse the byline
        parts = self.search_term.split('/')
        authors_string = parts[0]  # everything before first slash is an author
        affiliations_string = '^'.join(
            parts[1:])  # everything after first slash is an affiliation

        # split on separators that have a trailing (and optionally leading) space
        author_names = re.split(r"\s*[\,\+\^\&]\s+", authors_string)
        author_names = [name.lstrip() for name in author_names if name.strip()]
        affiliation_names = re.split(r"\s*[\,\+\^\&]\s+", affiliations_string)
        affiliation_names = [
            name.lstrip() for name in affiliation_names if name.strip()
        ]

        # Now, for any item in author_names or affiliation_names with an internal separator character,
        # perform a pre-check for that name. If not present, split it and move on.
        vetted_author_names = []
        for name in author_names:
            if re.search(r"[\,\+\^\&]",
                         name) and not NickVariant.objects.filter(
                             name__iexact=name.strip()).exists():
                for subname in re.split(r"[\,\+\^\&]", name):
                    subname = subname.lstrip()
                    if subname:
                        vetted_author_names.append(subname)
            else:
                vetted_author_names.append(name)

        vetted_affiliation_names = []
        for name in affiliation_names:
            if re.search(r"[\,\+\^\&]",
                         name) and not NickVariant.objects.filter(
                             name__iexact=name.strip(),
                             nick__releaser__is_group=True).exists():
                for subname in re.split(r"[\,\+\^\&]", name):
                    subname = subname.lstrip()
                    if subname:
                        vetted_affiliation_names.append(subname)
            else:
                vetted_affiliation_names.append(name)

        author_names = vetted_author_names
        affiliation_names = vetted_affiliation_names

        # attempt to autocomplete the last element of the name,
        # if autocomplete flag is True and search term has no trailing ,+^/& separator
        if autocomplete and not re.search(r"[\,\+\^\/\&]\s*$",
                                          self.search_term):
            if affiliation_names:
                autocompletion = NickVariant.autocomplete(
                    affiliation_names[-1],
                    significant_whitespace=False,
                    groups_only=True,
                    member_names=[name.strip() for name in author_names])
                affiliation_names[-1] += autocompletion
                self.search_term += autocompletion
            elif author_names:
                autocompletion = NickVariant.autocomplete(
                    author_names[-1], significant_whitespace=False)
                author_names[-1] += autocompletion
                self.search_term += autocompletion

        author_names = [name.strip() for name in author_names]
        affiliation_names = [name.strip() for name in affiliation_names]

        # construct a NickSearch for each element
        self.author_nick_searches = []
        for (i, author_name) in enumerate(author_names):
            try:
                selection = author_nick_selections[i]
            except IndexError:
                selection = None
            self.author_nick_searches.append(
                NickSearch(author_name,
                           selection,
                           group_names=affiliation_names))

        self.affiliation_nick_searches = []
        for (i, affiliation_name) in enumerate(affiliation_names):
            try:
                selection = affiliation_nick_selections[i]
            except IndexError:
                selection = None
            self.affiliation_nick_searches.append(
                NickSearch(affiliation_name,
                           selection,
                           groups_only=True,
                           member_names=author_names))

        self.author_nick_selections = [
            nick_search.selection for nick_search in self.author_nick_searches
        ]
        self.affiliation_nick_selections = [
            nick_search.selection
            for nick_search in self.affiliation_nick_searches
        ]
示例#5
0
    def handle(self, *args, **kwargs):
        self.verbosity = kwargs['verbosity']

        if self.verbosity >= 1:
            print("Looking for releasers without their name as a Nick")

        releasers = Releaser.objects.raw('''
            SELECT demoscene_releaser.*
            FROM
                demoscene_releaser
                LEFT JOIN demoscene_nick ON (
                    demoscene_releaser.id = demoscene_nick.releaser_id
                    AND demoscene_releaser.name = demoscene_nick.name
                )
            WHERE
                demoscene_nick.id IS NULL
        ''')
        for releaser in releasers:
            if self.verbosity >= 1:
                print("creating nick for %s" % releaser)
            nick = Nick(releaser=releaser, name=releaser.name)
            nick.save()

        if self.verbosity >= 1:
            print("Looking for Nicks without their name as a NickVariant")

        nicks = Nick.objects.raw('''
            SELECT demoscene_nick.*
            FROM
                demoscene_nick
                LEFT JOIN demoscene_nickvariant ON (
                    demoscene_nick.id = demoscene_nickvariant.nick_id
                    AND demoscene_nick.name = demoscene_nickvariant.name
                )
            WHERE
                demoscene_nickvariant.id IS NULL
        ''')
        for nick in nicks:
            if self.verbosity >= 1:
                print("creating nick_variant for %s" % nick)
            nick_variant = NickVariant(nick=nick, name=nick.name)
            nick_variant.save()

        if self.verbosity >= 1:
            print("Removing releaser abbreviations that are the same as the actual name")
        cursor = connection.cursor()
        cursor.execute('''
            UPDATE demoscene_nick
            SET abbreviation = ''
            WHERE LOWER(name) = LOWER(abbreviation)
        ''')

        if self.verbosity >= 1:
            print("Looking for Nicks without their abbreviation as a NickVariant")

        nicks = Nick.objects.raw('''
            SELECT demoscene_nick.*
            FROM
                demoscene_nick
                LEFT JOIN demoscene_nickvariant ON (
                    demoscene_nick.id = demoscene_nickvariant.nick_id
                    AND demoscene_nick.abbreviation = demoscene_nickvariant.name
                )
            WHERE
                demoscene_nick.abbreviation <> ''
                AND demoscene_nickvariant.id IS NULL
        ''')
        for nick in nicks:
            if self.verbosity >= 1:
                print("creating nick_variant for %s" % nick.abbreviation)
            nick_variant = NickVariant(nick=nick, name=nick.abbreviation)
            nick_variant.save()

        if self.verbosity >= 1:
            print("Truncating fuzzy dates to first of the month / first of January")
        cursor = connection.cursor()
        cursor.execute('''
            UPDATE productions_production
            SET release_date_date = date_trunc('month', release_date_date)
            WHERE
                release_date_precision = 'm'
                AND date_part('day', release_date_date) <> 1
        ''')
        cursor.execute('''
            UPDATE productions_production
            SET release_date_date = date_trunc('year', release_date_date)
            WHERE
                release_date_precision = 'y'
                AND (date_part('day', release_date_date) <> 1 or date_part('month', release_date_date) <> 1)
        ''')

        if self.verbosity >= 1:
            print("Removing abbreviations on scener nicks")
        nicks = Nick.objects.exclude(abbreviation='').filter(releaser__is_group=False)
        for nick in nicks:
            if self.verbosity >= 1:
                print("Removing abbreviation %s of %s" % (nick.abbreviation, nick.name))
            nick.abbreviation = ''
            nick.save()

        if self.verbosity >= 1:
            print("Stripping leading / trailing spaces from names and titles")
        cursor.execute('''
            UPDATE productions_production
            SET title = REGEXP_REPLACE(title, E'^\\\\s*(.*?)\\\\s*$', E'\\\\1', 'g')
            WHERE title LIKE ' %%' OR title LIKE '%% '
        ''')
        cursor.execute('''
            UPDATE demoscene_releaser
            SET name = REGEXP_REPLACE(name, E'^\\\\s*(.*?)\\\\s*$', E'\\\\1', 'g')
            WHERE name LIKE ' %%' OR name LIKE '%% '
        ''')
        cursor.execute('''
            UPDATE demoscene_nick
            SET name = REGEXP_REPLACE(name, E'^\\\\s*(.*?)\\\\s*$', E'\\\\1', 'g')
            WHERE name LIKE ' %%' OR name LIKE '%% '
        ''')
        cursor.execute('''
            UPDATE demoscene_nickvariant
            SET name = REGEXP_REPLACE(name, E'^\\\\s*(.*?)\\\\s*$', E'\\\\1', 'g')
            WHERE name LIKE ' %%' OR name LIKE '%% '
        ''')
        cursor.execute('''
            UPDATE parties_party
            SET name = REGEXP_REPLACE(name, E'^\\\\s*(.*?)\\\\s*$', E'\\\\1', 'g')
            WHERE name LIKE ' %%' OR name LIKE '%% '
        ''')
        cursor.execute('''
            UPDATE parties_partyseries
            SET name = REGEXP_REPLACE(name, E'^\\\\s*(.*?)\\\\s*$', E'\\\\1', 'g')
            WHERE name LIKE ' %%' OR name LIKE '%% '
        ''')

        # skip this. it takes ages.
        # print("Recursively marking children of deleted scene.org dirs as deleted")
        # for dir in Directory.objects.filter(is_deleted=True):
        #    dir.mark_deleted()

        if self.verbosity >= 1:
            print("Converting invitation competitions to party invitation relations")
        invitation_compos = Competition.objects.filter(name__istartswith='invitation').select_related('party')
        for compo in invitation_compos:
            placings = compo.placings.select_related('production')

            is_real_compo = False
            for placing in placings:
                if placing.ranking != '' or placing.score != '':
                    is_real_compo = True
                compo.party.invitations.add(placing.production)

            if not is_real_compo:
                compo.delete()

        if self.verbosity >= 1:
            print("Checking encodings on results files")
        results_files = ResultsFile.objects.all()
        for results_file in results_files:
            try:
                results_file.text
            except UnicodeDecodeError:
                if self.verbosity >= 1:
                    print(
                        "Error on /parties/%d/results_file/%d/ - cannot decode as %r"
                        % (results_file.party_id, results_file.id, results_file.encoding)
                    )
            except IOError:
                # ignore files that aren't on disk (which probably means this is a local dev instance with a live db)
                pass

        if self.verbosity >= 1:
            print("Deleting unused tags")
        Tag.objects.annotate(num_prods=Count('taggit_taggeditem_items')).filter(num_prods=0).delete()

        print("Fixing has_screenshots flag on productions that claim not to have screenshots but do")
        cursor.execute('''
            UPDATE productions_production SET has_screenshot = 't'
            WHERE productions_production.id IN (
                SELECT productions_production.id
                FROM productions_production
                INNER JOIN productions_screenshot on (
                    productions_production.id = productions_screenshot.production_id
                    and thumbnail_url <> ''
                )
                WHERE has_screenshot = 'f'
            )
        ''')
        print("Fixing has_screenshots flag on productions that claim to have screenshots but don't")
        cursor.execute('''
            UPDATE productions_production SET has_screenshot = 'f'
            WHERE productions_production.id IN (
                SELECT productions_production.id
                FROM productions_production
                LEFT JOIN productions_screenshot on (
                    productions_production.id = productions_screenshot.production_id
                    and thumbnail_url <> ''
                )
                WHERE productions_screenshot.production_id is null and has_screenshot = 't'
            )
        ''')

        if self.verbosity >= 1:
            print("Deleting duplicate soundtrack links")
        cursor.execute('''
            select p1.production_id, p1.soundtrack_id, min(p1.position) AS position
            from productions_soundtracklink as p1
            inner join productions_soundtracklink as p2 on (
                p1.production_id = p2.production_id and p1.soundtrack_id = p2.soundtrack_id
                and p1.id <> p2.id
            )
            group by p1.production_id, p1.soundtrack_id
        ''')
        for (prod_id, soundtrack_id, position) in cursor.fetchall():
            SoundtrackLink.objects.filter(
                production_id=prod_id, soundtrack_id=soundtrack_id, position__gt=position
            ).delete()

        if self.verbosity >= 1:
            print("Deleting duplicate pack members")
        cursor.execute('''
            select p1.pack_id, p1.member_id, min(p1.position) AS position
            from productions_packmember as p1
            inner join productions_packmember as p2 on (
                p1.pack_id = p2.pack_id and p1.member_id = p2.member_id
                and p1.id <> p2.id
            )
            group by p1.pack_id, p1.member_id
        ''')
        for (pack_id, member_id, position) in cursor.fetchall():
            PackMember.objects.filter(pack_id=pack_id, member_id=member_id, position__gt=position).delete()

        if self.verbosity >= 1:
            print("Closing gaps in pack member sequences")
        for k, pms in groupby(PackMember.objects.order_by('pack_id', 'position'), lambda pm: pm.pack_id):
            for i, pm in enumerate(pms):
                if i + 1 != pm.position:
                    pm.position = i + 1
                    pm.save()

        if self.verbosity >= 1:
            print("Closing gaps in soundtrack sequences")
        for k, stls in groupby(
            SoundtrackLink.objects.order_by('production_id', 'position'),
            lambda stl: stl.production_id
        ):
            for i, stl in enumerate(stls):
                if i + 1 != stl.position:
                    stl.position = i + 1
                    stl.save()

        if self.verbosity >= 1:
            print("Marking diskmags with pack contents as packs")
        diskmag_id = ProductionType.objects.get(name='Diskmag').id
        artpack_id = ProductionType.objects.get(internal_name='artpack').id
        pack = ProductionType.objects.get(internal_name='pack')
        for prod in Production.objects.raw('''
            SELECT distinct pack_id AS id
            FROM productions_packmember
            INNER JOIN productions_production AS packmember ON (member_id = packmember.id)
            WHERE
            pack_id NOT IN (
                select production_id from productions_production_types
                where productiontype_id in (%(pack)s, %(artpack)s)
            )
            AND pack_id IN (
                select production_id from productions_production_types where productiontype_id = %(diskmag)s
            )
            AND packmember.supertype <> 'music'
        ''', {'pack': pack.id, 'artpack': artpack_id, 'diskmag': diskmag_id}):
            prod.types.add(pack)

        if self.verbosity >= 1:
            print("Reassigning music in pack contents that looks like it should be soundtrack links")
        # get the most common prodtypes that have soundtracks mis-categorised as pack contents
        non_pack_type_ids = ProductionType.objects.filter(
            name__in=['Musicdisk', 'Chip Music Pack', 'Demo', 'Diskmag']
        ).values_list('id', flat=True)
        pack_type_id = ProductionType.objects.get(name='Pack').id
        actual_pack_ids = Production.objects.filter(types__id=pack_type_id).values_list('id', flat=True)
        # get packmembers which are music, inside productions that are one of our target types, but not actual packs
        pack_contents = PackMember.objects.filter(
            pack__types__in=non_pack_type_ids, member__supertype='music'
        ).exclude(
            pack_id__in=actual_pack_ids
        ).order_by('pack_id', 'position')
        for pack_id, pack_members in groupby(pack_contents, lambda pm: pm.pack_id):
            soundtrack_ids = list(
                SoundtrackLink.objects.filter(production_id=pack_id).order_by('position')
                .values_list('soundtrack_id', flat=True)
            )
            for pack_member in pack_members:
                if pack_member.member_id not in soundtrack_ids:
                    SoundtrackLink.objects.create(
                        production_id=pack_id, soundtrack_id=pack_member.member_id,
                        position=len(soundtrack_ids) + 1, data_source=pack_member.data_source
                    )
                    soundtrack_ids.append(pack_member.member_id)
                pack_member.delete()

        if self.verbosity >= 1:
            print("done.")
示例#6
0
	def __init__(self, search_term, selection=None,
		sceners_only=False, groups_only=False,
		group_names=[], member_names=[]):

		self.search_term = search_term

		nick_variants = NickVariant.autocompletion_search(
			search_term, exact=True,
			sceners_only=sceners_only, groups_only=groups_only,
			groups=group_names, members=member_names)

		self.suggestions = []

		for nv in nick_variants:
			suggestion = {
				'className': ('group' if nv.nick.releaser.is_group else 'scener'),
				'nameWithAffiliations': nv.nick.name_with_affiliations(),
				'name': nv.nick.name,
				'id': nv.nick_id
			}
			if nv.nick.releaser.country_code:
				suggestion['countryCode'] = nv.nick.releaser.country_code.lower()

			if nv.nick.differentiator:
				suggestion['differentiator'] = nv.nick.differentiator
				suggestion['nameWithDifferentiator'] = "%s (%s)" % (nv.nick.name, nv.nick.differentiator)
			else:
				suggestion['nameWithDifferentiator'] = nv.nick.name

			if nv.nick.name != nv.name:
				suggestion['alias'] = nv.name

			self.suggestions.append(suggestion)

		if not groups_only:
			self.suggestions.append({
				'className': 'add_scener',
				'nameWithAffiliations': "Add a new scener named '%s'" % search_term,
				'nameWithDifferentiator': search_term,
				'name': search_term,
				'id': 'newscener',
			})

		if not sceners_only:
			self.suggestions.append({
				'className': 'add_group',
				'nameWithAffiliations': "Add a new group named '%s'" % search_term,
				'nameWithDifferentiator': search_term,
				'name': search_term,
				'id': 'newgroup'
			})

		if selection:
			self.selection = selection
		else:
			# if there is a definite best-scoring nickvariant, select it
			if nick_variants.count() == 0:
				self.selection = None
			elif nick_variants.count() == 1 or nick_variants[0].score > nick_variants[1].score:
				self.selection = NickSelection(self.suggestions[0]['id'], self.suggestions[0]['nameWithDifferentiator'])
			else:
				self.selection = None
示例#7
0
	def __init__(self, search_term,
		author_nick_selections=[], affiliation_nick_selections=[],
		autocomplete=False):

		self.search_term = search_term

		# parse the byline
		parts = self.search_term.split('/')
		authors_string = parts[0]  # everything before first slash is an author
		affiliations_string = '^'.join(parts[1:])  # everything after first slash is an affiliation

		# split on separators that have a trailing (and optionally leading) space
		author_names = re.split(r"\s*[\,\+\^\&]\s+", authors_string)
		author_names = [name.lstrip() for name in author_names if name.strip()]
		affiliation_names = re.split(r"\s*[\,\+\^\&]\s+", affiliations_string)
		affiliation_names = [name.lstrip() for name in affiliation_names if name.strip()]

		# Now, for any item in author_names or affiliation_names with an internal separator character,
		# perform a pre-check for that name. If not present, split it and move on.
		vetted_author_names = []
		for name in author_names:
			if re.search(r"[\,\+\^\&]", name) and not NickVariant.objects.filter(name__iexact=name.strip()).exists():
				for subname in re.split(r"[\,\+\^\&]", name):
					subname = subname.lstrip()
					if subname:
						vetted_author_names.append(subname)
			else:
				vetted_author_names.append(name)

		vetted_affiliation_names = []
		for name in affiliation_names:
			if re.search(r"[\,\+\^\&]", name) and not NickVariant.objects.filter(name__iexact=name.strip(), nick__releaser__is_group=True).exists():
				for subname in re.split(r"[\,\+\^\&]", name):
					subname = subname.lstrip()
					if subname:
						vetted_affiliation_names.append(subname)
			else:
				vetted_affiliation_names.append(name)

		author_names = vetted_author_names
		affiliation_names = vetted_affiliation_names

		# attempt to autocomplete the last element of the name,
		# if autocomplete flag is True and search term has no trailing ,+^/& separator
		if autocomplete and not re.search(r"[\,\+\^\/\&]\s*$", self.search_term):
			if affiliation_names:
				autocompletion = NickVariant.autocomplete(
					affiliation_names[-1],
					significant_whitespace=False,
					groups_only=True, members=[name.strip() for name in author_names])
				affiliation_names[-1] += autocompletion
				self.search_term += autocompletion
			elif author_names:
				autocompletion = NickVariant.autocomplete(
					author_names[-1],
					significant_whitespace=False)
				author_names[-1] += autocompletion
				self.search_term += autocompletion

		author_names = [name.strip() for name in author_names]
		affiliation_names = [name.strip() for name in affiliation_names]

		# construct a NickSearch for each element
		self.author_nick_searches = []
		for (i, author_name) in enumerate(author_names):
			try:
				selection = author_nick_selections[i]
			except IndexError:
				selection = None
			self.author_nick_searches.append(
				NickSearch(author_name, selection, group_names=affiliation_names)
			)

		self.affiliation_nick_searches = []
		for (i, affiliation_name) in enumerate(affiliation_names):
			try:
				selection = affiliation_nick_selections[i]
			except IndexError:
				selection = None
			self.affiliation_nick_searches.append(
				NickSearch(affiliation_name, selection, groups_only=True, member_names=author_names)
			)

		self.author_nick_selections = [nick_search.selection for nick_search in self.author_nick_searches]
		self.affiliation_nick_selections = [nick_search.selection for nick_search in self.affiliation_nick_searches]
示例#8
0
	def handle_noargs(self, **options):
		print "Looking for releasers without their name as a Nick"

		releasers = Releaser.objects.raw('''
			SELECT demoscene_releaser.*
			FROM
				demoscene_releaser
				LEFT JOIN demoscene_nick ON (
					demoscene_releaser.id = demoscene_nick.releaser_id
					AND demoscene_releaser.name = demoscene_nick.name
				)
			WHERE
				demoscene_nick.id IS NULL
		''')
		for releaser in releasers:
			print "creating nick for %s" % releaser
			nick = Nick(releaser=releaser, name=releaser.name)
			nick.save()

		print "Looking for Nicks without their name as a NickVariant"

		nicks = Nick.objects.raw('''
			SELECT demoscene_nick.*
			FROM
				demoscene_nick
				LEFT JOIN demoscene_nickvariant ON (
					demoscene_nick.id = demoscene_nickvariant.nick_id
					AND demoscene_nick.name = demoscene_nickvariant.name
				)
			WHERE
				demoscene_nickvariant.id IS NULL
		''')
		for nick in nicks:
			print "creating nick_variant for %s" % nick
			nick_variant = NickVariant(nick=nick, name=nick.name)
			nick_variant.save()

		print "Removing releaser abbreviations that are the same as the actual name"
		cursor = connection.cursor()
		cursor.execute('''
			UPDATE demoscene_nick
			SET abbreviation = ''
			WHERE LOWER(name) = LOWER(abbreviation)
		''')

		print "Looking for Nicks without their abbreviation as a NickVariant"

		nicks = Nick.objects.raw('''
			SELECT demoscene_nick.*
			FROM
				demoscene_nick
				LEFT JOIN demoscene_nickvariant ON (
					demoscene_nick.id = demoscene_nickvariant.nick_id
					AND demoscene_nick.abbreviation = demoscene_nickvariant.name
				)
			WHERE
				demoscene_nick.abbreviation <> ''
				AND demoscene_nickvariant.id IS NULL
		''')
		for nick in nicks:
			print "creating nick_variant for %s" % nick.abbreviation
			nick_variant = NickVariant(nick=nick, name=nick.abbreviation)
			nick_variant.save()

		print "Truncating fuzzy dates to first of the month / first of January"
		cursor = connection.cursor()
		cursor.execute('''
			UPDATE productions_production
			SET release_date_date = date_trunc('month', release_date_date)
			WHERE release_date_precision = 'm'
		''')
		cursor.execute('''
			UPDATE productions_production
			SET release_date_date = date_trunc('year', release_date_date)
			WHERE release_date_precision = 'y'
		''')

		print "Removing abbreviations on scener nicks"
		nicks = Nick.objects.exclude(abbreviation='').filter(releaser__is_group=False)
		for nick in nicks:
			print "Removing abbreviation %s of %s" % (nick.abbreviation, nick.name)
			nick.abbreviation = ''
			nick.save()

		print "Stripping leading / trailing spaces from names and titles"
		cursor.execute('''
			UPDATE productions_production
			SET title = REGEXP_REPLACE(title, E'^\\\\s*(.*?)\\\\s*$', E'\\\\1', 'g')
			WHERE title LIKE ' %%' OR title LIKE '%% '
		''')
		cursor.execute('''
			UPDATE demoscene_releaser
			SET name = REGEXP_REPLACE(name, E'^\\\\s*(.*?)\\\\s*$', E'\\\\1', 'g')
			WHERE name LIKE ' %%' OR name LIKE '%% '
		''')
		cursor.execute('''
			UPDATE demoscene_nick
			SET name = REGEXP_REPLACE(name, E'^\\\\s*(.*?)\\\\s*$', E'\\\\1', 'g')
			WHERE name LIKE ' %%' OR name LIKE '%% '
		''')
		cursor.execute('''
			UPDATE demoscene_nickvariant
			SET name = REGEXP_REPLACE(name, E'^\\\\s*(.*?)\\\\s*$', E'\\\\1', 'g')
			WHERE name LIKE ' %%' OR name LIKE '%% '
		''')
		cursor.execute('''
			UPDATE parties_party
			SET name = REGEXP_REPLACE(name, E'^\\\\s*(.*?)\\\\s*$', E'\\\\1', 'g')
			WHERE name LIKE ' %%' OR name LIKE '%% '
		''')
		cursor.execute('''
			UPDATE parties_partyseries
			SET name = REGEXP_REPLACE(name, E'^\\\\s*(.*?)\\\\s*$', E'\\\\1', 'g')
			WHERE name LIKE ' %%' OR name LIKE '%% '
		''')

		# skip this. it takes ages.
		# print "Recursively marking children of deleted scene.org dirs as deleted"
		# for dir in Directory.objects.filter(is_deleted=True):
		#	dir.mark_deleted()

		print "Converting invitation competitions to party invitation relations"
		invitation_compos = Competition.objects.filter(name__istartswith='invitation').select_related('party')
		for compo in invitation_compos:
			placings = compo.placings.select_related('production')

			is_real_compo = False
			for placing in placings:
				if placing.ranking != '' or placing.score != '':
					is_real_compo = True
				compo.party.invitations.add(placing.production)

			if not is_real_compo:
				compo.delete()

		print "Checking encodings on results files"
		results_files = ResultsFile.objects.all()
		for results_file in results_files:
			try:
				results_file.text
			except UnicodeDecodeError:
				print "Error on /parties/%d/results_file/%d/ - cannot decode as %r" % (results_file.party_id, results_file.id, results_file.encoding)
			except IOError:
				pass  # ignore files that aren't on disk (which probably means this is a local dev instance with a live db)

		print "Deleting unused tags"
		Tag.objects.annotate(num_prods=Count('taggit_taggeditem_items')).filter(num_prods=0).delete()

		print "Setting has_screenshots flag on productions"
		cursor.execute('''
			UPDATE productions_production SET has_screenshot = (
				id IN (
					SELECT DISTINCT production_id
					FROM productions_screenshot
					WHERE thumbnail_url <> ''
				)
			)
		''')

		print "done."